create new tag
view all tags

Start Presentation

Lecture 2: LEDs and NeoPixel

CSC 321: Embedded Sysytem

First Semester 2020/2021

Slide 1: The ESP32-WROVER-B MCU

esp32board.jpg esp32boardlab.jpeg

Slide 2: CPU Pinout


Please note: The pin numbers IOxx on the ESP32 correspond to the GPIO pin numbers

Datasheet: https:// www.espressif.com /sites/default/files/documentation/esp32-wrover-b_datasheet_en.pdf

Slide 3: Meaning of pins on ESP32

pinout.jpg pinout3.png

Slide 4: How to program the processor

connect.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:

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



  • MicroPython


Slide 5: Flashing the code


Slide 6: 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 with Micropython IDE on uPyCraft/Thonny

    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.

Slide 7: How to write a Micropython program?

First we need a Micropython interpreter!

You find the sources here:


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 ESP32port of Micropython look at


Slide 8: 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 serial port to connect for communication


Slide 9: What is REPL?


Slide 10: The communication tools: minicom


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

But … how to upload scripts?

Slide 11: The command line tool ampy


Slide 12: IDE for Micropython: uPyCraft


Slide 13: Thonny


Slide 14: Thonny

Thonny is a 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

Slide 15: Thonny

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.


Slide 16: Flashing Micropython

Compiling a new version of Micropython is substantially much easier with Thonny.

You still need to install esptool first. You just select your port and firmware then install.


Slide 17: 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.

Slide 18: Switching on and off a LED

The ESP32-WROVER-B have a “user LED” connected to GPIO 19.

How do we control this LED?

  • Define that the LED is connected to GPIO 19

  • 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

Slide 19: Micropython hardware functions


Slide 20: The machine.Pin class


Slide 21: Switch the LED on, version 1


Slide 22: Switch the LED on, version 2


Slide 23: The blinking LED

Now we put the code into a script and run it


Slide 24: 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.


Slide 25: PWM in Micropython


Slide 26: Our PWM implementation


Slide 27: 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

Slide 28: WS2812B timing

For all the details on the ws2812b look at


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

Coding of a single bit


The control word:


Slide 29: Cascading the WS2812B


Slide 30: Using the neopixel library


Slide 31: … and our code

We have a single neopixel connected to

GPIO pin 4 (ESP8266)


GPIO pin 21 (ESP32)

This code works on both CPUs!


-- Isaac Armah-Mensah - 2021-02-28


Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng MPY1.png r1 manage 423.0 K 2021-03-02 - 08:20 IsaacArmahMensah  
JPEGjpg esp32board.jpg r1 manage 279.8 K 2021-03-02 - 00:20 IsaacArmahMensah  
PNGpng esp32board.png r1 manage 2095.9 K 2021-02-28 - 01:28 IsaacArmahMensah  
PNGpng esptool.png r2 r1 manage 385.0 K 2021-02-28 - 16:01 IsaacArmahMensah  
PNGpng interpret.png r1 manage 105.0 K 2021-03-02 - 00:42 IsaacArmahMensah  
Edit | Attach | Watch | Print version | History: r7 < r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r7 - 2021-03-09 - 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