Go Down

Topic: pyfirmata Analog Read returns nonetype  (Read 71 times) previous topic - next topic

dvanommen

I've already read through all the threads I could find that discuss this topic. It's common, but I haven't been able to resolve my issue based on the responses from those other threads.

My full script is below. This is written in python, using pyfirmata to control the Arduino. I'm trying to read in an analog value on the A1 pin, but it returns None every time. Any suggestions?

Code: [Select]
from pyfirmata import Arduino, util
import time

board = Arduino("COM12")
it = util.Iterator(board)
it.start()
board.analog[1].enable_reporting()

print('Enter the port numbers you are using, separated only by spaces:')
ports = [int(x) for x in input().split()]
print('For how long do you want to take data? Enter the time in seconds:')
duration = input()
duration = int(duration)

print ('The ports used in this test are ' + str(ports))
print ('The test will run for ' + str(duration) + ' seconds')
print ('Continue? y/n')

condition = input()

length = len(ports)

if condition=='y':
    start = time.time()
    current = 0

    for j in range(length):
        ports[j] = int(ports[j])
    while current < duration:
        for i in range(length):
            value = str(bin(ports[i])[2:].zfill(6))
            newval = list(value)
            newval.reverse()
            board.digital[2].write(int(newval[0]))
            board.digital[3].write(int(newval[1]))
            board.digital[4].write(int(newval[2]))
            board.digital[5].write(int(newval[3]))
            board.digital[6].write(int(newval[4]))
            board.digital[7].write(int(newval[5]))
            analogval = (board.analog[1].read())
            current = time.time()-start
            print("%.4f" % current, ports[i], analogval)

else:
    print('Please run the script again, and check all setup parameters carefully before entering')
    quit

dvanommen

I've added a time.sleep(0.05) line after starting the Iterator, and it is now returning values, but not what I would expect. I have the A1 pin jumped directly to 5V on the Arduino board, so I would expect the top of the range, which according to the documentation, is 1 (it outputs 0 to 1 rather than 0 to 1024). However, each time I run the code, it gives me one of three different values: 0, 0.219, or 28.0274. There has to be a reason for this, since these numbers are repeating and I get the same numbers if I change the sleep time after initializing the iterator. I just can't see what the reason is.

dvanommen

I should also note that I did try setting up a basic script in the Arduino IDE to read values off a potentiometer using the same board and the same analog pin. It functions exactly as expected.

dvanommen

Never mind. I'm brilliant.

I discovered I had to upload the Firmata sketch to the Arduino board before I could actually use it as such. It's working perfectly now.

Go Up