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):
As you can see, the pins on the triple board are marked
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