-
Notifications
You must be signed in to change notification settings - Fork 83
Using Arduino to program ATmega32U4 on ReSpeaker
This tutorial describes how to use Arduino with Light (RGB LEDs) and Touch (capacitive touch sensors) and how to communicate between ATmega32U4 and MT7688. On ReSpeaker, ATmega32U4 is in charge of the LEDs control and touch buttons detect while MT7688 is used for speech recognition, Wi-Fi communication and audio input/output. And there are 2 communication ways between ATmega32U4 and MT7688: UART connection and SPI bridge. It means you can control MT7688 with touch buttons or awaken the LEDs when MT7688 detects something.
##1. Preparation
- Installing the lastest Arduino IDE(1.6.9)
- Select Board "Arduino Leonardo" and the right Port
- Install respeaker arduino library(to play with Light, Touch, Sound and Internet)
##2. Getting started Touch to trigger RGB LEDs
- respeaker.h encapsulates touch buttons as interrupt and the interrupt handler will provide the id of touched button(0-7) and the event(touch "0" or release "1"). So you just need to add your code in the interrupt handler to program touch buttons.
#include "respeaker.h"
void setup() {
respeaker.begin();
respeaker.attach_touch_handler(touch_event); // add touch event handler
}
void loop() {}
// id: 0 ~ 7 - touch sensor id; event: 1 - touch, 0 - release
void touch_event(uint8_t id, uint8_t event) {
if (event) {
respeaker.pixels().set_pixel(id, Pixels::wheel(id * 32));
} else {
respeaker.pixels().set_pixel(id, 0);
}
respeaker.pixels().update();
}
##3. UART connection
There are 2 serial ports available in ATmage32U4: "serial" and "serial1".The "serial" is sumilated by the USB port shared with MT7688 and the "serial1" (on TXD1 and RXD1) is connected to MT7688 (on UART_RXD2 and UART_TXD2). They have been set their baudrate to 57600 bps in respeaker.begin()
.
void ReSpeaker::begin(int touch, int pixels, int spi)
{
Serial.begin(57600);
Serial1.begin(57600);
......
}
And read or write something in UART like this:
while (Serial.available() && Serial1.availableForWrite()) {
Serial1.write((char)Serial.read());
}
while (Serial1.available() && Serial.availableForWrite()) {
Serial.write((char)Serial1.read());
}
On the MPU side, libmraa provide API for UART in C/C++, Java, Python and JS.
##4. SPI bridge
With SPI bridge, MT7688 works as master and ATmega32U4 works as salve. Import spi.py in your python script on the MPU side and send data with SPI.write()
.
On Arduino side, you will get the data sent from MT7688 in the spi interrupt handler.
void spi_event(uint8_t addr, uint8_t *data, uint8_t len){
//handle the data sent from MT7688
}