Programming Arduino Using Python

Introduction: Programming Arduino Using Python

About: For more project and tutorial visit my YouTube Channel Smart Technology

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 back and forth between a computer though Arduino's serial.

So, I'm going to show you how to tell your arduino to blink using Python code. Once you understand this,You would touch lots of possibilities since python has an increased productivity with its ability to interact with other platforms.

Obviously,The main idea of this tutorial is to turn ON the LED when I type '1' and '0' to turn OFF.

Hardware components :

1. Arduino Uno

2. LED

3. 220 Ohms resistor

4. Breadboard

5. Wires

Software apps :

1.Python IDLE

2. PySerial

Step 1: Install Python IDLE on Your Computer

You can skip this step if you have installed the Python IDLE already in your computer.If yes then go to step 2 or else look to the following instructions:

1.Go to the python website and download it (here).

2.Once you have done,you move on to installation by keeping the directory in which the python is getting installed by default.

NOTE:Even if your Computer is operating on 64-bit you can use 32-bit Python itself, due to the lack of compatibility with Arduino Libraries.

Step 2: Watch the Video for More Details

Step 3: Install PySerial

PySerial is a Python API module which is used to read and write serial data to Arduino or any other Microcontroller.To install on Windows, simply visit PySerial's Download Page and following the steps bellow :

1.Download the PySerial from the link above.

2.Install it by keeping the setting as the default You should be sure that Pyserial worked correctly,To do this;You type in :

import serial

if you haven't confronted any error,so you're in the good way,if else I advice you to check your installation and Python IDLE extension.

Step 4: Python Code

First up, we need a simple program to get the Python sending data over the serial port.

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)


Step 5: Arduino Code

To initiate a connection with the Arduino from Python, we first have to figure out which COM Port the Arduino is on. This task is simply made by the Ardunio programming environment as I notify in the picture above.

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

}


Step 6: For Support

You can subscribe to the my YouTube channel for more tutorials and projects.
Subscribe for support. Thank you.

Go to my YouTube Channel -link https://goo.gl/EtQ2mp

Be the First to Share

    Recommendations

    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    Discussions

    0
    Phazle1
    Phazle1

    Question 6 months ago on Step 5

    Hi, I have followed this Programming Arduino using Python module copied the code in both Arduino and Python, linked up my Uno on Post 5; which Arduino tells me is the port and the demo 'blink' works changing delay so I see port 5 working and have used port 5 in my the code provided in module but trying to run Python at line 4 'print ArduinoUnoSerial.readline()' I get "Invalid Syntax".
    Arduino prints in monitor "Hello!, How are you Python? so it appears Arduino is working correctly.
    Where am I going wrong? The code is exactly as indicated in this module.
    I was excited finding this module believing my frustrations in getting Python and Arduino talking was finally going to work!
    Please tell me what might be the problem.
    Many thanks
    Philip

    Python error.jpg