David Hels
David Hels

Reputation: 41

Python and matplotlib and Arduino

I am new to arduino and coding in general. I am trying to get a real time plot to graph the analog values from my arduino. Looking at several examples online I got some code that will try to pull up a graph but states 'str' does not support the buffer interface. Also when I open the arduino program to run on the COM3 port, the Python code says it cannot open port COM3. Here is my Arduino Code

float Concentration=0;

int val = 0;

void setup() {

 // put your setup code here, to run once:

Serial.begin(9600);

}

void loop() {

  // put your main code here, to run repeatedly:

val = analogRead(3);

Concentration =  log ((val)/486.771)/-3.478;

Serial.println(Concentration);

Serial.print("\n");

delay(100);

And here is my Python code

import serial

import numpy as np

from matplotlib import pyplot as plt

ser = serial.Serial('COM3', 9600)

plt.ion() # set plot to animated


ydata = [0] * 50

ax1=plt.axes()  

# make plot

line, = plt.plot(ydata)

plt.ylim([100,400])

# start data collection

while True:  

    data = ser.readline().rstrip() # read data from serial 
                                   # port and strip line endings
    if len(data.split(".")) == 2:

        ymin = float(min(ydata))-10

        ymax = float(max(ydata))+10

        plt.ylim([ymin,ymax])

        ydata.append(data)

        del ydata[0]

        line.set_xdata(np.arange(len(ydata)))

        line.set_ydata(ydata)  # update the data

        plt.draw() # update the plot

        plt.pause(3)

Upvotes: 1

Views: 1080

Answers (1)

Henrik
Henrik

Reputation: 2229

The Arduino code is ok (if you add the missing } in the code you posted.

I've thrown together a small javascript for testing some arduino code here: http://zcuba.dk/arduinoSimpleSim/

It does not simulate an arduino, but translates the sketch into javascript, and runs that in the browser.

It is not complete, and cannot do much, but enough for simple testing. (one of the things it can't do yet is converting to math functions) so copy paste, add the "}" and replace "log(" with "Math.log(" and you'll see that your code runs perfectly.

It writes values to the serial port as it's supposed to: 5V -> -0.21354 +/- noise 0V -> 1.6 +/- noise

so the problem is a python issue.

I tested your code, but without a serial connection, and it works fine on python 2.7.10 If I do:

#import serial
from random import randint

and

'''
data = ser.readline().rstrip() # read data from serial 
                               # port and strip line endings
'''
data = "1." + str(randint(0,200))

so the problem is (as far as I can see) only in the in the serial connection.

see this tutorial: http://petrimaki.com/2013/04/28/reading-arduino-serial-ports-in-windows-7/

as you can see your code should work, so my guess is that your arduino is not on COM3, or COM3 is used by the Arduino IDE

unless you are using python 3. If you use Python3x then string is not the same type as for Python 2.x see this: PySerial and readline() return binary string - convert to regular alphanumeric string?

Upvotes: 1

Related Questions