I want to translate this Arduino code, to python code using pyfirmata. How can I do that?
int sw = 0;
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(6, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
sw = digitalRead(2);
if (sw == LOW){
digitalWrite(6, LOW);
}
else {
digitalWrite(6, HIGH);
}
}
I tried doing
from pyfirmata import Arduino, util
board = Arduino('COM3')
it = util.Iterator(board)
it.start()
button = board.get_pin('d:2:i')
led = board.get_pin('d:6:o')
while True:
sw = button.read()
print(sw)
if sw:
led.write(1)
else:
led.write(0)
But that did not work, when I printed sw
it returned None
Then I tried doing this, but that just returned None
all the time.
from pyfirmata import Arduino, util, INPUT
board = Arduino('COM3')
it = util.Iterator(board)
it.start()
while True:
board.digital[2].mode = INPUT
board.digital[2].enable_reporting()
print(board.digital[2].read())
You have to upload Firmata sketch from Arduino IDE to be able to control your board with PyFirmata.
PyFirmata can not communicate with your board if you do not have Firmata Arduino sketch on it. That is the first thing that I would do if I get "None" returned.