Session 2: Micropython and
Hardware access
Uli Raich
Formally CERN, Geneva, Switzerland
COPYRIGHT © 2024 by the contributing authors
Slide 1 of 64
The WeMos D1 mini CPU card
|
|
ESP8266 cost: 2.21 Euros |
ESP32 cost: 4.1o Euros |
COPYRIGHT © 2024 by the contributing authors
Slide 2 of 64
|
|
ESP8266 |
ESP32 |
Please note: The pin numbers IOxx on the ESP32 correspond
to the GPIO pin numbers
The pins in the white fields go to the WeMos D1 mini bus
and are also available on the ESP8266
The other pins are only accessible on the ESP32 CPU card
The pin numbers Dx do not correspond to GPIO pin numbers!
Please refer to the next slide for correspondence
COPYRIGHT © 2024 by the contributing authors
Slide 3 of 64
CPU pinouts for reference
COPYRIGHT © 2024 by the contributing authors
Slide 4 of 64
Meaning of pins on ESP8266
- GPIO: General Purpose Input Output.
Drives a single digital line which can be programmed input or output - SCL/SDA: The I2C bus:a 2 wire bus
interfacing sensors or actuators to the CPU
- SCL,MISO,MOSI,SS: SPI
the Serial Peripheral Interface. Used fast communication with external device |
|
COPYRIGHT © 2024 by the contributing authors
Slide 5 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 6 of 64
How to program the processor
COPYRIGHT © 2024 by the contributing authors
Slide 7 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 8 of 64
- esptool is called from the Makefiles in ESP-IDF
- esptool is used when we upload code from the Arduino IDE to the processor flash
- esptool is used with Micropython IDE on uPyCraft
installs Micropython onto the processor flash
In the above cases the use of esptool is hidden to us.
We can however also execute esptool directly.
COPYRIGHT © 2024 by the contributing authors
Slide 9 of 64
How to write a Micropython program?
COPYRIGHT © 2024 by the contributing authors
Slide 10 of 64
How to communicate with the Micropython interpreter?
We use a serial connection passing through the micro USB connection.
As soon as we connect the processor card to the PC we see the
UART bridge and a new device: dev/ttyUSB0 is created.
This device is used to communicate with the Micropython REPL.
You see the command prompt and you can interact with Micropython. But … how to upload scripts?
COPYRIGHT © 2024 by the contributing authors
Slide 11 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 12 of 64
The communication tools: minicom
You see the command prompt and you can interact with Micropython.
But … how to upload scripts?
COPYRIGHT © 2024 by the contributing authors
Slide 13 of 64
The command line tool ampy
COPYRIGHT © 2024 by the contributing authors
Slide 14 of 64
IDE for Micropython: uPyCraft
COPYRIGHT © 2024 by the contributing authors
Slide 15 of 64
uPyCraft is a rather complete Integrate Development Environment (IDE)
which lets you
- Access the REPL
- Create directories on the Micropython file system
- Upload scripts
- Syntax check scripts
- Run scripts
- Install Micropython on your processor board
COPYRIGHT © 2024 by the contributing authors
Slide 16 of 64
This has already been done for you! However, it is easy if you want to do it
at home with a new processor board.
Compiling a new version of Micropython is substantially harder but also perfectly possible.
COPYRIGHT © 2024 by the contributing authors
Slide 17 of 64
uPyCraft is based on QT4 and is available for Linux, Windows and Mac.
It is written in PyQt4 the Python language binding to Qt4.
The Linux version did not work for me when running Ubuntu 18.04 or later.
I found a version based on PyQt5 (new version of QT) which was even worse.
I tried to correct as much as I could to make the PyQt5 version usable on Linux:
https://github.com/uraich/uPyCraft-Qt5
COPYRIGHT © 2024 by the contributing authors
Slide 18 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 19 of 64
Thonny is an IDE for Python which has provisions for Micropython.
Under Tools → Options button you can select the type of
Python interpreter you intend to use.
COPYRIGHT © 2024 by the contributing authors
Slide 20 of 64
A “Hello World” program, just printing “Hello World” on the screen
does not look very exciting.
However, this is generally used to verify that the infrastructure
Compiler, linker, downloader, flash program
are working correctly
In embedded systems printing can be quite complex
and a blinking LED is used instead.
COPYRIGHT © 2024 by the contributing authors
Slide 21 of 64
Switching on and off a LED
The ESP8266 and the ESP32 have a “user LED” connected to GPIO 2.
How do we control this LED?
- Define that the LED is connected to GPIO 2
- Program this pin as output
- Write a logic 1 to the pin to switch it on
- Write a logic 0 to the pin to switch it off
- The logic state may be inverted if the LED is active low
COPYRIGHT © 2024 by the contributing authors
Slide 22 of 64
Micropython hardware functions
COPYRIGHT © 2024 by the contributing authors
Slide 23 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 24 of 64
Switch the LED on, version 1
COPYRIGHT © 2024 by the contributing authors
Slide 25 of 64
Switch the LED on, version 2
COPYRIGHT © 2024 by the contributing authors
Slide 26 of 64
Now we put the code into a script and run it
COPYRIGHT © 2024 by the contributing authors
Slide 27 of 64
Changing the light intensity
The LED is connected to a digital line which can only be set to 0 or Vcc.
How can we change the light intensity and dim the LED?
The light intensity depends on the average current flowing through the LED.
The answer is PWM: pulse width modulation.
COPYRIGHT © 2024 by the contributing authors
Slide 28 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 29 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 30 of 64
A more complex LED:
- rgb LED used in LED chains.
- each ws18b12 contains the 3 colored LEDs and a controller.
- Can be cascaded and individually addressed, depending on its position in the chain
- Needs precise timing
- To use it we pass through the neopixel library built into micropython
COPYRIGHT © 2024 by the contributing authors
Slide 31 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 32 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 33 of 64
Using the neopixel library
COPYRIGHT © 2024 by the contributing authors
Slide 34 of 64
We have a single neopixel connected to
GPIO pin 4 (ESP8266)
or
GPIO pin 21 (ESP32)
This code works on both CPUs!
COPYRIGHT © 2024 by the contributing authors
Slide 35 of 64
I2C stands for
Inter-
Integrated-
Circuit. It was invented by Philips Semiconductor
in 1982. Slow, short distance.
Quite a number of sensors in the "WeMos D1 sensor shield collection use the I2C bus
- SHT30 temperature and humidity sensor
- DS1307 real time clock
- The BMP180 barometric pressure and temperature sensor
- The SSD1306 OLED (Organic Light Emitting Diode) display
COPYRIGHT © 2024 by the contributing authors
Slide 36 of 64
The CPU (master) connects to the sensors (slaves) through
2 digital lines:
- SCL: the clock
- SDA: the data
COPYRIGHT © 2024 by the contributing authors
Slide 37 of 64
!I2C start and stop sequence
When the master want to talk to the slave it issues a
start sequenceIt terminates the transfer with a
stop sequence
COPYRIGHT © 2024 by the contributing authors
Slide 38 of 64
When talking to a slave the master sends a seven bit address
followed by a read/write bit
This allows to access at most 128 devices
COPYRIGHT © 2024 by the contributing authors
Slide 39 of 64
Data is transmitted 8 bits at a time followed by an acknowledge bit.
If acknowledge is low, transfer ok, otherwise: send stop sequence
COPYRIGHT © 2024 by the contributing authors
Slide 40 of 64
- Send start sequence
- Send I2C address and R/W bit low
- Send internal register number
- Send data byte
- Optionally send further data bytes
- Send stop sequence
COPYRIGHT © 2024 by the contributing authors
Slide 41 of 64
- Send start sequence
- Send slave address with R/W low
- Send address of internal register
- Send a start sequence again
- Send slave address with R/W high
- Read data byte
- Send stop sequence
COPYRIGHT © 2024 by the contributing authors
Slide 42 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 43 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 44 of 64
The SHT30 digital temperature and relative humidity sensor
The SHT30 is a digital temperature and humidity sensor based on the I2C bus
Here is its
data sheet.
Temperature precision: +- 0.3 °C
Relative humidity: +- 3 %
Works on 2.4V – 5.5 V
COPYRIGHT © 2024 by the contributing authors
Slide 45 of 64
A look at the SHT30 driver
COPYRIGHT © 2024 by the contributing authors
Slide 46 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 47 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 48 of 64
Where to find the demo code
While for the course we only use the CPU and 2 sensor shields:
- ESP8266 CPU (a more powerful ESP32 CPU exists)
- SHT30 temperature and humidity sensor
- Ws2812B rgb LED (neopixel) there are many more available on the market.
We have a dozen such shields here for demo.
All demo programs can be found at:
https://github.com/uraich/MicroPython_IoTDemos
COPYRIGHT © 2024 by the contributing authors
Slide 49 of 64
!WeMos D1 mini sensor and actuator shields (1)
COPYRIGHT © 2024 by the contributing authors
Slide 50 of 64
Wemos D1 mini sensor and actuator shields (2)
COPYRIGHT © 2024 by the contributing authors
Slide 51 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 52 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 53 of 64
Documentation for the demo programs
COPYRIGHT © 2024 by the contributing authors
Slide 54 of 64
The data logger features a DS1307 Real Time Clock (RTC) backed up by a battery
An SD card socket also provided. This allows to store large amounts of data locally
The RTC keeps the time and measurements can be supplied with a time tag
Programs to set and read the RTC are provided.
One of the programs setting the RTC gets the time from and NTP server
such that manual specification of the current date and time are not needed. |
|
https://github.com/uraich/MicroPython_IoTDemos/tree/master/drivers/ds1307
COPYRIGHT © 2024 by the contributing authors
Slide 55 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 56 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 57 of 64
SSD1306 48x64 OLED display
COPYRIGHT © 2024 by the contributing authors
Slide 58 of 64
|
|
The DHT11 is a digital temperature and relative humidity sensor. It uses a proprietary protocol of communication with its controlling host implemented in the dht.DHT11 class |
COPYRIGHT © 2024 by the contributing authors
Slide 59 of 64
The LED matrix has 8x8 LEDs on it, which can individually be switched on or off.
In addition to the mled class which takes care of the communication between the host and the device and which has classes to clear the display, set a pixel on or off and to change the brightness, I wrote a class matrix which takes a number 0..64 and lights this number of LEDs starting from bottom left |
|
COPYRIGHT © 2024 by the contributing authors
Slide 60 of 64
This shield implements a passive buzzer.
A passive buzzer takes a frequency(an active buzzer takes a signal level) and produces a sound at this frequency. This means that the frequency can be changed and a tune can be played. In
https://github.com/uraich/MicroPython_IoTDemos/tree/master/drivers/buzzer
you will find a program interpreting songs on RTTTL (Ring Tone Text Transfer Language) and playing these on the buzzer
A small song library is also provided |
|
COPYRIGHT © 2024 by the contributing authors
Slide 61 of 64
This is a home build module featuring a photo-resistor measuring the light intensity impinging on it.
It provides an analogue value converted to digital by the ADC on the WeMos D1 CPU
Please note that the ESP8266 has a single 10 bit ADC while the ESP32 has 3 12 bit ADCs. A MUX provides up to 18 analogue channels The light intensity can be changed with an LED |
|
COPYRIGHT © 2024 by the contributing authors
Slide 62 of 64
This module allows to easily stack a rather larger number of shields to a sandwich.
Make sure however that the GPIO lines used by the modules do not clash
COPYRIGHT © 2024 by the contributing authors
Slide 63 of 64
COPYRIGHT © 2024 by the contributing authors
Slide 64 of 64
Uli Raich - 2019-05-13
Comments
This topic: AFNOG
> WebHome >
AFNOGWorkshop2019 >
AFNOG-2019Slides >
WorkshopSlides > HardwareAccessAndMicropython
Topic revision: r7 - 2019-06-11 - UliRaich
Copyright © 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