Sunday, June 16, 2013

Raspberry Pi and Arduino (Gertboard) serial communication

A serial communication between Raspberry pi and Gertboard's ATMEGA328p chip
Remember both system work at 3.3 V.
NEVER CONNECT RASPBERRY PI and ARDUINO DIRECTLY.
But if you have Gertboard, Atmega328p on Gertboard is working on 3.3V
There are two codes, one for Arduino, the other for Raspberry Pi

Also remember the Jumpers

  • J7 - 3.3V jumper should be inserted
  • Jumper - between MCTX-GP15
  • Jumper - between MCRX-GP14



-----------------------This is the code for Arduino-----------------------------------------

char incomingByte=0;
char inData[20];
int x=0;
 void setup() {
  // initialize serial:
  Serial.begin(9600);
}
 void loop() {
    Serial.print("hello print");
    x=0;
    while (Serial.available()>0){
      incomingByte=Serial.read();
      inData[x]=incomingByte;
      inData[x+1]='\0';
      x++;
    }
    Serial.println(inData);
    delay(1000);
}
-----------------------------------------------------------------------------------------------------

And there is the code for Raspberry Pi based on Python
-----------------------This is the code for Raspberry Pi-----------------------------------------


from time import gmtime, strftime
import time
import serial
import struct
ser = serial.Serial(‘/dev/ttyAMA0‘, 9600)

while 1:
        ser.write(“testing”)
        temp=strftime(“%Y-%m-%d %H:%M:%S”, gmtime())+'\t'+ser.readline()
        print(temp)
        time.sleep(1)
-----------------------------------------------------------------------------------------------------



No comments: