Raspberry Pi Arduino Communication

In order to open Raspberry Pi Serial Communication we will need to do couple of things.
  • Download minicom
  • Remove any reference to ttyAMA0 from the raspbian installation
As with any installation on your Pi, you will want to start with update and upgrade command
run:sudo apt-get update Update command example
run: sudo apt-get upgrade Upgrade command example
when both the update and upgrade commands finished run the following command to install minicom:
sudo apt-get install minicom
After you are done with that, you will need to update two files:
  • /boot/cmdline.txt
  • /etc/inittab
run the following command to make a copy of this file... well for just in case:
sudo cp /boot/cmdline.txt /boot/cmdline.txt.backup
than you will need to open the original file and remove reference to ttyAMA0:
sudo nano /boot/cmdline.txt remove below: console=ttyAMA0,115200 console=tty1
similarly you will want to do for inittab
sudo cp /etc/inittab /etc/inittab.backup
than you will need to open the original file and remove reference to ttyAMA0:
sudo nano /etc/inittab remove below: T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
to start minicom run this:
sudo minicom -b 9600 -o -D /dev/ttyAMA0
where:
  • -b: allow you to set the baud rate
  • -D: allow you to set the device
I have the below code flashed on my arduino:
int const redPin = 9;
int const yellowPin = 8;
int const greenPin = 7;

void setup()
{
pinMode(redPin,OUTPUT);
pinMode(yellowPin,OUTPUT);
pinMode(greenPin,OUTPUT);
TurnOffAll();
Serial.begin(9600);

}

void loop()
{
if(Serial.available()>0)
{
int pinNumber = GetPinNumber();
Serial.readString();
Serial.print("Value Serial got:");
Serial.println(pinNumber);
TurnOffAll();
TurnCorrespondedLED(pinNumber);
delay(250);
}

}

int GetPinNumber()
{
int pinNumber = 0;

while(Serial.available()>0)
{
char val = Serial.read();
Serial.println(val);
pinNumber*=10;
pinNumber+= (val-'0');
delay(25);
}

return pinNumber;
}

void TurnCorrespondedLED(int pinNumber)
{
if(pinNumber == redPin || pinNumber == yellowPin || pinNumber == greenPin)
digitalWrite(pinNumber,HIGH);

}

void TurnOffAll()
{
digitalWrite(redPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(greenPin,LOW);
}
you will need to connect Arduino Rx, Tx to the Pi Tx,Rx (notice that Rx goes to Tx and Tx goes to Rx)
If you need to connect devices etc, I strongly recommend that you will have a voltage shifter between the Pi and the Arduino because of the Voltage differences.
If you used the Arduino code above and you have started minicom on the pi, after connecting all the required wires you can just type the pin number in the minicom terminal and you should be able to see the corresponding LED turning on
Raspberry Pi And Arduino Setup
Raspberry Pi And Arduino Setup - Note 5V & GND from the Pi are connected to Vin & GND on the Arduino