pythonarduino-unopyfirmata

I can't read analog pin with python, pyfirmata


I'm trying to read analog pin for push button. But ı cant read. ı get this error all the time.

My code:

import pyfirmata
import time
from pyfirmata import util


port = "COM4" # port number
board = pyfirmata.Arduino(port)

it = util.Iterator(board)
it.start()

pin_redLed = 8 # red LED pin number

board.analog[11].enable_reporting()



while True : 
            print (board.analog[11].read())
            time.sleep(1)    

Error Message: enter image description here


Solution

  • I solved it:

    import pyfirmata
    import time
    from pyfirmata import util
    
    
    port = "COM4" # port number
    board = pyfirmata.Arduino(port)
    
    it = pyfirmata.util.Iterator(board)
    it.start()
    
    pin_redLed = 8 # red LED pin number
    pin_button = 10 # button pin number
    
    board.digital[pin_button].mode = pyfirmata.INPUT
    
    while True : 
            sw = board.digital[pin_button].read()
            if sw is True :
                    board.digital[pin_redLed].write(1)
            else :
                    board.digital[pin_redLed].write(0)
            time.sleep(0.1)