Tutorial Display

From Sketching with Hardware at LMU Wiki
Revision as of 13:28, 31 August 2020 by Skwhadmin (talk | contribs) (→‎Related Components)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Control the OLED Display on the ESP32[edit]

In this part of the tutorial, we explain how to control the display that is included on the ESP32 web kit module. The display is connected over I2C and we use the microPython module ssd1306.py as library.

Success criteria[edit]

  • you can write a string to the display
  • you can converts numbers to strings and write them to the display
  • you can clear the display or fill it
  • you can position text at different positions on the display

Required Module and Files[edit]

Related Components[edit]

The components are related to the LMUBox. For more components, see the Hardware List. Many of the pages on actuators and sensor include additional examples.

Microcontroller[edit]

Actuators[edit]

Instructional Videos[edit]

Writing to the OLED Display over I2C[edit]

In this video on youtube (22:00) we show how to write text to the OLED Display on the ESP32 module. We then show how to connect a poti to an analog input and display the value in a loop on the display: https://youtu.be/UbxwePvgX-U

Another Tutorial (in German) on writing to the OLED Display with some background[edit]

There is a tutorial with several parts at "the Die Hobbyelektroniker - Community" on Micropython with ESP32 https://community.hobbyelektroniker.ch/wbb/index.php?board/51-lektion-5-das-display/

Using the uPyCraft IDE to upload files[edit]

In this video on youtube (6:48) we show how to upload an run python files with the uPyCraft IDE: https://youtu.be/MXfj_gWf0z4

Reading analog inputs[edit]

In this video on youtube (46:23 ) we show how to read an analog input and how to configure the ADC (there is also information on how to connect a resistor and about voltage dividers): https://youtu.be/gjj5KyK2qGI

Code Examples[edit]

Code Example: displaytest.py[edit]

 1 from machine import I2C, Pin 
 2 import ssd1306
 3 
 4 # ESP32 reset pin for display must be 1 - this is pin16 
 5 # should be done in ssd1306.py - if not uncommend the next 2 lines
 6 #pin16 = Pin(16, Pin.OUT)
 7 #pin16.value(1)
 8 
 9 
10 # Pins according the schematic https://heltec.org/project/wifi-kit-32/
11 i2c = I2C(-1, scl=Pin(15), sda=Pin(4))
12 
13 #display size
14 oled_width = 128
15 oled_height = 64
16 oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
17 
18 #test text
19 oled.text('Hello, World', 0, 0)
20 oled.text('some more text!?', 0, 10)
21 oled.text('...even more :-)', 0, 20)
22 
23 #show is required - otherwise you will not see it
24 oled.show()

Code Example: adc2disp.py[edit]

 1 # test program to read in a analog value on Pin 32 on the ESP32 board
 2 # and display it on the 0.96" OLED Display (Heltec ESP32 Web Kit)
 3 
 4 from machine import ADC
 5 from time import sleep
 6 from machine import I2C, Pin 
 7 import ssd1306
 8 
 9 # ESP32 reset pin for display must be 1 - this is pin16 
10 # should be done in ssd1306.py - if not uncommend the next 2 lines
11 #pin16 = Pin(16, Pin.OUT)
12 #pin16.value(1)
13 
14 # this is for ESP32
15 # Pins according the schematic https://heltec.org/project/wifi-kit-32/
16 i2c = I2C(-1, scl=Pin(15), sda=Pin(4))
17 
18 # for ESP8266 it should be:
19 # i2c = I2C(-1, scl=Pin(5), sda=Pin(4))
20  
21 
22 #display size
23 oled_width = 128
24 oled_height = 64
25 oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
26 
27 # for ESP32 setup the ADC on Pin GPIO32
28 adc = ADC(Pin(32)) 
29 adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
30 
31 # for ESP8266 setup the ADC(0)
32 #adc = ADC(0) 
33 
34 while True:
35   # read in analog value in v
36   v = adc.read()
37   # print to serial line
38   print(v)
39   # empty display
40   oled.fill(0)
41   oled.show()
42   # write v converted to a string onto the display at (0,0)
43   oled.text(str(v), 0, 0)
44   oled.show()
45   sleep(1)