Tim - Serial Communication between two Arduinos
Tim - Serial Communication between two Arduinos
What I wanted to learn, and why it interested me: I've always wondered what to do if I run out of pins on a single Arduino. Turns out I can use two Arduinos. This tech brings me peace of mind.
Final outcome: In my final build, I take in the rotation value of a potentiometer as an input to one Arduino. Then, I transmit the value to a second Arduino, where I convert the value to HSV, then to RGB. The input and output happen completely separately, with one-way communication.
Images of final creative/exploratory output
The workings of the program, featuring LED color changing read from potentiometer input on a separate Arduino
Process images from development towards creative/exploratory output
The tested wiring of the Arduinos, with AdaFruit LED on separate red breadboard.
Process and reflection:
Although the process seemed initially straightforward, I ran into a lot of trouble this week. To start with, I completely forgot about the interference of tx/rx pins with uploading code, which led to much consternation before I asked for help. The initial coding didn't go much better either. For some reason, running two delays independently in the two Arduinos might have conflicted with each other, causing the light to flash. Also, not having ".show()" in the loop caused issues such as the light turning off, which was completely unexpected. I had to review my old code to see what could be the problem. However, using HSV was pretty straightforward and took some light googling.
As compared to my last domain-specific skill building, I believe this one was harder for me to accomplish. Both weeks were pretty rough in terms of workload and club commitments, and I wish I could've done a bit more, such as transmitting two inputs. I also wonder if I can connect 5V to the other Arduino's Vin directly, saving the 5V pin for the breadboard.
Technical details
Electrical schematic; the LED is an AdaFruit NeoPixel with Vin, GND, and an input
/*
* Simple serial transmit counter
*/
const int POTPIN = A3;
int potVal;
void setup() {
Serial.begin(9600);
pinMode(POTPIN, INPUT);
}
void loop() {
potVal = analogRead(POTPIN);
Serial.println(potVal);
delay(10);
}
/*
Simple serial receiver
Changes LED color based on received input.
*/
#include <Servo.h>
#include <Adafruit_NeoPixel.h>
#define PIN 8 // Digital pin the NeoPixel data line is connected to
#define NUM_LEDS 1 // The number of NeoPixels in your strip or ring
const int LEDPIN = 13;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
int lightPos = 0;
void setup() {
pinMode(LEDPIN, OUTPUT);
strip.begin();
strip.setBrightness(50);
strip.show();
Serial.begin(9600);
}
void loop() {
strip.show();
// when there is serial data waiting to be read
if (Serial.available()) {
lightPos = map(Serial.parseInt(), 0, 1023, 0, 65535);
Serial.println(lightPos);
delay(10);
}
strip.setPixelColor(0, strip.ColorHSV(lightPos, 255, 255));
}