【Follow me Season 2 Episode 2】+Project Summary
[Copy link]
This post was last edited by superw on 2024-11-6 02:00
All tasks in the article are realized by the materials in the picture
1. Getting Started Tasks (Must Do): Build the environment and start the first step Blink / Serial port print Hello EEWorld!
Arduino official boards must be developed with Arduino IDE. Download the corresponding installation program according to your operating system on the Arduino official website and click Next. Of course, if your computer hard disk is not enough, you can also choose to use the portable version of IDE or browser for development.
Hardware Analysis:
The onboard DL4 is driven by a MOS tube. When P102 outputs a high level, DL4 lights up, and when P102 outputs a low level, DL4 turns off.
Software implementation:
By cyclically flipping the P102 level in the loop function and adding a corresponding delay, the onboard LED can be flashed. The default serial port of the Arduino official development board is initialized using Serial.begin, and other communication serial ports are initialized using Serialx.begin (x=1,2,...). After the serial port is initialized successfully, it can be printed through the print and println functions.
Code implementation:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Hello EEWorld!");
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
The above code implements Task 1, controlling the onboard LED Blink and printing Hello EEWorld through the serial port!
Effect:
2. Basic tasks (must be done): drive 12x8 dot matrix LED; use DAC to generate sine wave; use OPAMP to amplify DAC signal; use ADC to collect and print data to serial port and other interfaces to upload to the host computer to display curve
For the Arduino UNO R4 WiFi on-board resources, the official provides a detailed guide.
Based on the above guidelines, you can complete Task 2.
Hardware analysis: The hardware resources involved in this task are already available on the development board, and the DAC, OPAMP, and ADC are all on-chip resources of the MCU. You can directly operate the hardware through the corresponding functions.
To amplify the DAC signal with OPAMP, you need to use a breadboard to build a simple operational amplifier using the on-chip OPAMP op amp unit. The schematic diagram is as follows:
The above implementation is a voltage follower, but a voltage follower can be understood as a special operational amplifier with a gain of 1, and an output voltage with the same amplitude and frequency as the input voltage. Voltage followers often play a role of buffering and isolation in circuits.
Connect A0 (DAC output) to A1 (OPAMP positive input), short A2 (OPAMP negative input) and A3 (OPAMP output), then you can use an oscilloscope to observe that the output waveform of A3 is consistent with that of A0. Short A3 (OPAMP output) to A4 (ADC acquisition), and you can get the signal voltage collected by ADC.
Code implementation:
#include "analogWave.h"
#include "OPAMP.h"
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
analogWave wave(DAC);
ArduinoLEDMatrix matrix;
int freq = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
wave.sine(freq);
OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
analogReadResolution(14);
matrix.begin();
}
void loop() {
// put your main code here, to run repeatedly:
int adc_value = analogRead(A4);
Serial.println("current adc value is " + String(adc_value));
// Make it scroll!
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(50);
// add the text
const char text[] = " EEWorld & DigiKey ";
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
}
Effect:
For 12x8 dot matrix LED, you will see the words EEWorld & DigiKey after powering on.
The pictures taken by mobile phone cameras are not very clear, but the visual effect of human eyes is relatively better.
A sine wave is generated by a DAC and amplified by an OPAMP. The phenomenon can be observed by an oscilloscope.
Since there is no plug-in resistor, the OPAMP on the chip is configured as a voltage follower mode, that is, the output is the same signal as the input signal. The yellow in the figure is the original DAC sine wave signal, and the green is the sine wave signal output after the voltage follower, and the amplitude and frequency of the two are equal.
The generated sine wave signal is collected by ADC and printed to the serial port. The waveform can be roughly presented with the help of the serial port host computer of Arduino IDE.
3. Advanced Task (Must Do): Connect to the open source smart home platform HA (HomeAssistant) via Wi-Fi using the MQTT protocol
4. Extended Task 1: Upload the light intensity to HA through the external LTR-329 ambient light sensor and display the data through the HA panel
First of all, I would like to thank the teacher for the live broadcast task explanation, which clearly introduced how to build the HomeAssistant environment on the Windows platform. Of course, I also tried to install HomeAssistant through docker on Taishan Pie, but it seemed that due to the instability of Docker Hub, the access always timed out, and there was no magical Internet access on Ubuntu, so I had to give up. Later, I tried to install CasaOS (the system itself already has docker), but in the end I still needed to access HomeAssistant's Docker Hub through docker, which was also not fully implemented. In the end, I chose the method explained by the teacher in the live broadcast, installed Docker Desktop on Windows, then pulled the HomeAssistant image, and created a container.
HomeAssistant interface
EMQX (MQTT) Server Interface
Hardware Analysis:
The picture above shows the LTR329 light sensor module recommended for the task, which has an onboard Qwiic interface. The corresponding Adafruit purchase model is 5591.
The picture above shows the encoder module purchased outside the task, with a Qwiic interface on board. The corresponding Adafruit purchase model is 4991. The board has an MCU with built-in firmware to collect the onboard encoder information and support external IIC bus access to obtain it.
The above picture shows the J2 interface of the Arduino development board. The hardware form is Qwiic. The above two modules can be connected in series through the SH1.0 cable and mounted on the IIC bus together. The IIC host can address different device addresses and access the device.
Note: The IIC address of LTR329 is 0x29, and the IIC address of the encoder module is 0x36.
Software implementation:
Another device selected for this event is the I2C QT ROTARY ENCODER, which integrates an encoder and a WS2812 on the chip. The MCU downloads the seesaw firmware, obtains commands through the I2C interface, and further reads the encoder value through the MCU to control the WS2812. Enter seesaw in the search box and install the corresponding Library. Detailed introduction and driver code can be found at https://learn.adafruit.com/adafruit-i2c-qt-rotary-encoder
Of course, to access the HA platform via WiFi, the most important thing is a home-assistant-integration library, through which you can create various device types, including lights, buttons, sensors, locks, etc. For a detailed library introduction, please refer to https://dawidchyrzynski.github.io/arduino-home-assistant/index.html
After the environment is set up and various libraries are installed, you can start writing code.
Code implementation:
#include "WiFiS3.h"
#include "arduino_secrets.h"
#include "Adafruit_LTR329_LTR303.h"
#include <ArduinoHA.h>
#include <seesaw_neopixel.h>
#define MQTT_OBJECT_ID "followme"
#define BROKER_ADDR IPAddress(192,168,10,246)
#define MQTT_PORT 1883
#define MQTT_USERNAME "admin"
#define MQTT_PASSWORD "123"
#define SS_NEOPIX 6
#define SEESAW_ADDR 0x36
seesaw_NeoPixel sspixel = seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800, &Wire1);
Adafruit_LTR329 ltr = Adafruit_LTR329();
WiFiClient client;
HADevice device(MQTT_OBJECT_ID);
HAMqtt mqtt(client, device);
HAButton buttonR("myButtonR");
HAButton buttonG("myButtonG");
HAButton buttonB("myButtonB");
HASensorNumber lightSensor("mylight");
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;
unsigned long lastUpdateAt = 0;
void onButtonCommand(HAButton* sender)
{
if (sender == &buttonR) {
// button A was clicked, do your logic here
sspixel.setPixelColor(0, sspixel.Color(255, 0, 0));
} else if (sender == &buttonG) {
// button B was clicked, do your logic here
sspixel.setPixelColor(0, sspixel.Color(0, 255, 0));
} else if (sender == &buttonB) {
sspixel.setPixelColor(0, sspixel.Color(0, 0, 255));
}
sspixel.show();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
wifi_init();
ltr329_init();
pixel_init();
device.setName("Arduino");
device.setSoftwareVersion("1.0.0");
buttonR.setIcon("mdi:fire");
buttonR.setName("RED");
buttonG.setIcon("mdi:home");
buttonG.setName("GREEN");
buttonB.setIcon("mdi:water");
buttonB.setName("BLUE");
buttonR.onCommand(onButtonCommand);
buttonG.onCommand(onButtonCommand);
buttonB.onCommand(onButtonCommand);
lightSensor.setIcon("mdi:home");
lightSensor.setName("light value");
mqtt.begin(BROKER_ADDR, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD);
}
void loop() {
// put your main code here, to run repeatedly:
bool valid;
uint16_t visible_plus_ir, infrared;
mqtt.loop();
if ((millis() - lastUpdateAt) > 1000)
{ // 1000ms debounce time
if (ltr.newDataAvailable())
{
valid = ltr.readBothChannels(visible_plus_ir, infrared);
if (valid)
{
Serial.print("CH0 Visible + IR: ");
Serial.print(visible_plus_ir);
Serial.print("\t\tCH1 Infrared: ");
Serial.println(infrared);
lightSensor.setValue(visible_plus_ir);
}
}
lastUpdateAt = millis();
}
}
void wifi_init()
{
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
printWifiStatus();
}
void ltr329_init()
{
Serial.println("Adafruit LTR-329 advanced test");
if ( ! ltr.begin(&Wire1) ) {
Serial.println("Couldn't find LTR sensor!");
while (1) delay(10);
}
Serial.println("Found LTR sensor!");
ltr.setGain(LTR3XX_GAIN_2);
ltr.setIntegrationTime(LTR3XX_INTEGTIME_100);
ltr.setMeasurementRate(LTR3XX_MEASRATE_200);
}
void pixel_init()
{
Serial.println("Looking for seesaw!");
if (!sspixel.begin(SEESAW_ADDR)) {
Serial.println("ERROR! seesaw not found");
while(1) delay(1);
}
Serial.println("seesaw started");
// set not so bright!
sspixel.setBrightness(20);
sspixel.show();
}
/* -------------------------------------------------------------------------- */
void printWifiStatus() {
/* -------------------------------------------------------------------------- */
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
The above code implements two functions. One is to upload the light intensity obtained by LTR-329 to the HA platform. The other is to create three buttons through the HA platform to control the red, green and blue colors of WS2812 respectively.
Hardware connection as shown in the figure
The uploaded light intensity values can be seen on the HA platform, and the specific phenomenon can be seen in the video.
5. Experience
Thank you very much for the official Arduino board. This board was released in 2022 and is very unique in terms of technical innovation and functions. What I am most interested in is the 12x8 dot matrix LED on it, which can generate expressions, animals, characters and other information. In addition, in this activity, I also learned how to build and use the home assistant by following the tutorial, and I will continue to try to successfully build it on the Taishan Pie in the future. Finally, I hope that the follow me activity will get better and better, and bring you more and better development boards from big manufacturers.
6. Compilable and downloadable code
|