GPIO programming on ReSpeaker

ReSpeaker has one MCU MT7688 and co MCU ATmega32U4, so we can access both MCUs from our program. Inside the board, ReSpeaker runs Linux OpenWrt, so we can perform Linux operations on the ReSpeaker terminal. Not all GPIO pins are accessible from a program. ReSpeaker exposes specific GPIOs.

In general, we can use the following GPIO layout of the ReSpeaker board:

You can see here that some GPIO pins belong to MCU MT7688 and MCU ATmega32U4. If you want to know the complete schematic of the ReSpeaker core board, I recommend you read this document at https://github.com/respeaker/get_started_with_respeaker/blob/master/Introduction.md#hardware.

To access GPIO pins on MCU ATmega32U4, you can use Arduino software. We can write a sketch for Arduino program in the MCU ATmega32U4.

For MCU MT7688, we can access GPIO using GPIO programming for Linux since ReSpeaker uses Linux OpenWrt.

For testing, we'll connect an LED to GPIO on MCU MT7688. You can connect it on MT_GPIO18/PWM_CH0. You can see my wiring here:

Let's start writing a program using GPIO with the Linux approach. Open the ReSpeaker terminal. Since we use GPIO18 on MCU MT7688, we activate it with the output direction. Type these commands:

    $ echo 18 > /sys/class/gpio/export
    $ echo "out" > /sys/class/gpio/gpio18/direction  

In this case, GPIO18 is activated for output mode.

Now you can set values for HIGH and LOW as 1 and 0. You can type these commands on the ReSpeaker terminal:

    $ echo 1 > /sys/class/gpio/gpio18/value
    $ echo 0 > /sys/class/gpio/gpio18/value  

You should see the LEDs light up.

If you don't need this GPIO anymore, you can release it so other programs can access it. Type this command on the ReSpeaker terminal:

    $ echo 18> /sys/class/gpio/unexport  

You can see my program output on the ReSpeaker terminal here:

We also can develop a Python program to access GPIO on MT7688. In general, ReSpeaker already provides an installed Python library for MT7688. You read about it at https://github.com/respeaker/respeaker_python_library.

Extending our previous demo, we'll try to turn on/off LEDs on GPIO18 using Python. Type this script:

from respeaker import gpio 
 
gpio18 = gpio.Gpio(18, gpio.DIR_OUT) 
# turn on LED 
gpio18.write(1) 
# turn off LED 
gpio18.write(0) 
 
# close gpio 
gpio18.close() 

Save it as ch05_respeaker_gpio.py. You can run this program by typing this command on the ReSpeaker terminal:

    $ python ch05_respeaker_gpio.py  

Now you can see LEDs turning on and off.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.239.110