Documenting Intro to Asynchronous Serial Communications
In the code below, I’m getting a reading from pin A0 which is connected to a potentiometer.
#include <Arduino_LSM6DS3.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (!IMU.begin()) {
Serial.println(“Failed to initialize IMU”);
// stop here if you can’t access the IMU:
while (true);
}
}
void loop() {
// put your main code here, to run repeatedly:
int analogValue = analogRead(A0);
int mappedValue = map(analogValue, 0, 1023, 0, 255);
Serial.write(mappedValue);
}
The wiring:
This is not the first time I used a potentiometer but it is my first time to realize that the three pins can be inserted into the breadboard.
of course this is not going to work ….
The reading does not change no matter the position of the potentiometer. I realize I forgot to ground the Arduino…Hope it won’t damage the unit.
The reading changes with the potentiometer after grounding the Arduino. Then I try to display the value in different types.
Then I try to get a second reading from the second potentiometer and have the two values printed out separated by “,”. The code looks like this:
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop() {
for (int thisSensor = 0; thisSensor < 2; thisSensor++) {
int sensorValue = analogRead(thisSensor);
Serial.print(sensorValue);
// if you’re on the last sensor value, end with a println()
// otherwise, print a comma
if (thisSensor == 1) {
Serial.println();
} else {
Serial.print(“,”);
}
}
}
It worked!
Then I try to get three more readings from the accelerometer and gyrometer that’s built inside the Arduino Nano 33 IoT and have them printed out separated by “,”. The code looks like this:
#include <Arduino_LSM6DS3.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (!IMU.begin()) {
Serial.println(“Failed to initialize IMU”);
// stop here if you can’t access the IMU:
while (true);
}
}
void loop() {
// put your main code here, to run repeatedly:
int analogValue = analogRead(A0);
int mappedValue = map(analogValue, 0, 1023, 0, 255);
Serial.write(mappedValue);
// values for acceleration and rotation:
float xAcc, yAcc, zAcc;
float xGyro, yGyro, zGyro;
// if both accelerometer and gyrometer are ready to be read:
if (IMU.accelerationAvailable() &&
IMU.gyroscopeAvailable()) {
// read accelerometer and gyrometer:
IMU.readAcceleration(xAcc, yAcc, zAcc);
// print the results:
IMU.readGyroscope(xGyro, yGyro, zGyro);
Serial.print(xAcc);
Serial.print(“,”);
Serial.print(yAcc);
Serial.print(“,”);
Serial.print(zAcc);
Serial.print(“,”);
Serial.print(xGyro);
Serial.print(“,”);
Serial.print(yGyro);
Serial.print(“,”);
Serial.println(zGyro);
}
}
Lastly, I tried the handshaking flow control. The code looks like this:
void setup() {
Serial.begin(9600);
while (Serial.available() <= 0) {
Serial.println(“ready”);
delay(300);
}
}
void loop() {
int sensorValue = analogRead(A0);
// print the results:
Serial.print(sensorValue);
Serial.print(“,”);
// read the sensor:
sensorValue = analogRead(A1);
// print the results:
Serial.print(sensorValue);
Serial.print(“,”);
// read the sensor:
sensorValue = digitalRead(2);
// print the results:
Serial.println(sensorValue);
if (Serial.available()) {
// read the incoming byte:
int inByte = Serial.read();
// read the sensor:
sensorValue = analogRead(A0);
// print the results:
Serial.print(sensorValue);
Serial.print(“,”);
// read the sensor:
sensorValue = analogRead(A1);
// print the results:
Serial.print(sensorValue);
Serial.print(“,”);
// read the sensor:
sensorValue = digitalRead(2);
// print the results:
Serial.println(sensorValue);
}
}
Change of Plan: Time Zone Converter with Arduino
I’m terrible with time conversion. Even when it’s simple conversion, for example 12 hour between between Shanghai and New York, I still get confused with the date. My goal is to create a device that will facilitate appointments making across time zones.
On p5.js, I created a tool that allows the user to select a target time zone, and see the time and date there by toggling through user’s local time. I am going to create a physical way to interact with the slider, and the buttons with Arduino Nano 33 IoT.
The device has two parts: p5.js and Arduino. The user will be able to change the source time zone date, hour, and minute, and to choose the target time zone with four rotary encoders / potentiometers. p5.js will then do the conversion and light up one of the two LEDs to indicate AM/PM in the target time zone.
System Diagram
Breadboard
BOM
Component | Quantity ¥ | Unit Price |
Arduino Nano 33 IoT | 1 | 220 |
Jumper Wires | 1 set | 3.69 |
Breadboard | 1 | 6 |
Potentiometer | 4 | 3.21 |
LED | 2 | 2 |
Resistor | 2 | 2 |
Demo
Special thanks to Yuchen who helped me debug the code!
To-do list
-
- Find out a way to stabilize the readings so the numbers don’t flash.
so far I have tested the following code to be working on one reading:
/*
SmoothingReads repeatedly from an analog input, calculating a running average and
printing it to the computer. Keeps ten readings in an array and continually
averages them.The circuit:
– analog sensor (potentiometer will do) attached to analog input 0created 22 Apr 2007
by David A. Mellis <dam@mellis.org>
modified 9 Apr 2012
by Tom IgoeThis example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Smoothing
*/// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 200;int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the averageint inputPin = A0;
void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}void loop() {
// subtract the last reading:
total = total – readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;// if we’re at the end of the array…
if (readIndex >= numReadings) {
// …wrap around to the beginning:
readIndex = 0;
}// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
}I still need to figure out how to incorporate it into the for-loop so it works on all four values.
- Find out a way to stabilize the readings so the numbers don’t flash.
- Add the feature to turn on LED with the output from p5.js to Arduino as a feedback.
- Add sound effects when turning the potentiometer as a feedback.
Documenting Final Project: Ideas & Initial Prototype
My star moss needs a consistent supply of water to prosper. Too much water, it gets all brown. Too less water, the petals shrink and lost the shape of a star. So I will make a mist maker with LED light to give the plant a light but consistent amount of water.
BOM:
the mist maker module ¥10
WS2812 5050 RGB LED ¥ 14.5
Arduino Nano 33 IoT ¥220
Breadboard ¥6
Jumper wires ¥ 3.44
Documenting I/O Assignment
- I want to create a device that reminds me when my coffee bean is running low.
- I will place a FSR at the bottom of a box where I keep my bag of coffee bean. I will set a threshold on the FSR analog read. If it is lower than certain value, a LED light will switch on. If it is above the threshold, then the LED light will be kept off.
- I use 50 grams of coffee bean every time either with a Syphon coffee maker or cold brew bottle. So the threshold should be 50 grams which will at least last me one more day.