light sensor + DC motor
We are going to learn :
-
How light sensor works.
-
How to connect light sensor to ESP32.
-
Turns DC motor on if the analog value measured from light sensor's is below a threshold.
Video Tutorial
you can watch this video tutorial
Hardware Required
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | Micro USB Cable | |
1 | × | L298N Motor Driver Module | |
1 | × | DC Motor | |
1 | × | Light Sensor | |
1 | × | LED | |
1 | × | 220 ohm resistor | |
1 | × | 10 kΩ resistor | |
1 | × | Breadboard | |
n | × | Jumper Wires |
Introduction to DC Motor and Light Sensor
DC Motor
We have specific tutorials about DC motor contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, ESP32 code... Learn more at the following link : DC Motor
Introduction to Light Sensor
The most wide-used light sensor is a photoresistor (also known as photocell, or light-dependent resistor, LDR).
It can be used to detect the presence ofthe light. It can also be used to measure the illuminance/brightness level of the light.
Light Sensor Pinout
A light sensor has two pins. Just like a normal resistor, We do NOT need to distinguish these pins.
How Light Sensor Works
The photoresistor's resistance is in inverse proportion to the intensity of the light. The less light the photoresistor's face is exposed, the more the photoresistor's resistance is. Therefore, we can infer how bright the ambient light is by measuring the photoresistor's resistance.
※ NOTE THAT:
The value measured by photoresistor reflects the approximated tendency of the light's intensity, it does NOT represent exactly the luminous flux. Therefore, the photoresistor should not be used in an application that requires high accuracy. calibration is also required for some kind application.
ESP32 - Light Sensor
The ESP32's analog input pin converts the voltage (between 0v and ADC_VREF - default is 3.3V) into integer values (between 0 and 4095), called analog value or ADC value. By connecting an analog input pin of ESP32 to the photoresistor, we can read the analog value by using analogRead() function.
Introduction to L298N Driver
L298N Driver can be used to control DC motor and stepper motor. In this tutorial, we learn how to use it to control the DC motor.
L298N Driver Pinout
L298N Driver can control two DC motor independently at the same time, called motor A and motor B. L298N Driver has 13 pins:
The common pins for both motors:
-
VCC pin: supplies power for the motor. It can be anywhere between 5 to 35V.
-
GND pin: is a common ground pin, needs to be connected to GND (0V).
-
5V pin: supplies power for L298N module. It can be supplied by 5V from ESP32.
Motor A pins (Channel A):
-
ENA pins: are used to control the speed of Motor A. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor A.
-
IN1 & IN2 pins: are used to control the spinning direction of Motor A. When one of them is HIGH and the other is LOW, the Motor A will spin. If both the inputs are either HIGH or LOW the Motor A will stop.
-
OUT1 & OUT2 pins: are connected to Motor A.
Motor B pins (Channel B):
-
ENB pins: are used to control the speed of Motor B. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor B.
-
IN3 & IN4 pins: are used to control the spinning direction of Motor B. When one of them is HIGH and the other is LOW, Motor B will spin. If both the inputs are either HIGH or LOW the Motor B will stop.
-
OUT3 & OUT4 pins: are connected to Motor B.
As described above, the L298N driver has two input powers:
-
One for DC motor (VCC and GND pins): from 5 to 35V.
-
One for the L298N module's internal operation (5V and GND pins): from 5 to 7V.
The L298N driver also has three jumpers for advanced uses or other purposes. For the sake of simplicity, please remove all jumpers from the L298N driver.
We can control two DC motors independently at the same time by using an ESP32 and an L298N Driver. To control each motor, we need only three pins from ESP32.
How To Control the Speed of DC Motor via L298N Driver
This a list of the most useful functions to use ESP-Now if we want to exchange data between ESP devices:
-
Connect an ESP32 pin to ENA of L298N
-
Generate PWM signal to ENA pin by using ledcWrite() function. L298N Driver amplify PWM signal to DC motor
The speed is a value between 0 and 255. If the speed is 0, the motor stops. If the speed is 255, the motor spins at maximum speed.
How To Control the Direction of DC Motor via L298N Driver
The spinning direction of a motor can be controlled by applying a logic HIGH/LOW to IN1 and IN2 pins. The below table illustrates how to control the direction in both channels.
IN1 pin | IN2 pin | Direction |
---|---|---|
LOW | LOW | Motor A stops |
HIGH | HIGH | Motor A stops |
HIGH | LOW | Motor A spins Clockwise |
LOW | HIGH | Motor A spins Anti-Clockwise |
-
Control motor A spins clockwise
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW);
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH);
※ NOTE THAT:
The spinning direction is inverted if the OUT1 & OUT2 pin connects to two pins of the DC motor in an inverse way. If so, it just needs to swap between OUT1 & OUT2 pin or change the control signal on IN1 and IN2 pin in the code.
How To Stop DC Motor Spinning
There are two ways to stop DC motor
-
Controls the speed to 0
analogWrite(ENA_PIN, 0);
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW);
or
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH);
ESP32 Code
The below code Turns off the motor if it is dark and the more lighting the more motor's speed
#define LIGHT_SENSOR_PIN 12 // ESP32 pin GIOP36 (ADC0) connected to light sensor // Motor A int motor1Pin1 = 21; int motor1Pin2 = 19; int enable1Pin = 18; // Setting PWM properties const int m_freq = 30000; const int m_pwmChannel = 3; const int m_resolution = 8; void setup() { pinMode(LIGHT_SENSOR_PIN, INPUT); // sets the pins as outputs: pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(enable1Pin, OUTPUT); // configure LED PWM functionalitites ledcSetup(m_pwmChannel, m_freq, m_resolution); // attach the channel to the GPIO to be controlled ledcAttachPin(enable1Pin, m_pwmChannel); } void loop() { int analogValue = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin // We'll have a few threshholds, qualitatively determined if (analogValue < 40) { // Stop the DC motor digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); } else if (analogValue < 800) { digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); ledcWrite(m_pwmChannel, 50); } else if (analogValue < 2000) { digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); ledcWrite(m_pwmChannel, 100); } else if (analogValue < 3200) { digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); ledcWrite(m_pwmChannel, 150); } else { digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); ledcWrite(m_pwmChannel, 200); } }
Quick Instructions
-
Remove all three jumpers on the L298N module.
-
Copy the above code and paste it to Arduino IDE.
-
Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
-
You will see:
the motor is turned off if it is dark and the more lighting the more motor's speed
References
※ NOTE THAT:
Some components works on 3.3v and others works on 5v!