0

I want to send data from python to may Arduino Mega via serial output. The Arduino's RX Led blinks, when I run the Python script. However, the Serial.available() = false. This is just an example code. The real project is building a speedometer for sim racing games (using UDP data). Any idea why it isn't working?

Here is the code:

Python:

import time
import serial
port = "COM3"
Arduino = serial.Serial(port ,9600, timeout=1);

i = 0
while i<= 9999 :
    time.sleep(1/10)
    i+=1
    print(i)
    Arduino.write(i)

Arduino:

#include <Arduino.h>
#include <TM1637Display.h>

#define CLK 2
#define DIO 3
int i=0;
int Number;
TM1637Display display(CLK, DIO);
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  display.setBrightness(0x0f);
  display.showNumberDec(9999);
  delay(3000);
}

void loop() { 
   Number= Serial.read();
  
  if (Serial.available()>0){
    display.setBrightness(0x0f);
    display.showNumberDec(Number);
  }
  if (Serial.available() == false){
    display.setBrightness(0x0f);
    display.showNumberDec(0000);
  }
    
}
5
  • What is happening when you execute your script, does the number shows and stays on the display? Did you check your connection? Are you using the Rx port for another use (like the arduino monitor)? Commented Apr 8, 2022 at 15:06
  • 1
    You're calling Serial.read() before checking Serial.available(), I believe by the time if gets to the if statements, there is nothing "available" anymore because it's been read already. Also, Serial.read() returns bytes (arduino.cc/en/serial/read). Try Serial.parseInt() Commented Apr 8, 2022 at 15:07
  • Arduino Mega resets on new USB connection and then waits some seconds in bootloader Commented Apr 8, 2022 at 16:10
  • @sleepystar96 Thanks for your answer. I followed your advice and wrote Serial.read in the if loop. The data is now available. Unfortunetly it can't read the data properly with booth Serial.read() and Serial.parseInt. The Number always equals 0. In Python i tried `print( Arduni.read()) and for every number i get: b''. Might this be the problem of all? sorry for the first comment i accidently pressed enter and couldn't edit it. Commented Apr 12, 2022 at 14:12
  • No, don't use both read and parseInt -> use one or the other. Commented Apr 13, 2022 at 4:12

1 Answer 1

1

This is how it works. Big thanks to csongoose from reddit who helped me a lot. Also thanks to all your replies on this. Python:

import time
import serial
port = "COM3"
Arduino = serial.Serial(port ,9600, timeout=1);

i = 0
while i<= 9999 :
    time.sleep(1)
    i+=1
    print(i)
    #converts int i to string b
    b = "%s" %i
    Arduino.write(bytes(b, 'utf-8'))
    #the Arduino waits for this and can seperate the numbers 
    Arduino.write(b'\n')

Arduino:

#include <Arduino.h>
#include <TM1637Display.h>

#define CLK 2
#define DIO 3
#define D1 5
#define D2 7

int Number;
String b;
//i am using a 7-digit display to show the numbers
TM1637Display display(CLK, DIO);
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(D1 ,OUTPUT);
  pinMode(D2 ,OUTPUT);
  Serial.begin(9600);
}

void loop() { 
  if (Serial.available()>0){
    //looks for new numbers, which are sperated with '\n'
    b = Serial.readStringUntil('\n');
    //converts string b to integer Number
    Number = b.toInt();
    display.setBrightness(0x0f);
    display.showNumberDec(Number);
    //delay equals the same rate as python sends data
    delay(1000);
  
  }
  //this indicates if the data is available and if the Arduino has rebooted
  //you need to wait for it and then start your Python stream
  if (Serial.available() == 0){
    digitalWrite( D1, HIGH);}
    else digitalWrite(D1, LOW);

  if (Number != 0){
    digitalWrite( D2, HIGH);}
    else digitalWrite(D2, LOW);
    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.