0

I have started a project on arduino where i require it to communicate with python.I have gone through web and found a sample code on arduino python serial communication where,it lights up the LED when 1 is enterd. Both python and arduino code are working but the LED is not lighting up. The board is functioning properly since,i have tried other basic examples

Arduino code:

I

  void setup() 
   {
      pinMode(12,OUTPUT);
     digitalWrite(12,LOW);  
     Serial.begin(9600);
 }

  void loop() 
 {
   if(Serial.available() > 0)
   {
     if(Serial.read() == 1)
     {
       digitalWrite(12,HIGH);
       delay(2000);
     }
   }  

     else
     {
       digitalWrite(12,LOW);
     }
     }

Python Code:

import serial
import time  # Required to use delay functions

arduinoSerialData = serial.Serial('/dev/ttyACM0', 9600)  # Create Serial port object called arduinoSerialData
time.sleep(2)  # wait for 2 secounds for the communication to get established


print ("Enter 1 to turn ON LED and 0 to turn OFF LED")

while 1:  # Do this forever

    var =input()  # get input from user
    var=var.encode()


    arduinoSerialData.write(var)

1 Answer 1

1

Try this

   if(Serial.available() > 0)
   {
     if((char)Serial.read() == '1')
     {
       digitalWrite(12,HIGH);
       delay(2000);
     }
   } 

And check this awesome tutorial on arduino forum

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.