Monday, 26 June 2017

Sound Sensor data plot in graph using Arduino and Raspberry Pi

Sound Sensor data plot in graph using Arduino and Raspberry Pi :


Arduino Code:


const int analogIn = A0;
int analogVal = 0;

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

void loop() {

  analogVal = analogRead(analogIn);
  Serial.println(analogVal);
  delay(1000);
}

Python Script:

import serial
import matplotlib.pyplot as plt
from drawnow import *

values = []

plt.ion()
cnt=0

serialArduino = serial.Serial('/dev/ttyACM0', 19200)// this is com port

def plotValues():
    plt.title('Serial data from Arduino')
    plt.grid(True)
    plt.ylabel('Values')
    plt.plot(values, 'rx-', label='values')
    plt.legend(loc='upper right')

#pre-load dummy data
for i in range(0,26):
    values.append(0)
 
while True:
    while (serialArduino.inWaiting()==0):
        pass
    valueRead = serialArduino.readline()

    #check if valid value can be casted
    try:
        valueInInt = int(valueRead)
        print(valueInInt)
        if valueInInt <= 1024:
            if valueInInt >= 0:
                values.append(valueInInt)
                values.pop(0)
                drawnow(plotValues)
            else:
                print "Invalid data"
        else:
            print "Invalid! too large value"
    except ValueError:
        print "Invalid! cannot used"

Circuit Diagram:




0 comments:

Post a Comment

if you have any doubt please let me know