Arduino, General

Develop Programming an Arduino by Python

In this article , we shall learn how to use python on our computer and all the ways to utilize it with Arduino. We can utilize our computer to transmit data.

Python for the Lab | How to control an Arduino from the computer using Python

Materials Needed:

Control Your Arduino With Python's Pyfirmata Library : 7 Steps - Instructables

for Hardware:

  • Arduino Uno
  • Led (generic)
  • Jumper wires (generic)
  • Resistor 221 ohm
  • Breradboard (generic)

for Software:

  1. Arduino IDE
  2. Python IDLE
  3. PySerial Library

About:

In this tutorial, we’ll learn how we can install Python on our computer and how to use it with Arduino, enabling us to send information across between a computer and an Arduino through serial. I will show you how to send your Arduino to blink via Python code. By knowing this, you would be able to faucet numerous opportunities thanks to Python’s ability to interact with other devices.  I will show you how to use Python code to connect to an Arduino board if your Raspberry Pi is rechargeable. This will help you improve the efficiency of your electronic device.

The main idea of this lesson is to turn on the light when I type 1 and 0 to turn off the light.

 

 

Hardware components:

1. Arduino Uno

  1. LED
  2. 220 Ohms resistor
  3. Breadboard
  4. Wires

Software apps :

1.Python IDLE

  1. PySerial

Steps:

1.Download and Install the python IDLE on the computer.

How to Install Python on Windows [Pycharm IDE]

Downloading and Installing Python on Windows – Dataquest

Python IDLE is installed on your computer? If so go to step 2 but otherwise continue straight to the following instructions:

  • Go to the python website and download its setup file. (file)
  • Once you complete the project, you move on to installation by continuing to keep the directory where the python installation is being carried out by default.

NOTE: Even if your Computer is operating on 32-bit, you can use 64-bit Python itself, because there is no compatibility for Arduino libraries.

 

2. Watch the Video for More Details

VIDEO LINK HERE

3. Install PySerial

Arduino Python Communication Via USB : 4 Steps - Instructables

PySerial is a Python API module that interacts with serial data in order to control an Arduino or other microcontroller. To download the program on Windows, follow these steps: Go to the Download Page for PySerial.

 

  • Download PySerial from the website link provided.
  • Make certain that everything runs smoothly when installing Pyserial. To do this, you should make sure that the Python serial substitution was working:

If you haven’t had any errors, you are in the good space; otherwise, I wholeheartedly advise you to check your installation and Python IDLE extension.

4. Python Code

Programming Arduino Using Python : 6 Steps - Instructables

To send the data produced by the program over the serial port, we have to get a Python program up and running.

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)

5. Coding Arduino

Everything You Need to Know About Arduino Code

To begin a communication with the Arduino from Python, we first have to determine which COM Port the Arduino is on. The Python-based programming environment for the Arduino makes the task appear as shown in the image 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

}