搜索代码

Controlling DC Motor Speed Using DHT11 and DHT22 Temperature Sensors with an L298N Module

Controlling DC Motor Speed Using DHT11 and DHT22 Temperature Sensors with an L298N Module

This code is to control the speed of a motor with temperature. As the temperature increases, the speed of the motor increases, and as the temperature decreases, the speed of the motor decreases. We have used an L298N motor controller module to drive the motor. DHT11 and DHT22 sensors can be used to sense the temperature.

Resources for this sketch

260-Speed of a Motor with Temperature using DHT11, DHT22 Sensors
语言: C++
/*
 * Control the Speed of a Motor with Temperature using DHT11, DHT22 Sensors
 * using a Motor driver L298N Module 
 * 
 * This code is to control a single motor.
 * 
 * Written by Ahmad Shamshiri on Nov 11, 2019 at 05:40
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * 
 * Watch video instructions for this code:https://youtu.be/5xQ8vLavWKQ
 * 
 * You must watch 
 * 1-Introduction to L298N with driver (install library)
 * 2-DHT22, DHT11 Introduction video (install library)
 * 
 * Get this code and other Arduino codes from Robojax.com.

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 2
#define IN2 4
#define ENA 3 // this pin must be PWM enabled pin

const int CCW = 2; // do not change
const int CW  = 1; // do not change
const motorDirection = CW;// set your motor direction
#define motor1 1 // do not change
// use the line below for single motor
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, true);

const float tempMin = 40.0;// (watch video)
const int speedMin =30;//speed in % from 0 to 100

const float tempMax = 65.0;// (watch video)
const int speedMax =100;//speed in % from 0 to 100

int motorSpeed;// variable holding motor speed
//Setting for DHT sensor
#include "DHT.h"
#define DHTPIN 6 
// Uncomment(remove  //) whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("Robojax L298N with Motor");
  Serial.println("Speed Depends on Temperature");
  Serial.println();      
  motor.begin();
  dht.begin(); 
  //L298N DC Motor by Robojax.com

}

void loop() {
  // Robojax.com Temperature to speed
  tempToSpeed();
  Serial.print("motorSpeed:"); 
  Serial.println(motorSpeed); 
  //motor.demo(1);// to test your motor
  motor.rotate(motor1, motorSpeed, motorDirection);//run motor1 at 60% speed in CW direction
 
  delay(500);

//  motor.brake(1);

  
}

/*
 * tempToSpeed()
 * gets temperature and proportionally sets the speed of the motor
 * returns nothing
 * written by Ahmad Shamshiri on Nov 11, 2019
 * in Ajax, Ontario, Canada 
 * www.Robojax.com
 */
void tempToSpeed()
{
  float t = dht.readTemperature();// read temperature in C  
  //float t = dht.readTemperature(true);// read temperature in F
  Serial.print("T:"); 
  Serial.print(t,1);  
  Serial.println(" C");
  motorSpeed = map(t, tempMin, tempMax, speedMin,speedMax); 
  if(t <tempMin)
  {
    motorSpeed=0;
    motor.brake(1);
  }
    

  if(t >tempMax)
  {
    motorSpeed=100;
  }


}//tempToSpeed() end

文件📁

没有可用的文件。