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 rThe back of the ESP32 CPU board
tripleBase.png esp32_back.png

As you can see, the pins on the triple board are marked

Left Column on triple base Left Column on CPU board (GPIO) Right Column on triple base Right Column on CPU board (GPIO)Sorted descending
3V3 3.3V 5V VCC
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
D8 IO5 GND GND
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 .

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

-- Uli Raich - 2022-10-15

Comments

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 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: r2 - 2022-10-16 - UliRaich
 
  • Edit
  • Attach
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