1

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())
MasOOd.KamYab
  • 944
  • 11
  • 25

1 Answers1

0

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.

  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 23 '22 at 06:35