Control speed of DC motor using temperature sensor DHT11, DHT22 with L298N Module
How to control speed of DC motor with Temperatur using DHT11 and DHT22 sensor with L298N Module
This code is to control speed of motor with temperature. As the temperature increases the speed of motor increases and as the temperature decreaases the speed of motor decreased. We have used L298N motor controller moduel to drive the motor. DHT11 and DHT22 can be used to sense the temperature.
Resources for this sketch
- Robojax L298N DC Motor Library (zip)
- Getting started DHT11, DHT22 with Arduino (code and video)
- Introduction to L298N DC motor control Module (code and video)
- Robojax Arduino Course on Udemy
- Robojax Arduino Course on Udemy
- Get Early Access to my videos via Patreon
/*
* Control Speed of Motor with Temperatur using DHT11, DHT22 Sensors
* using Motor driver L298N Module
*
* This code is to control single motor.
*
* Written by Ahmad Shamshiri on Nov 11, 2019 at 05:40
* in Ajax, Ontario, Canada. www.robojax.com
*
*
* Watch video instruction for this code:https://youtu.be/5xQ8vLavWKQ
*
* You must watch
* 1-introdution to L298N with driver (install library)
* 2-DHT22, DHT11 Introduction video (install library)
*
* Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62
If you found this tutorial helpful, please support me so I can continue creating
content like this. You can support me on Patreon http://robojax.com/L/?id=63
or make donation using PayPal http://robojax.com/L/?id=64
* * 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 download 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
// Uncommentz(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 set speed of mootr
* 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