I can't read analog pin with python, pyfirmata

Question:

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

Answers:

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)
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.