Arduino – Read Serial Communication with Raspberry Pi

You can connect a Raspberry Pi to your Arduino with a USB cable and read the Serial Output into Variable Values.

Prerequisites:

Functional Parts in the Project:

Python Script

import serial

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    ser.flush()

    while True:
        if ser.in_waiting > 0:
            line = ser.readline().decode('utf-8').rstrip()
            print(line)

Find the connection to the Arduino. Type in the Terminal and find ttyACM0 or ttyACM1. (If you unplug the Arduino and plug it back in the address may iterate up to ACM1 instead of ACM0)

ls /dev/tty*

Arduino Sketch (Any Sketch that outputs in Serial will work)

#define sensorPin  A0

void setup() {
  Serial.begin(9600);
}

void loop()
{
  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

  Serial.print(voltage); Serial.print(" volts  -  ");
  Serial.print(temperatureC); Serial.print(" degrees C  -  ");
  Serial.print(temperatureF); Serial.println(" degrees F");

  delay(1000);
}
This image has an empty alt attribute; its file name is Analog-Temp-Sensor-1-1024x781.jpg
This image has an empty alt attribute; its file name is IMG_3196-1024x768.jpg

Be the first to comment

Leave a Reply