End Presentation


TWiki Slide Show
Next
Setting up and IoT

Session 2: Micropython and
Hardware access

Uli Raich
Formally CERN, Geneva, Switzerland

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 1 of 35





















TWiki Slide Show
Next
The WeMos D1 mini CPU card
esp8266.png wemosEsp32.png

ESP8266

cost: 2.21 Euros

ESP32

cost: 4.1o Euros

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 2 of 35





















TWiki Slide Show
Next
CPU Pinout
esp8266Pinout.png esp32Pinout-v2.png
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

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 3 of 35





















TWiki Slide Show
Next
CPU pinouts for reference
cpuPinouts.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 4 of 35





















TWiki Slide Show
Next
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
esp8266PinMeaning.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 5 of 35





















TWiki Slide Show
Next
Meaning of pins on ESP32
esp32-pinout-chip-ESP-WROOM-32.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 6 of 35





















TWiki Slide Show
Next
How to program the processor
esp8266Programming.png Several development tools are available:
  • ESP_IDF: The official Espressif development tool.
    Includes a gcc compiler for the ESP8266 and the ESP32

Please see
https://docs.espressif.com/projects/esp-idf/en/latest/

  • The Arduino IDENeeds extensions for these processors
    and the different CPU boards

see:
https://www.arduino.cc/en/Main/Software

  • MicroPython

see:
https://micropython-docs-esp32.readthedocs.io/en/esp32_doc/

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 7 of 35





















TWiki Slide Show
Next
Flashing the code
esptool.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 8 of 35





















TWiki Slide Show
Next
esptool
  • 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 when the Micropython IDE 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.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 9 of 35





















TWiki Slide Show
Next
How to write a Micropython program?
First we need a Micropython interpreter!

You find the sources here:

https://github.com/micropython/micropython/

In the repository you find ports for the ESP8266 and the ESP32.

In order to compile the code you need the ESP-IDF and its cross compilers

The code compiles into a binary file (firmware-combined.bin) which contains a boot loader and the interpreter.

This binary must be uploaded and flashed.

For documentation of the ESP8266 port of Micropython look at

https://docs.micropython.org/en/latest/esp8266/tutorial/index.html

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 10 of 35





















TWiki Slide Show
Next
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.

lsusb.png

You see the command prompt and you can interact with Micropython. But … how to upload scripts?

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 11 of 35





















TWiki Slide Show
Next
What is REPL?

repl.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 12 of 35





















TWiki Slide Show
Next
The communication tools: minicom
minicom.png

You see the command prompt and you can interact with Micropython.

But … how to upload scripts?

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 13 of 35





















TWiki Slide Show
Next
The command line tool ampy
ampy.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 14 of 35





















TWiki Slide Show
Next
IDE for Micropython: uPyCraft
uPyCraft.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 15 of 35





















TWiki Slide Show
Next
uPyCraft
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

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 16 of 35





















TWiki Slide Show
Next
Flashing Micropython
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.

updateFirmware.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 17 of 35





















TWiki Slide Show
Next
uPyCraft(2)
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

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

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 18 of 35





















TWiki Slide Show
Next
Thonny
thonny.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 19 of 35





















TWiki Slide Show
Next
Thonny (2)
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.

thonny_uP.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 20 of 35





















TWiki Slide Show
Next
!IoT Hello World program
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.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 21 of 35





















TWiki Slide Show
Next
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

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 22 of 35





















TWiki Slide Show
Next
Micropython hardware functions
hardwareFunctions.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 23 of 35





















TWiki Slide Show
Next
The machine.Pin class
pinClass.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 24 of 35





















TWiki Slide Show
Next
Switch the LED on, version 1
ledControl1.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 25 of 35





















TWiki Slide Show
Next
Switch the LED on, version 2
ledControl2.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 26 of 35





















TWiki Slide Show
Next
The blinking LED
Now we put the code into a script and run it

blinkingLed.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 27 of 35





















TWiki Slide Show
Next
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.

pwm.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 28 of 35





















TWiki Slide Show
Next
PWM in Micropython
pwm_uP.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 29 of 35





















TWiki Slide Show
Next
Our PWM implementation
pwmImpl.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 30 of 35





















TWiki Slide Show
Next
The WS2812B LED
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

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 31 of 35





















TWiki Slide Show
Next
WS2812B timing
For all the details on the ws2812b look at

https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf

24 bit of colour data puts 2e24 colours at your disposal

Coding of a single bit

ws2812Bits.png

The control word:

ws2812ControlWord.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 32 of 35





















TWiki Slide Show
Next
Cascading the WS2812B

ws2812Chain.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 33 of 35





















TWiki Slide Show
Next
Using the neopixel library
neopixel.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 34 of 35





















TWiki Slide Show
Next
… and our code
We have a single neopixel connected to

GPIO pin 4 (ESP8266)

or

GPIO pin 21 (ESP32)

This code works on both CPUs!

neopixelCode.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 35 of 35





















First slide Previous End Presentation






























Uli Raich - 2019-05-13

Comments

I Attachment History Action SizeSorted ascending Date Who Comment
PNGpng ws2812ControlWord.png r1 manage 7.6 K 2019-05-14 - 12:42 UliRaich  
PNGpng ws2812Bits.png r1 manage 11.9 K 2019-05-14 - 12:42 UliRaich  
PNGpng minicom.png r1 manage 17.2 K 2019-05-14 - 11:42 UliRaich  
PNGpng ledControl1.png r1 manage 19.8 K 2019-05-14 - 12:17 UliRaich  
PNGpng ledControl2.png r1 manage 19.8 K 2019-05-14 - 12:18 UliRaich  
PNGpng pinControl2.png r1 manage 19.8 K 2019-05-14 - 12:18 UliRaich  
PNGpng ws2812Chain.png r1 manage 19.9 K 2019-05-14 - 12:42 UliRaich  
PNGpng repl.png r1 manage 20.2 K 2019-05-14 - 11:45 UliRaich  
PNGpng pwm_uP.png r1 manage 24.6 K 2019-05-14 - 12:31 UliRaich  
PNGpng updateFirmware.png r1 manage 27.3 K 2019-05-14 - 11:59 UliRaich  
PNGpng thonny_uP.png r1 manage 31.0 K 2019-05-14 - 12:05 UliRaich  
PNGpng cpuPinouts.png r1 manage 34.1 K 2019-05-13 - 20:32 UliRaich  
PNGpng pwm.png r1 manage 51.4 K 2019-05-14 - 12:27 UliRaich  
PNGpng lsusb.png r1 manage 73.3 K 2019-05-14 - 11:48 UliRaich  
PNGpng blinkingLed.png r1 manage 79.9 K 2019-05-14 - 12:17 UliRaich  
PNGpng neopixelCode.png r1 manage 79.9 K 2019-05-14 - 12:46 UliRaich  
PNGpng ampy.png r1 manage 100.7 K 2019-05-14 - 11:42 UliRaich  
PNGpng esp8266Programming.png r1 manage 108.3 K 2019-05-14 - 09:59 UliRaich  
PNGpng esptool.png r1 manage 114.3 K 2019-05-14 - 11:28 UliRaich  
PNGpng esp8266.png r1 manage 116.1 K 2019-05-13 - 20:27 UliRaich  
PNGpng pwmImpl.png r1 manage 116.7 K 2019-05-14 - 12:31 UliRaich  
PNGpng esp32-pinout-chip-ESP-WROOM-32.png r1 manage 137.4 K 2019-05-14 - 09:50 UliRaich  
PNGpng thonny.png r1 manage 148.1 K 2019-05-14 - 12:05 UliRaich  
PNGpng esp32Pinout-v2.png r1 manage 151.2 K 2019-05-14 - 09:01 UliRaich  
PNGpng esp8266PinMeaning.png r2 r1 manage 155.3 K 2019-05-14 - 09:43 UliRaich  
PNGpng hardwareFunctions.png r1 manage 159.5 K 2019-05-14 - 12:17 UliRaich  
PNGpng pinClass.png r1 manage 160.1 K 2019-05-14 - 12:20 UliRaich  
PNGpng uPyCraft.png r1 manage 164.1 K 2019-05-14 - 11:43 UliRaich  
PNGpng neopixel.png r1 manage 165.5 K 2019-05-14 - 12:46 UliRaich  
PNGpng esp8266Pinout.png r1 manage 176.4 K 2019-05-13 - 20:41 UliRaich  
PNGpng wemosEsp32.png r1 manage 213.3 K 2019-05-13 - 20:29 UliRaich  
Unknown file formatodp afnog19-ws-session2.odp r1 manage 6431.5 K 2019-05-14 - 09:01 UliRaich  

This topic: AFNOG > WebHome > AFNOGWorkshop2019 > AFNOG-2019Slides > WorkshopSlides > HardwareAccessAndMicropython
Topic revision: r4 - 2019-05-14 - UliRaich
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