Arduino Interfacing with Python

Hello There,

I am trying to learn how to interface Arduino with python. I have come across some resources which describe pyfirmata and pyserial packages. Which one is better and why?

Also, I was trying to run a simple LED blink code from python. The result is that:
1.) RX led of arduino blinks
2.) LED connected to pin 10 and breadboard doesn’t

How can I go about these issues?

Take a look at forum.arduino.cc/index.php?topic=598209 as a start

Please post the sketch that you have problems with

Sure UKHeliBob,

So I am trying to run a simple code to run in python to make the LED ligh on bread board blink. The two codes i have used are as follows:
1.) pySerial: Here I have to run the code in the arduino first and next when i run it in python, it executes the code but when I stop it still keeps on blinking. The code is:

import serial
import time
ser = serial.Serial('/dev/cu.usbmodem142101',9600)
time.sleep(1)

def led_on():
  while True:
      while ser.in_waiting==0:
          pass
      data = ser.readline()
      data = str(data,'utf-8')
      print(data)

led_on()
print("done")

2.)Here I try to use pyfirmata. First, I run the standardFirmata through the Arduino IDE and then try to run this python code. But all that happens is that the RX keeps blinking:

import pyfirmata
import time

board = pyfirmata.Arduino('/dev/cu.usbmodem142101')
pin = 12
while True:

    board.digital[pin].write(1)
    time.sleep(1)
    board.digital[pin].write(0)
    time.sleep(1)

As this is an Arduino forum do you think that it might help if you posted the Arduino code ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.