Narayana Bhosle
Narayana Bhosle

Reputation: 11

Not able write 1 or 0 to Arduino serial port

I am a beginner in Python 3 and Arduino Uno. I am trying control on board LED from Python command line.

Arduino code:

const int led=13;
int value=0;

void setup() 
{ 
  Serial.begin(9600); 
  pinMode(led, OUTPUT);
  digitalWrite (led, LOW);
  Serial.println("Connection established...");
}


void loop() 
{
  while (Serial.available())
  {
    value = Serial.read();
  }

  if (value == '1')
    digitalWrite (led, HIGH);
  else if (value == '0')
    digitalWrite (led, LOW);
}

Python Code:

import serial                                 # add Serial library for Serial communication

Arduino_Serial = serial.Serial('com3',9600)  #Create Serial port object called arduinoSerialData
print (Arduino_Serial.readline())               #read the serial data and print it as line
print ("Enter 1 to ON LED and 0 to OFF LED")

while 1:                                      #infinite loop
    input_data = input()                  #waits until user enters data
    print ("you entered", input_data )          #prints the data for confirmation

    if (input_data == '1'):                   #if the entered data is 1 
        Arduino_Serial.write('1')             #send 1 to arduino
        print ("LED ON")


    if (input_data == '0'):                   #if the entered data is 0
        Arduino_Serial.write('0')             #send 0 to arduino 
        print ("LED OFF")

I am getting the below error:

TypeError: unicode strings are not supported, please encode to bytes: '1'

Upvotes: 1

Views: 189

Answers (1)

Rakmo
Rakmo

Reputation: 1982

If error is reported by Python, then you can try this:

import serial                                 # Add Serial library for Serial communication

Arduino_Serial = serial.Serial('com3',9600)  # Create Serial port object called  arduinoSerialData
print (Arduino_Serial.readline())               # Read the serial data and     print it as line
print ("Enter 1 to ON LED and 0 to OFF LED")

while 1:                                      # Infinite loop
    input_data = input()                  # Waits until user enters data
    print ("you entered", input_data )          # Prints the data for confirmation

    if (input_data == '1'):                   # If the entered data is 1 
        one_encoded = str.encode('1')
        Arduino_Serial.write(one_encoded)             #send byte encoded 1 to arduino
        print ("LED ON")


    if (input_data == '0'):                   #if the entered data is 0
        zero_encoded = str.encode('0')
        Arduino_Serial.write(zero_encoded)             #send byte encoded 0 to arduino 
        print ("LED OFF")

Explanation:

Python strings can be converted to bytes in following manner:

old_string = 'hello world'
old_string_to_bytes = str.encode(old_string)

Alter:

old_string = 'hello world'
old_string_to_bytes = old_string.encode()

The encoded string is converted to bytes, and is no more treated as a string.

>>> type(old_string_to_bytes)
<class 'bytes'>

You can explore this topic in the documentation.

Upvotes: 1

Related Questions