March 10, 2011

Plotting analog readings from Arduino using Matplotlib

Uses an LDR (Light Dependent Resistor) to sample readings through the serial port every half second. A Python script reads from the serial port and plots the light intensity using Matplotlib.

Optionally, just for the kicks, there are two LEDs:

  • Green - Lit when the light intensity is higher than 50%
  • Red - Otherwise

Components

  • LDR
  • 2.2kΩ Resistor (red-red-red)
  • Python (with Matplotlib and Serial)
  • Red LED (optional)
  • Green LED (optional)
  • Two 220Ω Resistors (optional, red-red-brown)
  • WARNING: The probability of the resistors being wrong is... extremely high!

Schematics

Arduino Code

const int BAUD_RATE = 9600;
 
const int RED = 9;
const int GREEN = 8;
const int LDR = 0;
 
void setup()   {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  Serial.begin(BAUD_RATE);
}
 
void loop()
{
  int light = analogRead(LDR);
 
  if (light > 500) {
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
  } else {
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, LOW);
  }
 
  Serial.println(light);
  delay(500);
}

Python Code

from serial import Serial
from pylab import *
 
# serial connection to arduino
arduino = Serial('/dev/ttyUSB0', 9600)
 
# readings
y = []
 
# empty plot (interactive mode)
ion()
plot(y, 'b')
draw()
 
while True:
  try:
    # read from arduino
    reading = int(arduino.readline())
    # replot
    y.append(reading)
    plot(y, 'b')
    draw()
  except ValueError:
    pass

Final Result

© Rafael Marmelo 2017