|
Post by marcofusco on Jul 10, 2016 15:38:12 GMT
Hey guys,
Haven't made a thread yet, so I decided my first one would include both Arduino and Raspberry Pi! This thread is about serial communication between a Raspbebrry Pi and an Arduino. To do this, I plugged my Uno into the raspberry pi B+ via USB. I installed the Arduino IDE by entering this into the terminal:
sudo apt-get update && sudo apt-get install arduino
This installed an older version of Arduino (1.0.6 or something like that). I uploaded this code to the Arduino:
char receivedChar; boolean newData = false;
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); }
void loop() {
recvInfo(); lightLED(); }
void recvInfo() {
if (Serial.available() > 0) {
receivedChar = Serial.read(); newData = true; } }
void lightLED() {
int led = (receivedChar - '0');
while(newData == true) {
digitalWrite(led, HIGH); delay(2000); digitalWrite(led, LOW);
newData = false; } }
Which reads for a single character coming in to the Arduino from the Pi, and then turns on an LED based off of the character received. (For this to work- connect an led to pin 3, 5, and 6 on the Arduino.
Now, open up IDLE on the Raspberry Pi, and enter this:
>>> import serial >>> ser = serial.Serial('/dev/ttyACM0', 9600)
After this, you should have everything set up. Now-
Turn red LED on:
>>> ser.write('3')
Turn green LED on:
>>> ser.write('5')
Turn yellow LED on:
>>> ser.write('6')
Obviously, this is very simple, and with this code, you could simply just enter these characters to the already existing serial monitor. But, there is always room for expanding. You could create a graphic interface on the Raspberry Pi in which you 'click' buttons on the screen with your mouse that turns on the LEDs.
Marco
|
|
|
Post by Admin on Jul 10, 2016 20:30:08 GMT
|
|
|
Post by curious48 on Jul 10, 2016 20:37:30 GMT
Marco, this is an extremely useful post because it lists all steps that you've taken. I've added the word "walkthrough" to it to emphasize that this has listed all of your steps. Thanks so much for taking the time to write this up!! It may prove useful to people
|
|
|
Post by marcofusco on Jul 11, 2016 0:03:11 GMT
Marco, this is an extremely useful post because it lists all steps that you've taken. I've added the word "walkthrough" to it to emphasize that this has listed all of your steps. Thanks so much for taking the time to write this up!! It may prove useful to people Glad it was useful.
|
|