tino / pyFirmata

Python interface for the Firmata (http://firmata.org/) protocol. It is compliant with Firmata 2.1. Any help with updating to 2.2 is welcome. The Capability Query is implemented, but the Pin State Query feature not yet.
MIT License
576 stars 193 forks source link

Analog input already having a reading when nothing is connected to it #64

Closed skcareer closed 6 years ago

skcareer commented 6 years ago

Hi Tino,

Thanks for your great work!

I was trying to implement pyFirmata for the first time, but i was reading even when no voltage signal is connected to the assigned analogue input.

Please find my code below:

!/usr/bin/python

from pyfirmata import Arduino, util import time

board = Arduino('/dev/ttyACM3') it = util.Iterator(board) it.start() analog_pin = board.get_pin('a:2:i') analog_pin.enable_reporting() led = board.get_pin('d:13:o') while 1:

   reading =  analog_pin.read()
   if reading != None:
             voltage = reading * 5 
             print("Reading = %f\tVoltage = %f" %(reading, voltage))
             led.write(1)
             time.sleep(1)
             led.write(0)
             time.sleep(2)

Looking forward to receiving your possible support

Thanks in advance.

tino commented 6 years ago

Analog ports will have "noise" or "float". You will see the same when you run this directly on the board.

You can "silence" it by connecting the port to ground.