How can I fix issue connecting Arduino with python?

Viewed 27

I am trying to configure python on my arduino. Ive followed like 10 tutorials but none of them are working. Ive tried to connect via pyserial. When I use pyserial, I simply get no response, here is the code.

I am trying to process data and code in python, then send data to arduino to run. I already downloaded the Firmata libraries on both, no issues there. I have also uploaded the arduino standard firmata sketch, no issues there.

Code being run on my python ide:

import serial
import time

Arduino = serial.Serial('com5',115200)
time.sleep(5)
while True:
    while (Arduino.inWaiting()==0):
        print("ur dumb")
        pass
    dataPacket = Arduino.readline()
    print(dataPacket)

Code being run on my Arduino IDE:

int count = 1;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print(count);
  delay(1000);
  count = count+1;

}

Similary I've tried:

import pyfirmata
import time

board = pyfirmata.Arduino('/dev/ttyACM0')

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

digital_input = board.get_pin('d:10:i')
led = board.get_pin('d:13:o')

while True:
    sw = digital_input.read()
    if sw is True:
        led.write(1)
    else:
        led.write(0)
    time.sleep(0.1)
1 Answers

you can use a more accessible python program here to work:

import serial                                                              
import time                                                                   
ArduinoUnoSerial = serial.Serial('com5',115200)                                                                   

while 1:         
    print (ArduinoUnoSerial.readline())

this will work with your Arduino program

don't worry about the board no matter whether it is mega or uno or anything else ArduinoUnoSerial.readline() will work

Related