An ESP32 will be the brains of your device, and you will attach various inputs and outputs to it.
We will be programming it in Python during this club; the version of Python used is MicroPython, which means that it's a smaller version of the full Python programming language designed to run on microcontrollers such as the ESP32 and micro:bit.
A lot of background set-up code has been prepared in advance, and so you will use lots of libraries in your program. This is how professional programmers work - it is unusual to start everything from scratch yourself!
We will be using Thonny as the editor during these workshops.
Code is saved in two places - on your computer, and on the ESP32.
While you are working on the code, run it from your computer (it will still interact with the ESP32) - only when you are happy with it should you upload it to the device.
Double-click on the file named first-test.py to open it.
Without changing anything, click the Run button on Thonny's menu.
Look at Thonny's terminal - what information are you being given?
Look at your ESP32 - what is it doing?
Look at your LCD screen - what is it showing?
The terminal is very useful for debugging. We use this syntax to send messages to it:
print("this message will show on the terminal!")
In this code, to pause between commands, we are not using standard sleep() functions - this is because of all the background code that needs to keep in sync with everything else.
To pause, we use this syntax:
await asyncio.sleep(1)
The ESP32's built-in LED is flashing.
The screen has two lines which you can put information on in the form of strings.
Because Python usually starts counting from zero,
lcd[0]lcd[1]After the screen has been set up, you set the string that you want each line to show.
lcd = LCD()
lcd[0] = "Welcome to"
lcd[1] = "Science Oxford"
Test out this code:
lcd.scroll(0, "Hello")
speed=200An RGB has been connected to your board as well - this is three lights in one.
We have told the ESP32 where they have been connected, using this syntax:
red = Pin(14, Pin.OUT, Pin.PULL_DOWN)
To use them, you can turn on, or off, each of the three colours. For example:
red.on()
green.off()
blue.off()
Once you have turned on a colour, it will stay on until you tell it to turn back off again.
To change from red to blue, you need to turn off red and turn on blue, if you don't turn off red, you will get purple.
To make it easier to use lots of colours, you can define some functions for each.
# define your new function
def make_purple():
red.on()
green.off()
blue.on()
# use your new function
make_purple()
Until now, we have been setting up code to happen just once.
You might have noticed the while True near the end of the programme, which currently doesn't do very much.
while True is Python's version of a forever loop. Currently, the thing that happens forever is:
gc.collect()
This code runs in the background to free up some memory, and helps all the code run smoothly!
You can add more things into this loop though, for example, to flash an LED.
Try this code example:
while True:
gc.collect()
if red.value() == 1:
red.off()
else:
red.on()
await asyncio.sleep(1)
This causes the red led to flash every time the loop runs.
Now that you have practiced with some of the outputs, we can add in the inputs - specifically the environmental sensor for your air quality monitor!
Save first-test.py, and open session1.py in Thonny.
There is a lot more code in this one, as more libraries and more set-up code is necessary to get everything working.
Look through the file without changing anything first - which parts match what you have used so far?
You will be able to get four main pieces of data from the sensor:
In pairs, investigate one of these sensors, finding our information such as:
Currently, the sensor information is showing up in the terminal - can you find the four sensors amongst the information?
We have created four variables for the four sensors, in preperation for displaying on the screen - look for them in your code.
To start with, pick two sensors to show on your LCD screen, in the example below, I have chosen Temperature and CO2 levels:
lcd[0] = str(temp) lcd[1] = str(co2)
The sensor data updates every 30 seconds.