marcofusco

New Member







Posts: 9

New Member Walkthrough: serial Com between Raspberry Pi and Arduino curious48 likes this Quote Select Post

Select Post Deselect Post

Deselect Post Link to Post

Link to Post Member Give Gift

Member Back to Top Post by marcofusco on 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





