#2, Digital Inputs - Twistx77/Winja101 GitHub Wiki
Winja Lab Guide #2, Digital Inputs!
In this second exercise we will be learning how to read a digital input which electrically means that we will check if there is a LOW (~0V) or HIGH (~3.3V) value in one of the pins of the ESP32. To do this we will be using the Winja's 101 user push button and the user LED used in the last lab exercise.
Objectives
- Learn how to setup digital inputs in the Arduino environment.
- Learn what pull-up and pull-down resistors are and what are they used for.
- Learn how to control the user LED with the user push button.
Hardware Introduction
In this exercise we will be using the same hardware used in the first exercise, the LED (D3) with its resistor and also the user push button (BTN) and capacitor (C19). To control the LED, we will be using again the pin IO17 of the ESP32 and to read the state of the push button we will be using the pin IO16 as shown in the board.
As shown in the picture above the LED has one lead connected in series to a the resistor which connects it to the supply power (3.3V). The second lead of the LED is connected to the IO17 of the ESP32. This way, the LED will turn on when the current flows through it which only happens when the IO17 pin is at a low voltage (~ 0V), a digital LOW. On the contrary, when the IO17 pin is set to HIGH, which in this case corresponds to a voltage close to the supply power (~ 3.3V), the LED will not light up since there is no potential difference between both sides of the LED.
In the Winja 101 board you can see where are the components associated to this first lab in the image below.
You can see that in the silkscreen (white text in the real board) there is written not only the components desginators (R17, D3) but also the IO of the ESP32 to which the LED is connected. In this case IO17 so you don't have tor remember it.
Note: The resistor is used to limit the current that flows through the LED since too much current will burn the LED. To determine the value of the resistor that is needed we need to know:
- The current we want for the LED which will be determined by the light we want the LED to emit and the limits given by the datasheet.
- The voltage drop of the LED given in the datasheet. (Always the works case which is the lowest value since the lower it is the more current will flow through the LED and thus the more possible it is to be burnt),
- The voltage different in the entire brunch from the top of the resistor ( 3.3V of the supply) and the voltage in the ESP32 IO17 pin when it is LOW (~ 0V). In this case the current we want to flow through the LED is 1 mA (miliAmperes), the drop of the LED (150060BS75000 model) at 1mA is 2.7V. We assume that the voltage in the ESP32 IO17 pin when is LOW is 0V and since our supply voltage is 3.3V we can calculate the resistor using Ohm's Law ( V = I*R ) and isolate R to get the value of the resistor needed:
'R = V/I R = (3.3V - 2.7V) /5mA) R = 0.6V/0.001A R = 600'
So the value of the resistor needed is 600 Ohms.
Code
The code in this exercise will have the basic structure of an Arduino sketch.
#define LED_DELAY 1000;
const int pinLED = IO17;
void setup(){
pinMode(pinLED, OUTPUT);
}
void loop() {
digitalWrite(pinLED, HIGH);
delay(LED_DELAY);
digitalWrite(pinLED, LOW);
delay(LED_DELAY);
}
The first line #define LED_DELAY 1000
just defines a constant named LED_DELAY with the value of 500. This is used later to set the time between LED state changes, this way if you have many places in the code where you use it and you want to change it values you will only have to change it in this part of the code. It also helps to make the code more readable.
const int pinLED = IO17;
defines also a constant but in this case the pin that we will be using to control the LED which is IO17 as we saw in the schematic above.
The void setup() section of the code will execute what is inside of it only once every time the board is powered or reset so it is used to setup our hardware peripherals. In this case, we only need to set our IO17 pin to an output so we can set the value of the pin from a digital HIGH (~3.3V) to a digital LOW (~0V) and vice-versa thus making the LED turn ON and OFF. To do so we use pinMode(pinLED, OUTPUT);
In the last section of the code, void loop(), we add the things we want the board to do indefinitely. In this case, we want to turn the LED on, then wait a second, turn it OFF wait a second and do it all over again. To do so, we have the following code:
digitalWrite(pinLED, LOW); // Turns the LED ON
delay(LED_DELAY); // Waits LED_DELAY miliseconds, in this case 1000
digitalWrite(pinLED, HIGH); // Turns the LED OFF
delay(LED_DELAY); // Waits LED_DELAY miliseconds, in this case 1000
It is important to remark that the LED turns on when the IO17 is set to low (Inverse Logic) because of the way it is connected in the hardware as it is explained above.