Friday 12 October 2018

Arduino - IV

In the fourth post on Arduino, we see how Python can communicate with Arduino via Serial Port. For the all the work in this post, we will be using the same Arduino Uno and Arduino IDE version 1.8.7. Python version 3.7.0 is used. We will need pyserial library version 3.4 also. Details are shown below:

F:\>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

pyserial details are here. At the time of this line being written 3.4 version is the latest

The Arduino program is shown below:

int data;
int red = 10;

void setup() {
  Serial.begin(9600); //initialize serial COM at 9600 baudrate
  pinMode(red, OUTPUT); //make the LED pin (10) as output
  digitalWrite (red, LOW);
  Serial.println("Message from Arduino setup()");
  Serial.println("Enter 1 for firing up LED");
  Serial.println("Enter 0 for turning off LED");
  Serial.println("Any other number to exit Python program");
}

void loop() {
  
  while (Serial.available()){
    data = Serial.read();
  }
  if (data == '1'){
    digitalWrite (red, HIGH);
  }
  if (data == '0'){
    digitalWrite (red, LOW);
  }
  
}

The above program is uploaded to Arduino board:

























The circuit is shown below:




















The python program used to send input is shown below:

import serial #Serial imported for Serial communication
import time #Required to use delay functions

ArduinoSerialPort = serial.Serial('COM4',9600)

time.sleep(2)

print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())

while True:
  var = input() #get input from user
  print("You entered ", var) #print the input for confirmation
  if ((var != '0') and (var != '1')):
    print("Exiting program")
    break
  elif (var == '1'): #if the value is 1
    ArduinoSerialPort.write(b'1') #send 1 to Arduino
    print("LED is turned ON")
    time.sleep(1)

  elif (var == '0'): #if the value is 0
    ArduinoSerialPort.write(b'0') #send 0 to Arduino
    print("LED is turned OFF")

    time.sleep(1)

If the user enters 1 in Python Shell, LED is turned on. Then, if 0 is entered, LED is turned off. If the user enter any other number, then, the program exits

Open above program in IDLE as shown below:

 























To run above program, click on Run Module under Run menu as shown below:

























This will open the Python Shell as shown below and wait for user input:

























Enter 1. This will light up the LED as shown below:












































Entering 0 will turn of the LED as shown below:












































Entering 5 will exit the program as shown below:

























Thus, we have seen how Python communicates with Arduino using Serial Port using pyserial. This concludes the post on Python interaction with Arduino