#1, Hello World - Twistx77/Winja101 GitHub Wiki
Winja Lab Guide #1, Hello World!
In this first exercise, we will be implementing the most famous beginner program, the Hello World, which in hardware basically means to blink a LED. We will be using the Winja 101 to blink a LED at a desired rate. This will allow you to get comfortable with the programming environment and the basics of the board.
Objectives
- Learn the basic structure of an Arduino Sketch.
- Learn how to control a digital output pin.
- Learn the basics about LEDs.
- Using delays.
- Program the board using the USB port.
Hardware Introduction
The hardware used for this exercise is really simple since it only consists on three components, the LED (D3), the resistor (R17) and the ESP32 which will only be using a general purpose pin (IO17, Pin 28). In the schematic we can see that the ESP32 pin and the LED are connected using a label designed "LED" which basically connects both nodes electrically and it is used to reduce the number of lines in the schematic making in it easily readable.
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.