Smart Technology
Published © CC BY-NC-SA

Programming Arduino Using Python!!!

In this tutorial, we are going to learn how we can install python on our computer and how to use it with Arduino,It allows us to send data.

IntermediateProtip2 hours34,114
Programming Arduino Using Python!!!

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Python IDLE
PySerial Library

Story

Read more

Code

Code snippet #2

Plain text
import serial                                                              #Serial imported for Serial communication
import time                                                                #Required to use delay functions   
ArduinoUnoSerial = serial.Serial('com15',9600)       #Create Serial port object called ArduinoUnoSerialData time.sleep(2)                                                             #wait for 2 secounds for the communication to get established
print ArduinoUnoSerial.readline()                             #read the serial data and print it as line 
print ("You have new message from Arduino")
  while 1:         #Do this forever
            var = raw_input()                                          #get input from user             
            if (var == '1'):                                                #if the value is 1         
                ArduinoUnoSerial.write('1')                      #send 1 to the arduino's Data code       
                print ("LED turned ON")         
                time.sleep(1)          
             if (var == '0'): #if the value is 0         
                ArduinoUnoSerial.write('0')            #send 0 to the arduino's Data code    
                print ("LED turned OFF")         
                time.sleep(1)
             if (var == 'fine and you'): #if the answer is (fine and you)        
                ArduinoUnoSerial.write('0') #send 0    to the arduino's Data code    
                print ("I'm fine too,Are you Ready to !!!")         
                print ("Type 1 to turn ON LED and 0 to turn OFF LED")         
                time.sleep(1)

Code snippet #3

Plain text
int data;
int LED=13;

void setup() { 
  Serial.begin(9600);                               //initialize serial COM at 9600 baudrate
  pinMode(LED, OUTPUT);                    //declare the LED pin (13) as output
  digitalWrite (LED, LOW);                     //Turn OFF the Led in the beginning
  
  Serial.println("Hello!,How are you Python ?");
}
 
void loop() {
while (Serial.available())    //whatever the data that is coming in serially and assigning the value to the variable “data”

{ 

data = Serial.read();

}

if (data == '1')

digitalWrite (LED, HIGH);                  //Turn On the Led

else if (data == '0')

digitalWrite (LED, LOW);                  //Turn OFF the Led

}

Credits

Smart Technology

Smart Technology

7 projects • 29 followers
Graduate of a Professional License in Instrumentation and Biomedical Maintenance and a D.U.T in Electrical Engineering and Maintenance.

Comments