02 Analog to Digital Converter (ADC) | Pulse Width Modulation (PWM) | Hall Effect | Touch | ESP32

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for today's lesson you'll learn how to work with analog input signals and pulse width modulation on your esp32 we'll explore some of the common built-in sensors available on your esp32 such as the hall effect sensor and touch sensors we'll test the analog to digital converters of your esp32 using a potentiometer you'll also learn how to set up pwm channels and attach them to multiple gpio pins of your esp32 at the end of this lesson you should be able to create a prototype consisting of red green and blue LEDs where we can adjust the light intensity through a potentiometer and can be selected through the touch sensors hi my name is Joe Edgar and welcome to lesson 2 of the iot development training course with esp32 for this experiment you'll need the following materials your esp32 development board three LEDs red green and blue three current limiting resistors values could range from 150 ohms to 470 ohms a strong magnet 10 kilo ohm potentiometer a couple of connecting wires breadboard and a micro USB data cable let's look at our circuit diagram we'll start by connecting our potentiometer to our breadboard recall that it has three terminals one end will be connected to the ground and the other end to the positive 3.3 volts of your esp32 the middle terminal which is the wiper is going to be connected to one of the 18 available analog to digital converter pins of our esp32 you can use any but for this demonstration I'll be using the ADC on gpio 13. then the three LEDs red green and blue with their corresponding current limiting resistors will be connected to gpio 22 gpio19 and gpio 18 respectively again you can use any gpio pins available that you desire and finally we'll attach three floating wires for our touch sensors similarly you can use any of the 10 available capacitive touch sensor pins of your esp32 here I'll be using touch zero touch 3 and Touch 5 which are connected to gpio4 gpio15 and gpio12 respectively now let's Implement our circuit connection place your esp32 board on the breadboard make sure that you connect the 3.3 volt and the ground pins of your esp32 board to the positive and negative bus strips of your breadboard on both sides [Music] thank you [Music] thank you [Music] foreign [Music] codes to test our connection first we'll test the built-in hall effect sensor on your esp32 the sensor is located behind the metal lid of your esp32 module so what is a hall effect sensor and what are its common use cases well a whole effect sensor is a type of magnetic sensor that can detect variations in the magnetic field in its surroundings it can be used for detecting the strength and direction of a magnetic field produced from a permanent magnet or an electromagnet the greater the magnetic field the greater the sensors output voltage for example the hall effect sensor can be combined with threshold detection to act as a switch one of the common uses of magnetic sensors is in Automotive Systems it can be used to count the number of Revolutions of a wheel to compute the distance and speed of a car it is used to calculate positioning detect proximity detect closing of doors and much more reading the hall effect sensor measurements with the esp32 using the Arduino IDE is simple as using the whole read function let's take a look first let's define three constant values for the red LED pin at gpio 22 the green LED pin at gpio19 and the blue LED pin at gpio18 by the way the hashtag defined in Arduino is used only to give a name to the constant value like some sort of a placeholder it does not take any memory space on the chip because it only replaces all mentions of this constant with its value at the compiled time and then I'll declare and initialize an integer whole data this is where we store the value that we will be getting from the hall effect sensor in the setup function I'll set the pin mode of the three LEDs as output and let's have our serial monitor with a baud rate of 11 5200 now inside the void Loop all we need to do is use this whole read function then in the serial monitor I'll display the value we received from the hall effect sensor now let's upload our program and then let's check the values we receive in our serial monitor as you can see the value receive ranges from about 40 to 50. this is without the presence of a strong magnetic field near the esp32 module I have here a magnet for my cell phone holder in your case you can try and experiment any magnet that is strong enough to be detected now notice that as I place this magnet on top of this module the value received drops significantly reaching roughly to negative 180. now I'll twist it to the other side and the value received is greatly increased reaching to positive 300 plus now let's modify this program and use this hot effect sensor value to act as a switch and depending on the threshold that I want I could create a condition that if the whole data is below 30 or the whole data is above 60 then I can say that there is a detectable presence of a magnetic field near my esp32 module in response I'll turn on the three LEDs otherwise I'll turn it off okay I'll upload a code and now notice as I place the magnet near the esp32 module the lead starts to flicker a bit then when the condition is satisfied the LED is turned on in our connection we captured both the lower and the higher threshold of below 30 and above 60. for the LEDs to turn on and depending on the magnet you are using and your desired threshold you can change these values accordingly now let's understand how the built-in touch sensors of your esp32 work I'll Define three values red touch pin D5 green touch pin d0 and blue touch bin D3 this correspond to touch 5 Touch 0 and touch 3 of the esp32 respectively and are represented by the three hanging wires in our circuit colored red green and blue as well then I'll also declare and initialize three integer variables for red green and blue touch data these variables will hold the value received from the three touch sensors to read values from the touch sensor simply call the touch read function and pass in the gpio pin we want to read and then I'll print the value to the serial monitor then I'll duplicate this code and change this to Green and last one change this to Blue let me adjust this code so that it's a lot easier to see on the same line also I'll add a one second delay and re-upload the program again as you can see the values of our three touch sensors are ranging between 55 to 65. now see what happens if I touch this red wire the value of the red touch data drops ranging from about 12 to 7. the same goes with the sensor on the green wire as well as on the blue wire now let's use this red green and blue wires to act as a switch so I'll create a condition that if the red touch data is below a certain threshold say 50. I'll turn the red LED on otherwise I'll turn it off I'll duplicate this code twice and then change all these red to Green and all this red to blue let's upload our code and now observe what happens as I touch the red wire the value reads below 50 and the red lead is turned on the same goes with a green lid add a force with the blue lead we can touch two wires or all the wires simultaneously and our code is able to respond accordingly turning all the three LEDs simultaneously now let's understand the analog to digital converter in our esp32 using a potentiometer I'll Define another value bot bin at gpio13 this is where the wiper terminal of our potentiometer is connected to basically the analog output voltage 0 to 3.3 volts of this wiper terminal will be sent to the gpio 13 and using the analog grid function we should be getting an equivalent digital value so I'll declare and initialize an integer variable plot data this is where the equivalent digital value from our analog input will be stored note that the esp32 uses 12-bit resolution for its ADC so it means that we are expecting a possible range of values from 0 to 40.95 recall that in your Arduino Uno the ADC is only 10-bit resolution so the value unit is only between 0 to 10 23. I'll also declare another variable voltage of type load here we'll do some simple computation to compute for the equivalent voltage being read we are expecting to read a value between 0 volt to 3.3 volts next we simply call the analog grid function and pass in the gpio pin 13 Connected to our potentiometer and similarly let's display the EDC readings from our potentiometer to the serial monitor now let's upload our code as you can see the initial value being read from our potentiometer is zero now as I turn the knob clockwise toward the direction of the positive terminal it gradually increases its value and the max value is 14.95 and if I turn it on the other direction toward the Ground Terminal the value drops and the minimum value is zero now instead of displaying the value ranging from 0 to 4095 let's have a simple formula to compute for its equivalent voltage so I'll cast this integer pod data to float and then divide it by 1495 then multiplied it by 3.3 volts and finally I'll display the equivalent voltage of the 12 bit ADC reading to my serial monitor so let's upload a program if you want a full explanation on how the analog signals are converted to digital signals in Arduino Uno please check the link in the description below I'll open my serial monitor and as you can see as I rotate the knob of this potentiometer the equivalent voltage received is being displayed finally let's talk about pulse width modulation in esp32 pwm is a way to control Analog Devices through digital means so pwm is not true analog output pwm only imitates an analog-like signal by applying power in pulses or short burst of regulated voltage note that esp32 has two through digital to analog converters or dacs to control Analog Devices but for this topic we'll experiment first with the pwm so pwm is one of the primary means by which microcontrollers Drive Analog Devices like variable speed motors dimmable lights actuators and speakers through digital means in esp32 pwm must be configured first before use the esp32 has a LED pwm controller with 16 independent channels that can be configured to generate pwm signals with different properties you can use any channel from 0 to 15. in my program I'll use Channel 0. then we need to specify the pwm signal frequency recall that in Arduino Uno the pwm frequencies are 490 Hertz and 980 Hertz in esp32 we can set each pwm channel to a much higher frequency in this example I'll create a constant frequency and set it to 5000 Hertz or 5 kilohertz and finally the resolution is also configurable esp32 pwm can be programmed anywhere between 1 bit to 16-bit resolution note that in Arduino Uno the default pwm resolution is 8-bit in here let's try setting it to 12. note that the pwm frequency and resolution are inversely proportional and is dependent on the clock source now in the setup function you need to call the LED Z setup function and pass in three arguments the first is pwm channel then the frequency and the resolution next is the lead C attached pin and pass in the gpio pin that will be attached to the pwm channel note that you can attach multiple gpio pins to a single pwm channel for them to share the same configuration so I'll attach the green and the blue LED pins as well note that there are also other functions that you can use in the LED C API for example you can use the lensity touch pin function to detach a pin in a pwm channel and then finally the last function that you need to call is the lead C right this is quite similar to The arduino's Analog light function this requires two arguments pwm Channel and the duty cycle and since our pod data variable holds a 12-bit ADC value from our potentiometer we can simply pass it here our bot data has values ranging from 0 to 4095. I'll also remove this one second delay so that you can see the glowing and led fading effect in real time let's upload our code and as you can see the three LEDs glow in sync with the potentiometer's knob rotation the LED light also fade as the knob is turned toward the negative terminal note that you can experiment on these variables for example instead of 12 bit we can use 8-bit resolution so now it only has 256 discrete Duty Cycles it means from 0 to 255 that's 2 raised to 8 but since our pot data receives a value from our 12-bit analog to digital converter thus we need to map this 4096 discrete Duty Cycles to only 256 discrete Duty Cycles so I'll create another variable here to be precise I want an unsigned 16-bit integer as my duty cycle and then I'll use the map function to map the spot data from 0 to 4095 possible values to a new duty cycle value of 0 to 255 only and then I'll use this duty cycle as an argument here now let's upload and test our code [Music] and for your circuit connection and programming challenge I want you to implement a similar circuit but using different gpio pins same as with the example the three LEDs glow and fade in sync with the potentiometer's knob rotation now the three hanging wires will act as a selector to the three LEDs wherein if the blue wire is touched only the blue LED can be controlled however if the green wire is touched only the green LED can be controlled and the same goes with the red wire [Music] foreign foreign [Music] [Music] thank you very much for watching and if you've learned something of value here please click the like And subscribe button and don't forget to hit that Bell icon to get notifications for every new video that I uploaded for this course see you in the next lesson
Info
Channel: Education is Life
Views: 7,405
Rating: undefined out of 5
Keywords: ADC, PWM, Pulse Width, Pulse Width Modulation, Analog, Analog Input, Analog-to-digital converter, analog to digital conversion, potentiometer, voltage divider, touch sensor, touch, hall sensor, hall effect, hall effect sensor, PWM controller, PWM Channel, Glowing LED, Fading LED, hallRead, ledcWrite, ledcAttachPin, ledcSetup
Id: E3ibreAcAiw
Channel Id: undefined
Length: 23min 56sec (1436 seconds)
Published: Tue Sep 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.