create new tag
view all tags

Hardware access, the General Purpose Input Output (GPIO) pins.

Running the programs on the PC

Up to now, all programs we have written can be executed on the ESP32 but also on the PC. Make sure you import time and not utime and do not use sleep_ms, but use sleep instead. So... instead of sleep_ms(100) use sleep(0.1) and you will be able to run the programs on your PC with

python3 name_of_your_program.py

Please give it a try.

Accessing the hardware

The connection of digital input or output signals is made through General Purpose Input Output pins. These pins can be programmed to output (control) a signal level or to input (acquire) a signal level.

The base board and the back of the CPU card look like this (the pins on the ESP32 CPU are mirrored with respect to the base board):

The triple base board The back of the ESP32 CPU board
tripleBase.png esp32_back.png

As you can see, the pins on the triple board are marked, as are the ones on the ESP32 COU board. Please note that the pins on the CPU card are mirrored.

Left Column on triple base Left Column on CPU board (GPIO) Right Column on triple base Right Column on CPU board (GPIO)
RST (Reset) RST Tx TXD
A0 (ADC) SVP (IO36) Rx RXD
D0 IO26 D1 IO22
D5 IO18 D2 IO21
D5 IO19 D3 IO17
D7 IO23 D4 IO16
3V3 3.3V 5V VCC
In the table above, the IO numbers correspond to the GPIO numbers. As you can see, there are 10 such GPIO lines at your disposal. Most of these lines can be reallocated to different functions. They can be used as serial line (3 serial ports) as I2C ports (2 hardware interfaces) or as SPI ports or mapped to Analogue to Digital (ADC) or Digital to Analog Converters (DAC). During the course, we will see how to reprogram GPIO 36 (SVP) to use the ADC. The CPU has a user programmable LED connected to GPIO 2.

Accessing the user programmable LED

In order to understand how to access GPIO line we must look up the MicroPython documentation. Select Quick reference for the ESP32 and search for Pins and GPIO .

The schematic diagram of the LED connection looks like this:


When the output level on GPIO 2 is zero, then there is no voltage across the LED and the resistor. The LED is switched off. If GPIO 2 is at high level, then there is a voltage of 3.3V across the LED and the resistor and the LED lights up. The 330 Ω resistor limits the current flowing through the LED to 10 mA.

As you can see, MicroPython supplies a Python module named Pin, which is used to program GPIO pins. In order to control a LED the corresponding pin must be programmed to be an output line.

from machine import Pin
from time import sleep
led = Pin(2,Pin.OUT) # the led is connected to GPIO 2 and this is an output line
led.on()             # switches the LED on
sleep(2)             # keeps it on for 2 s
led.off()            # and switches it off again.

Instead of the on() and off() functions, you may also use

led.value(1)         # switches the LED on
led.value(0)         # switches it off

Reading a switch

While for the previous exercises as well as for programming the user LED we only needed the CPU card, the push button is located on a second card (sometimes also called shield). If you look at the backside of the card very, very attentively, you may see that there is a bridge from the D3 pad (GPIO 17) to the middle pad. This means that the push button is connected to GPIO 17 by default. Cutting this bridge and creating a solder bridge from D1 .. D7 to a middle pad allows reconfiguring the push button and use it on a different GPIO line

push_button.png push_button_back.png
The circuit diagram for s push button switch connection to the ESP32 GPIO line should look like this:


You can see, however, that there is no (pull-up) resistor on the pushbutton card. In fact, we can connect such a pull-up resistor by program on the ESP32 GPIO line.

Here is a script that reads the state of the pushbutton switch every 100 ms and prints the result:

from machine import Pin
from time import sleep_ms

pb = Pin(17, Pin.IN, Pin.Pin.PULL_UP)   # set the GPIO line 0 to input and add a pull-up resistor
while True:
     if pb.value() :                    # if we read a high state, the switch is not pressed
           print("Push button is released")
           print("Push button is pressed")

I think we are ready for another exercise session ExerciseSheets#ThirdSession

In the next step, we will read analogue signal levels: Analogue Signals

-- Uli Raich - 2022-10-15


Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng esp32_back.png r1 manage 165.4 K 2022-10-16 - 10:03 UliRaich  
PNGpng led.png r1 manage 5.7 K 2022-10-16 - 16:44 UliRaich  
PNGpng push_button.png r1 manage 270.2 K 2022-10-16 - 19:33 UliRaich  
PNGpng push_button_back.png r1 manage 259.9 K 2022-10-16 - 19:33 UliRaich  
PNGpng switch.png r4 r3 r2 r1 manage 6.4 K 2022-10-16 - 20:09 UliRaich  
PNGpng tripleBase.png r1 manage 196.1 K 2022-10-16 - 09:59 UliRaich  
Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2022-10-23 - UliRaich
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback