Lab 10 - Getting started with Arduino & ESP32
Introduction: Breadboards & Components
We will assemble a basic circuit for reading an analog sensors value and controlling a LED. Components we will work with are:
- ESP32 development board - A microcontroller board with integrated WiFi and Bluetooth capabilies.
- Light Emitting Diode (LED)
- Resistors
- 10kΩ Resistor - let's label it R1
- 100Ω Resistor - let's label it R2
- Photocell, also known as Photoresistor, a kind of resistor whose resistance is based on the light level.
- Breadboard and jumper wires - for laying out the components.
Setting up
- Install latest version of Arduino Desktop IDE
- Also install USB-UART drivers
- Start Arduino and open Preferences window.
- File -> Preferences or Arduino -> Preferences (macOS)
- Enter
https://dl.espressif.com/dl/package_esp32_index.json
into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas. - Open Boards Manager from Tools > Board > Boards Manager menu and install esp32platform(esp32 by Espressif Systems)
- After installing, you can select ESP32 Dev Module board from Tools > Board as the currently used board.
- You also have to specify the (USB) port to use (assuming device has been connected!)
- On Windows it will be something like: COM4
- On Linux, it will be something like: /dev/tty/USB0
- On Mac, it will be something like: /dev/cu.usbserial-0001
Hello World
Let's use the serial interface between the board and our development machine to see output from the program executed on the ESP32. Create a new project. In setup():
Serial.begin(9600); // initialize serial communication at 9600 bits per second Serial.println("Running setup() function!");
In loop():
Serial.println("Hello!"); delay(1000); // wait for a second
Compile & Upload the code to your board, then open Serial Monitor (Tools > Serial Monitor). You should be able to see the output we printed in code.
- If you are seeing
connecting....____....
for more than 10 seconds, then try holding the "BOOT" button on the board while it Arduino uploader is connecting to the board.
- If you are seeing
Establishing power rails on the breadboard
Now we can start adding additional components to our set-up.
- Wire the common ground (GND) pin from ESP32 to the negative (blue) power rail of the breadboard
- Wire the 3.3V pin from ESP32 to the positive (red) power rail of the breadboard.
As a result, we components have easy access to both GND and voltage source, regardless of where they are physically placed on the breadboard.
Reading a Sensor - Photoresistor
We will add the photocell, wiring it up to form a voltage divider circuit. See here (scroll to Reading Resistive Sensors) for more information.
Wiring:
- From one leg of the photocell, there's a connection to R1 and pin D2 of ESP32
- The other leg of the photocell is connected to the 3.3V power line.
- The R1 is connected to photocell on one leg, and to GND.
Add the photoresistor, together with resistor R1 as shown below:
Code:
Outside of the setup() or loop() functions, let's put the pin we are using to read analog values in a constant:
const int sensorPin = 2; int lightVal; // light reading
- In loop():
lightVal = analogRead(sensorPin); // read the current light levels //TODO: write value to serial output
Finish the above code and run the code. Then, you should be able to see numeric values in your Serial Monitor. Try also using the Serial Plotter.
Controlling a LED
Wiring:
Note that for LED (unlike resistors for example), the direction in which you wire the legs is important - one is positive and the other is negative. Distinguish them by noting:
- The positive lead is longer
- The negative lead has a larger, flatter edge inside the LED "bulb" part.
- Connect the negative LED leg to GND.
- Connect the positive LED leg to R2
- Connect the other leg of R2 to D5 of ESP32.
Code:
const int ledPin= 5;
- store reference to sensorPin we are using
First, we have to setup the LED pin to act as an output:
// inside setup(): pinMode(ledPin, OUTPUT);
We can change the state of the LED with:
//inside loop(): digitalWrite (ledPin, HIGH); // turn on light delay(500); digitalWrite (ledPin, LOW); // turn off light delay(500);
The above code, will blink the LED at 500ms interval.
Feedback loop
Now, write code which turns the LED on or OFF based on a fixed light sensor value. You can use a simple if-else based logic.
For reference on the Arduino language, you can refer to the documentation. For this task, it is useful to check Control structures -> Else.