ESP32 In MicroPython: GPIO
Written by Harry Fairhead & Mike James   
Monday, 18 December 2023
Article Index
ESP32 In MicroPython: GPIO
Basic GPIO Functions

Basic GPIO Functions

The MicroPython Pin class allows you to create an object which controls the way a single GPIO line works. The simplest form of the constructor is:

Pin(id, mode = mode)

where id is the number of the GPIO line you want to use and mode is Pin.IN or Pin.OUT. There are other possibilities and these are discussed later.

Notice that id is the GPIO number and not the hardware pin number. For example, 5 means GPIO5 and not “connector pin 5” but connector J3 pin 10.

There is also an init method which can be used to change the configuration of the pin. For example, pin.init(mode = Pin.IN) might be used to change a pin from output to input.

A number of methods are provided to work with the state of a Pin object:

Method

Description

value(x) 

Sets the line to x, usually 0 or 1, but x can be anything that evaluates to true or false

on() 

Sets the line to high

off()

Sets the line to low

There are a number of other methods and properties, but these are the most basic. You can discover the current state of a GPIO line using state = pin.value() and you can set the initial state of a GPIO output using value = in the constructor or in the init method.

Blinky

By tradition, the first IoT program you write is Blinky which flashes an LED. A program to flash an LED uses a general I/O line and an external LED. Some development boards have an LED already connected to GPIO2. With this in mind, let’s flash an LED connected to GPIO2 which will either use the onboard LED or an external LED you have connected.

Enter the program:

from machine import Pin
import time
pin = Pin(2, Pin.OUT)
while True:
    pin.value(1)
    time.sleep(1)
    pin.value(0)
    time.sleep(1)

The program doesn’t use any constants in order to make what is happening clearer. It first initializes GPIO2 to be an output and sets it repeatedly high and low with a pause of one second in between.

If the board you are using has an LED connected to GPIO2 you will see it flashing. If not and you want to connect an LED to see the "blinking" for real then this is easy enough, but you do need a current-limiting resistor to avoid the LED drawing more current than the GPIO line can supply and possibly damaging the chip. A 200Ω resistor is a good choice, see Chapter 5, where a better way to drive an LED is also discussed.

esp32LED

How you build the circuit is up to you. You can use a prototyping board or just a pair of jumper wires. The short pin and/or the flat on the side of the case marks the negative connection on the LED – the one that goes to ground.


If you can't be bothered to go though the ritual of testing "Blinky" with a real LED, then just connect a logic analyzer to J3 Pin 15 and you will see pulses at 1-second intervals.  

ledlogic

Adding A Function Toggle

The ESP32 doesn’t support a toggle function, but it is easy to add one:

def toggle(pin):
    temp = pin.value()
    temp = not temp
    pin.value(temp)

This sets a line high it if it is low and low if it is high. This form of the function shows how it works, but in practice you would probably write it in a more compact form:

def toggle(pin):
    pin.value(not pin.value())

With toggle it is even easier to implement Blinky:

from machine import Pin
import time
pin = Pin(2,Pin.OUT)
while True:
    toggle(pin)
    time.sleep(1) 

 esp32LED

Summary

  • The ESP has 34 GPIO lines in total, but some are already used by the development board.

  • The pin numbering used isn’t standardized, but using the GPIO numbers that the ESP32 module uses is a safe option.

  • The GPIO lines are organized in four groups, GPIO0 to GPIO19, GPIO21 to GPIO23, GPIO25 to GPIO27 and GPIO32 to GPIO39.

  • Pins GPIO34 to GPIO39 are input only.

  • The ESP32 is a 3.3V device and the GPIO lines should not be used at a higher voltage.

  • MicroPython provides the Pin class to control a single GPIO line and its basic methods let you set the line high or low and to discover what it is currently set to.

  • A Blinky program is the usually the first IoT program you write on a new machine. For the ESP32 you can easily arrange to make an externally connected LED blink on and off.

  • An externally connected LED needs a current limiting resistor.

 

Programming the ESP32in MicroPython

By Harry Fairhead & Mike James

esppython360

Buy from Amazon.

Contents

       Preface

  1. The ESP32 – Before We Begin
  2. Getting Started
  3. Getting Started With The GPIO 
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. Advanced Input – Interrupts
  8. Pulse Width Modulation
       Extract:
    PWM And The Duty Cycle
  9. Controlling Motors And Servos
  10. Getting Started With The SPI Bus
  11. Using Analog Sensors
       Extract:
    Analog Input
  12. Using The I2C Bus
       Extract
    : I2C, HTU21D And Slow Reading 
  13. One-Wire Protocols
  14. The Serial Port
  15. Using WiFi
     Extract:
    WiFi 
  16. Sockets
     Extract:
    Client Sockets
     Extract:
    SSL Client Sockets***NEW!
  17. Asyncio And Servers
  18. Direct To The Hardware
     Extract:
    Using Hardware Registers 

<ASIN:187196282X>

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner



Last Updated ( Tuesday, 19 December 2023 )