Wireless track smart car school competition | How to do a preliminary programming test on ESP32?

  • Ask/Aim:
  • Principle:
  • Practice:
  • Linkage:
  • Exercise/Experiment:

 

01 radio track


1. Problem/goal

  This ESP32 control board is the core of the wireless track car. After it is connected to the computer, you can practice MicroPython programming through a few small experiments, which can lay the foundation for the subsequent car debugging. Through this process, you can gradually become familiar with each port on the control board. Let's look at the development and testing of several basic small programs.

GM1690194710_1280_720.MPG|_-5

2. Basic principles

  To use ESP32 to control the LEDs, buttons and OLED screen on the circuit board, you need to know the corresponding ports of these peripherals. This is the definition of the external port of ESP32, LED1 is connected to IO2,. The two buttons are divided into IO25 and IO26 in different levels. Here are the ports corresponding to the five control signals of the OLED screen. Also, find their respective locations on the circuit board. Viewed from the front of the circuit board, the screen surface corresponding to LED1 is D1, and the two buttons are on the right side of the circuit board. This 7-pin socket is used to fix the OLED screen.

GM1690195236_1280_720.MPG|_-10

  According to the schematic diagram, if the LED1 port is low level, the corresponding LED is on, and when the level is high, the LED is off. The two button ports are set to high potential by two pull-up resistors. When pressed, the corresponding IO port level is low. The OLED port on the right is used to connect an external 128 x 64 LCD screen.

GM1690195429_1280_720.MPG|_-6

3. Application example

1. LED flashes

  In the given information, there is a Python program for Test LED,  

GM1690196462_1280_720.MPG|_-1

  Here is the code for the program. The modules needed after the first two lines of import in the program are the Pin module for pin control and the time module for timing. Declare an LED0 object using the Pin class, with two parameters. The first parameter indicates the serial number of the IO pin. We can know from the schematic diagram that LED1 is connected to IO2. The second pin defines the input and output properties of the pin. Here it is defined as an output pin to drive an LED. Next, use the object function value to set the state of the pin. 1 means high level, 0 means low level. The second half of the program is a while loop structure. Since the condition is always TRUE, this loop is always executed until a break instruction is encountered or an error occurs in the program. The following uses an if else statement to change the state of the LED. The value function is used to read the state of the LED, and when it is 0, it is set to 1. Otherwise set to 0,. So this operation is to change the state of the LED. Each cycle pauses for 0.5 seconds, so the LED changes state every second.

GM1690196567_1280_720.MPG|_-14
  Copy the program to the program editing window on Thonny, and start the program to run through the menu Run command.
GM1690196975_1280_720.MPG|_-2

  Once the program is running, the LED starts blinking. If you change this delay time, for example, to 0.25 seconds, reload the program and run, you can observe that the LED blinking frequency doubles.

GM1690196814_1280_720.MPG|_-8

2. Button test

  The keys on the circuit board can be used to input temporary commands, such as starting or stopping the execution of the program. These two buttons are located on the right side of the board.  

GM1690197636_1280_720.MPG|_-2

  The program is very simple. The first two sentences are still input Pin and time software modules. Next, use the Pin declaration to measure the pins. The numbers in front represent the serial numbers of the IO ports that the two buttons are connected to the ESP32, and the parameters in the back represent the properties of the pins, input ports. It is followed by a while loop body, which reads the status of the two keys through value, and uses print to display the Shell window in the Thonny development environment. If the key level is high level, return 1, otherwise return 0. According to the schematic diagram, we know that when the button is pressed, the corresponding pin is low. Through the sleep function of time, the control loop period is 1 second, so after the program runs, the button status is displayed every second.

GM1690197712_1280_720.MPG|_-10

  Copy the program to the Thonny environment and run it. You can see that the Shell window outputs a row of numbers every second. The button is not pressed, and the number is 1. When the button is pressed, the corresponding number is 0. Therefore, by judging the level of the pin, it can be known whether the corresponding button is pressed, and thus the execution flow of the program can be controlled.

GM1690197978_1280_720.MPG|_-5

3. Test OLED

  If more information needs to be displayed, it will be more convenient to use OLED. The OLED in the development kit is a 128 by 64 dot matrix OLED display, which can display characters, graphics and so on. The OLED screen has seven pins that can be inserted into sockets on the circuit board. After access, it can be tested through the program.

GM1690198377_1280_720.MPG|_-3

  Since the OLED program is relatively complicated, the following is a brief introduction to the testing steps. The program involves two files, one is the previous OLED096, and the other is the test oled file. The OLED096 file contains screen driver functions, which will be called by the test oled program. Next, both files need to be loaded into the ESP32 device. First, create a new file in Thonny letter, and copy the content of OLED096 file to the program editing interface. Then store. When saving, select the MicroPython device, and the stored file name is LQ_OLED096. Note that this name cannot be changed. If the stored procedure shows that the device is busy, you can press "STOP" to stop the ESP32 program from running. Next, create a new file, copy the TEST OLED program content into the edit box, and then execute the software. There is no error prompt in the Shell window, and the program is running normally.

GM1690198906_1280_720.MPG|_-6

  After the program runs, you can observe the test information displayed on the OLED screen, with an increasing number below it. If the OLED screen is dual-color, you can see the corresponding yellow and blue characters. So far, the OLED program test is completed. If you are interested in the corresponding program, you can study the codes in the two files by yourself.

GM1690199289_1280_720.MPG|_-3

Four. Summary

  This paper gives three basic test experiments of the control board based on ESP32. Through these experiments, everyone is familiar with the development of the MicroPython program on the development board. Lay the foundation for developing and modifying the smart car program later. It is recommended that you complete more tests by yourself on the basis of being familiar with the schematic diagram of the development board.

GM1690199769_1280_720.MPG|_-4

 

02 Lecture information


1. Lecture video

Preliminary experiments on the ESP32 control board

Test LED program

from machine import Pin
import time

LED0 = Pin(2,Pin.OUT)
LED0.value(1)

while True:
    if LED0.value() is 0:
        LED0.value(1)
    else:
        LED0.value(0)
    time.sleep(0.5)

test button program

from machine import Pin
import time

KEY0 = Pin(25, Pin.IN)     # create input pin on GPIO25
KEY1 = Pin(26, Pin.IN)     # create input pin on GPIO26

while True:
    print(KEY0.value(),"   ",KEY1.value())
    time.sleep(1)

test OLED

Test OLED program

import time
from machine import Pin,SoftSPI
from LQ_OLED096 import OLED096_SPI

LED0 = Pin(2,Pin.OUT)     #创建LED对象

spi=SoftSPI(baudrate=100000,polarity=1,phase=0,sck=Pin(18),mosi=Pin(23),miso=Pin(19)) #创建软件模拟spi
dc=Pin(15,Pin.OUT)
res=Pin(13,Pin.OUT)
cs=Pin(19,Pin.OUT)
oled=OLED096_SPI(128,64,spi,dc,res,cs)  #创建oled对象

value = 0

def Show():   #显示
    txt = ""
    oled.fill(0)   #清屏
    txt = ("    OLED_Test    ")
    oled.text(txt, 6*0, 8*0 ,1)
    txt = ("HELLO ESP32  ")
    oled.text(txt, 6*0, 8*2 ,1)
    txt = ("             ")
    oled.text(txt, 6*0, 8*3 ,1)
    txt = ("value: %d ") % (value)
    oled.text(txt, 6*2, 8*5 ,1)
    oled.show()

while True:
    value+=1
    Show()
    if LED0.value() is 0:
        LED0.value(1)
    else:
        LED0.value(0)
    time.sleep(0.5)

OLED096 program

# ssd1306.py
#MicroPython SSD1306 OLED driver, I2C and SPI interfaces created by Adafruit
 
import time
import framebuf
 
# register definitions
SET_CONTRAST        = const(0x81)
SET_ENTIRE_ON       = const(0xa4)
SET_NORM_INV        = const(0xa6)
SET_DISP            = const(0xae)
SET_MEM_ADDR        = const(0x20)
SET_COL_ADDR        = const(0x21)
SET_PAGE_ADDR       = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP       = const(0xa0)
SET_MUX_RATIO       = const(0xa8)
SET_COM_OUT_DIR     = const(0xc0)
SET_DISP_OFFSET     = const(0xd3)
SET_COM_PIN_CFG     = const(0xda)
SET_DISP_CLK_DIV    = const(0xd5)
SET_PRECHARGE       = const(0xd9)
SET_VCOM_DESEL      = const(0xdb)
SET_CHARGE_PUMP     = const(0x8d)
 
class OLED096:
    def __init__(self, width, height, external_vcc):
        self.width = width
        self.height = height
        self.external_vcc = external_vcc
        self.pages = self.height // 8
        # Note the subclass must initialize self.framebuf to a framebuffer.
        # This is necessary because the underlying data buffer is different
        # between I2C and SPI implementations (I2C needs an extra byte).
        self.poweron()
        self.init_display()
 
    def init_display(self):
        for cmd in (
            SET_DISP | 0x00, # off
            # address setting
            SET_MEM_ADDR, 0x00, # horizontal
            # resolution and layout
            SET_DISP_START_LINE | 0x00,
            SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
            SET_MUX_RATIO, self.height - 1,
            SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
            SET_DISP_OFFSET, 0x00,
            SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
            # timing and driving scheme
            SET_DISP_CLK_DIV, 0x80,
            SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
            SET_VCOM_DESEL, 0x30, # 0.83*Vcc
            # display
            SET_CONTRAST, 0xff, # maximum
            SET_ENTIRE_ON, # output follows RAM contents
            SET_NORM_INV, # not inverted
            # charge pump
            SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
            SET_DISP | 0x01): # on
            self.write_cmd(cmd)
        self.fill(0)
        self.show()
 
    def poweroff(self):
        self.write_cmd(SET_DISP | 0x00)
 
    def contrast(self, contrast):
        self.write_cmd(SET_CONTRAST)
        self.write_cmd(contrast)
 
    def invert(self, invert):
        self.write_cmd(SET_NORM_INV | (invert & 1))
 
    def show(self):
        x0 = 0
        x1 = self.width - 1
        if self.width == 64:
            # displays with width of 64 pixels are shifted by 32
            x0 += 32
            x1 += 32
        self.write_cmd(SET_COL_ADDR)
        self.write_cmd(x0)
        self.write_cmd(x1)
        self.write_cmd(SET_PAGE_ADDR)
        self.write_cmd(0)
        self.write_cmd(self.pages - 1)
        self.write_framebuf()
 
    def fill(self, col):
        self.framebuf.fill(col)
 
    def pixel(self, x, y, col):
        self.framebuf.pixel(x, y, col)
 
    def scroll(self, dx, dy):
        self.framebuf.scroll(dx, dy)
 
    def text(self, string, x, y, col=1):
        self.framebuf.text(string, x, y, col)
 
class OLED096_I2C(OLED096):
    def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
        self.i2c = i2c
        self.addr = addr
        self.temp = bytearray(2)
        # Add an extra byte to the data buffer to hold an I2C data/command byte
        # to use hardware-compatible I2C transactions.  A memoryview of the
        # buffer is used to mask this byte from the framebuffer operations
        # (without a major memory hit as memoryview doesn't copy to a separate
        # buffer).
        self.buffer = bytearray(((height // 8) * width) + 1)
        self.buffer[0] = 0x40  # Set first byte of data buffer to Co=0, D/C=1
        self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
        super().__init__(width, height, external_vcc)
 
    def write_cmd(self, cmd):
        self.temp[0] = 0x80 # Co=1, D/C#=0
        self.temp[1] = cmd
        self.i2c.writeto(self.addr, self.temp)
 
    def write_framebuf(self):
        # Blast out the frame buffer using a single I2C transaction to support
        # hardware I2C interfaces.
        self.i2c.writeto(self.addr, self.buffer)
 
    def poweron(self):
        pass
 
class OLED096_SPI(OLED096):
    def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
        self.rate = 10 * 1024 * 1024
        dc.init(dc.OUT, value=0)
        res.init(res.OUT, value=0)
        cs.init(cs.OUT, value=1)
        self.spi = spi
        self.dc = dc
        self.res = res
        self.cs = cs
        self.buffer = bytearray((height // 8) * width)
        self.framebuf = framebuf.FrameBuffer1(self.buffer, width, height)
        super().__init__(width, height, external_vcc)
 
    def write_cmd(self, cmd):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        self.cs.on()
        self.dc.off()
        self.cs.off()
        self.spi.write(bytearray([cmd]))
        self.cs.on()
 
    def write_framebuf(self):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        self.cs.on()
        self.dc.on()
        self.cs.off()
        self.spi.write(self.buffer)
        self.cs.on()
 
    def poweron(self):
        self.res.on()
        time.sleep_ms(1)
        self.res.off()
        time.sleep_ms(10)
        self.res.on()

2. Lecture information

▲ Figure 2.2.1 Preliminary experiment

▲ 图2.2.1 初步实验


■ Links to related literature:

Guess you like

Origin blog.csdn.net/zhuoqingjoking97298/article/details/131899313