Raspberry Pi and Arduino Uno

Setup the Raspberry Pi and Arduino Uno

A few sources, eg.Connect Your Raspberry Pi and Arduino Uno! [Instructables, ARDUINO 1.6.9] and Program an Arduino UNO with your Raspberry Pi [raspberrypi.org, ARDUINO 1.8.3], are suggesting to use apt-get to install Arduino;

sudo apt-get update & & sudo apt-get install arduino

But this is contradicted at https://playground.arduino.cc/Linux/Raspbian/, as the apt-get library will install an obsolete version.

Use the “Linux ARM’ download from: https://www.arduino.cc/en/Main/Software.
DO NOT use sudo apt-get install arduino because you will get an extremely outdated and possibly non-standard version of the Arduino IDE.



Using the Chrome browser on the Pi, get latest package from https://www.arduino.cc/en/Main/Software currently ARDUINO 1.8.9
The Raspberry Pi 3 uses the “Linux ARM 32 bits”, not the “Linux ARM 64 bits” package.
Testing using a Pi 3B+ running Raspbian Buster.

cd Downloads/
tar -xf arduino-1.8.9-linuxarm.tar.xz
sudo mv arduino-1.8.9 /opt
sudo /opt/arduino-1.8.9/install.sh

Alternatively

cd Downloads/
wget -qO- https://downloads.arduino.cc/arduino-1.8.9-linuxarm.tar.xz | tar xJv
cd arduino-1.8.9
./install.sh

You can test if the package has installed by checking int the Pi application menu.
Programming > Arduino IDE

Check connectivity between the Raspberry Pi
and Arduino Uno

launch Arduino 1.8.9
Tools >
Board = Arduino/Genuino Uno
Port = “dev/ttyACMO (Arduino/Genuino Uno)”
Get Board Info

Check coding capability: Raspberry Pi
and Arduino Uno

TEST PROJECT 1: LED Traffic Light
LED Traffic Light Module for Arduino CAT.NO: XC3720
https://www.instructables.com/id/LED-Traffic-Light-Module-Using-Arduino-UNO/

Traffic_Light.ino

/*MYBOTIC TRAFFIC LIGHT PEDESTRIAN CONTROL*/

#define RED_SET_TIME 5000
#define YELLOW_SET_TIME 2000
#define GREEN_SET_TIME 5000

#define LIGHT_1_RED 2
#define LIGHT_1_YELLOW 3
#define LIGHT_1_GREEN 4

#define GREEN_LIGHT 0
#define YELLOW_LIGHT 1
#define RED_LIGHT 2

unsigned long gulStart_Timer = 0;

unsigned short gusSet_Time[3] = {GREEN_SET_TIME, YELLOW_SET_TIME, RED_SET_TIME};

short gsWhich_Light_Index = GREEN_LIGHT;

void setup() 
{
  Serial.begin(9600);
  pinMode(LIGHT_1_RED, OUTPUT);
  pinMode(LIGHT_1_YELLOW, OUTPUT);
  pinMode(LIGHT_1_GREEN, OUTPUT);

  digitalWrite(LIGHT_1_GREEN, HIGH);
  digitalWrite(LIGHT_1_YELLOW, LOW);
  digitalWrite(LIGHT_1_RED, LOW);  
  gulStart_Timer = millis();
}

// the loop function runs over and over again forever
void loop() 
{

  if((millis()-gulStart_Timer) >= gusSet_Time[gsWhich_Light_Index])
  {
    gsWhich_Light_Index++;

    if(gsWhich_Light_Index > RED_LIGHT)
    {
      gsWhich_Light_Index = GREEN_LIGHT;
    }
    
    gulStart_Timer = millis();

    if(gsWhich_Light_Index == GREEN_LIGHT)
    {
      digitalWrite(LIGHT_1_GREEN, HIGH);
      digitalWrite(LIGHT_1_YELLOW, LOW);
      digitalWrite(LIGHT_1_RED, LOW);      
    }
    else if(gsWhich_Light_Index == YELLOW_LIGHT)
    {
      digitalWrite(LIGHT_1_GREEN, LOW);
      digitalWrite(LIGHT_1_YELLOW, HIGH);
      digitalWrite(LIGHT_1_RED, LOW);      
    }
    else if(gsWhich_Light_Index == RED_LIGHT)
    {
      digitalWrite(LIGHT_1_GREEN, LOW);
      digitalWrite(LIGHT_1_YELLOW, LOW);
      digitalWrite(LIGHT_1_RED, HIGH);      
    }
  }
}

TEST PROJECT 2: Test LED Traffic Light from the Terminal
Once Traffic_Light.ino is uploaded to the Arduino board, you can activate the lights direct from the Terminal console.

echo “2” >/dev/ttyACM0
echo “3” >/dev/ttyACM0
echo “4” >/dev/ttyACM0

TEST PROJECT 3: LED Traffic Light
Use base details from https://www.instructables.com/id/Connect-Your-Raspberry-Pi-and-Arduino-Uno/
Change pins in the .ino to match Project 1 (R=2, Y=3, G=4)

TRAFFIC-LIGHT.ino

char receivedChar;
boolean newData = false;

void setup() {

  Serial.begin(9600);

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, 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;
    
  }
  
}

This appears to be a Python 2.7 project, Python 3.x we get errors with the ser.write(‘X’)
(‘unicode strings are not supported, please encode to bytes: {!r}’.format(seq))
change this to ser.write(‘X’.encode()) fixes the problem, and so does ser.write(bytes(b’X’))

from the Python shell you can use;
>>> ser.write(bytes(b’3′))

TRAFFIC-LIGHT.py

import serial
from time import sleep
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.flush.Input()

# Traffic Light
# Pin 2 = Red, Pin 3 = Yellow, Pin 4 = Green

while True:
                ser.write(bytes(b'2'))
                sleep(2)
                ser.write(bytes(b'3'))
                sleep(2)
                ser.write(bytes(b'4'))
                sleep(2)     
This entry was posted in 2019, Edfac, education, geek. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.