member
Basics: Project 076b ESP32 Development board - MicroPython with GPIOs
of Acoptex.com in ESP8266 ESP-32
Basics: Project 076b
Project name: ESP32 Development board - MicroPython with GPIOs
Tags: EESP32 Dev Module, ESP32 development board, ESP32 Development board with WiFi and Bluetooth, ESP32-DevKitC V4 development board, ESP-WROOM-32 module with ESP32‑D0WDQ6 chip, Espressif Systems, ESP32-based development board, ESP32 modules, ESP32-WROOM-32, ESP32-WROOM-32U, ESP32-WROOM-32D, ESP32-SOLO-1, USB-UART bridge, IOT, ESP-WROOM-32 Dev Module, ESP32 DEVKITV1, ESP32 Development board, MicroPython, GPIO, interaction, read digital and analog inputs, how to control digital outputs, how to generate PWM signals
Attachments: sketch
In this project, you need these parts :
1. ESP32 development board with WiFi and Bluetooth and USB A / micro USB B cable 1 pc
2.Jumper cables F-M, M-M
3. Resistor 3 pcs (220 Ohm -2 pcs and 10 KOhm - 1pc)
4.LED 2 pcs (can be of different color)
5. Breadboard 1 pc
6. Momentary switch (Pushbutton) 1 pc
7. Potentiometer 1 pc
8. uPyCraft IDE (you can download and read more about ithere)
General
We will learn how to read digital and analog inputs, how to control digital outputs and how to generate PWM signals.
In order to program the ESP32 development board with MicroPython you need to:
- Install uPyCraft IDE in your PC;
- Upload MicroPython firmware to your ESP32 development board.
In this project you will learn how to control the ESP32 development board GPIOs with MicroPython:
- Read digital inputs;
- Control digital outputs;
- Read analog inputs;
- Generate PWM signals.
We will use the momentary switch and LED. Read the state of the momentary switch and set the LED state accordingly – when you press the momentary switch the LED will be ON, when you release the momentary switch the LED will be OFF. We will also read the voltage from a potentiometer and dim the LED accordingly to the potentiometer shaft's position (angle).
Understanding the potentiometer
See more information about potentiometer here.
Understanding the ESP32 Development board with WiFi and Bluetooth
You can read more about it here.
Signals and connections of the potentiometer
Signals and connections of the ESP32 Development board with WiFi and Bluetooth
You can find more information (datasheets, schematics, pins descriptions, functional desgn descriptions) about each board (made by Espresiff Systems) by pressing Getting started link close to each board here.
Let's check our development board - ESP32 DEVKITV1with ESP-WROOM-32 module from Espressif Systems:
Pinout diagram for the ESP Wroom 32 breakout:
ESP32-WROOM-32 - ESP32-WROOM-32 module soldered to the development board. Optionally ESP32-WROOM-32D, ESP32-WROOM-32U or ESP32-SOLO-1 module may be soldered instead of the ESP32-WROOM-32.
USB-UART Bridge - A single chip USB-UART bridge provides up to 3 Mbps transfers rates.
BOOT button - Download button: holding down the Boot button and pressing the EN button initiates the firmware download mode. Then user can download firmware through the serial port.
EN button - Reset button: pressing this button resets the system.
Micro USB Port - USB interface. It functions as the power supply for the board and the communication interface between PC and the ESP module.
TX0, TX2 - transmit pin. GPIO pin
RX0, RX2 - receive pin. GPIO pin
3V3 (or 3V or 3.3V) - power supply pin (3-3.6V).
GND - ground pin.
EN - Chip enable. Keep it on high (3.3V) for normal operation.
Vin - External power supply 5VDC.
Wiring
- Install uPyCraft IDE on your PC;
- Plug the ESP32 development board to your PC and wait for the drivers to install (or install manually any that might be required).
- Upload MicroPython firmware to your ESP32 Development board .
- Open uPyCraft IDE. Double-click uPyCraft_V1.1.exe file. A new window opens with the uPyCraft IDE software.
- Go to Tools -> Board. If you are using ESP32 development board select esp32.
- Go to Tools -> Serial and select your ESP32 Development board COM port (in our case it’s COM8). If you don’t see the COM Port, you need to install the ESP32 CP210x USB to UART Bridge VCP Drivers , check your USB cable too (it should be data cable).
- You have established a serial communication with your board. The >>> should appear in the Shell window after a successful connection with your board.
- Press New file button to create a new file.
- Press Save file button to save the file in your PC. Input file name main.py and press ok button.
- You should see the following in your uPyCraft IDE -the boot.py file in your device and a new tab with the main.py file.
- Press Download and run button to upload the file to your ESP32 Development board. The device directory should now load the main.py file. Your ESP32 Development board has two files - boot.py and main.py stored now.
- You should see a message download ok in the Shell window.
- Copy the following sketch to the Editor on the main.py file.
- Press Stop button to stop any script from running in your board.
- Press Download and Run button to upload the script to the ESP32 Development board.
- You should see a message download ok in the Shell window.
- Press the ESP32 development board on-board EN button to reboot it.
- The LED should be ON when you press the momentary switch.The LED brightness changes when you rotate the potentiometer.
Code
To interact with the GPIOs we need to import the machine module that contains classes to interact with the GPIOs. Import the Pin class to interact with the pins, the ADC class to read analog value and the PWM class to generate PWM signals: from machine import Pin, ADC, PWM
Import the sleep() method from the time module. The sleep() method allows us to add delays to the code:
from time import sleep
After importing all the necessary modules create a Pin object called led on GPIO 2 that is an OUTPUT: led = Pin(2, Pin.OUT)
The Pin object accepts the following attributes in the following order: Pin(pin number, pin mode, pull, value):
- pin number refers to the GPIO we want to control;
- pin mode can be input (IN), output (OUT) or open-drain (OPEN_DRAIN);
- pull argument is used if we want to activate a pull up or pull down internal resistor (PULL_UP, or PULL_DOWN);
- value corresponds to the GPIO state (if is is on or off): it can be 0 or 1 (True or False). Setting 1 means the GPIO is on. If we don’t have any parameter used, its state is 0 by default (that’s what we’ll do in this example).
After creating the led object we need another instance of the Pin class for the momentary switch. The momentary switch connected to GPIO 15 and it’s set an input: button = Pin(15, Pin.IN)
To create an ADC object for the potentiometer on GPIO 34 use: pot = ADC(Pin(34))
The following line applies just to the ESP32 development board. It defines that we want to be able to read voltage in full range. This means we want to read voltage from 0 to 3.3 V: pot.atten(ADC.ATTN_11DB)
The next line means we want readings with 10 bit resolution (from 0 to 1023): pot.width(ADC.WIDTH_10BIT)
The width() method accepts other parameters to set other resolutions:
- WIDTH_9BIT: range 0 to 511
- WIDTH_10BIT: range 0 to 1023
- WIDTH_11BIT: range 0 to 2047
- WIDTH_12BIT: range 0 to 4095
If you don’t set the resolution it will be 12-bit resolution by default on the ESP32 development board.
Then create a PWM object called led_pwm on GPIO 4 with 5000 Hz: led_pwm = PWM(Pin(4), 5000)
To create a PWM object, we need to pass as parameters: pin, signal’s frequency, and duty cycle.
- frequency can be a value between 0 and 78125. A frequency of 5000 Hz for an LED works just fine.
- duty cycle can be a value between 0 and 1023. In which 1023 corresponds to 100% duty cycle (full brightness), and 0 corresponds to 0% duty cycle (unlit LED).
We’ll just set the duty in the while loop, so we don’t need to pass the duty cycle parameter at the moment. If you don’t set the duty cycle when instantiating the PWM object, it will be 0 by default.
We have a while loop that is always True. This is similar to the loop() function in the Arduino IDE. We start by getting the button state and save it in the button_state variable. To get the pin state use the value() method as follows:
button_state = button.value() This returns 1 or 0 depending on whether the momentary switch is pressed or not.
To set the pin state, use the value(state) method in the Pin object. In this case we’re setting the button_state variable as an argument. This way the LED turns on when we press the momentary switch: led.value(button_state)
To read an analog input use the read() method on an ADC object (in this case the ADC object is called pot): pot_value = pot.read()
To control the duty cycle, use the duty() method on the PWM object (led_pwm). The duty() method accepts a value between 0 and 1023 (in which 0 corresponds to 0% duty cycle, and 1023 to 100% duty cycle). So, pass as argument the pot_value (that varies between 0 and 1023). This way we change the duty cycle by rotating the potentiometer: led_pwm.duty(pot_value)
Summary
We have learnt to read digital and analog inputs, how to control digital outputs and how to generate PWM signals.
Libraries
- No libraries required
Sketch
- See attachments on the begining of this project
Other projects of Acoptex.com










Viewed: 2377 times