1. Faculty of Engineering Science & Technology(FEST)
Hamdard Institute of Engineering Technology(HIET)
HAMDARD UNIVERSITY
Instructor
ABDUL HASEEB
HANDS-ON WORKSHOP ON PYTHON
PROGRAMMING LANGUAGE
Faculty Development Program (Session-10)
Python Arduino Interfacing
4. Firmata
• Firmata (Italian word)
Library is use to make
Arduino board as Slave
board for Computer
programs like Python, C
etc.
• Simply it MAKES or ADD
IO pins to computer
5. Process of Interfacing Arduino
• Configure Arduino with Standard Firmata
Program
• Interface Arduino through PC or Laptop via
PyFirmata package
9. Complete the Arduino IDE window
• Arduino Board is
Configure with Firmata.
Note the Port name:
Tool>>Port>>Port_Name
• No need of Arduino
Software further, so
close it
10. Working on PyFirmata
• PyFirmata is not a build-in package for Python
IDLE, so first install PyFirmata
11. Initializing PyFirmata
1) Import Module
import pyfirmata
2) Initialize Arduino Board and Com Port Name
Syntax:
board_name_variable = pyfirmata.Arduino_Board(Com Port Name)
Example:
Arduino Uno
board = pyfirmata.Arduino(‘COM4')
Arduino Mega
board = pyfirmata.ArduinoMega (‘COM4')
Arduino Due
board = pyfirmata.ArduinoDue (‘COM4')
For Raspberry pi the com name will be /dev/ttyACM0
Example:
board = pyfirmata.Arduino('/dev/ttyACM0')
12. Setup Pin
The get_pin function is used to set pin as Input or Output and also set
the
Syntax:
Pin_name = board_name.get_pin(‘d/a:pin_number:i/o’)
Example:
• LED = board.get_pin('d:13:o')
• switch = board.get_pin('d:4:i')
• analog = board.get_pin('a:0:i')
• led_PWM = board.get_pin('d:10:p')
• servo = board.get_pin('d:11:s')
Working
Signal Type
a (analog) d (digital)
i (input)
o (output)
p (PWM)
s (servo)
13. Digital Output
import pyfirmata
from time import sleep
board = pyfirmata.Arduino("COM4")
led = board.get_pin('d:13:o')
print("Press Ctrl + c to Exit")
try:
while True: #Infinite Loop
led.write(1) #ON LED
sleep(1) #Delay
led.write(0) #OFF LED
sleep(1) #Delay
except:
print("Program End!")
finally:
led.write(0) #Clear LED before close
board.exit()
17. Controlling a Servo Using PyFirmata
import pyfirmata
board = pyfirmata.Arduino("COM4")
servo_pin = board.get_pin("d:7:s")
print("Press Ctrl + c to Exit")
try:
while True:
angle = int(input("Enter Angle (0 to 180):"))
servo_pin.write(angle)
except:
print("Program End!")
finally:
board.exit()
18. Reading Analog or Digital data from
Arduino
• PyFirmata uses the concept of an Iterator to
monitor the Arduino input pin. The reasons
for this are bound up in the implementation of
Firmata.
it = pyfirmata.util.Iterator(board)
it.start()
Pin_name.enable_reporting()
19. Digital Input
import pyfirmata
board = pyfirmata.Arduino("COM4")
switch = board.get_pin(“d:4:I”)
led = board.get_pin(“d:7:o”)
it = pyfirmata.util.Iterator(board)
it.start()
switch.enable_reporting()
print("Press Ctrl + c to Exit")
try:
while True:
if switch.read() == True:
led.write(1) #ON LED
print('Switch is High')
else:
led.write(0) #OFF LED
print('Switch is Low')
except:
print("Program End!")
finally:
led.write(0) #Clear LED before close
board.exit()
20. Analog Read
import pyfirmata
from time import sleep
board = pyfirmata.Arduino("COM4")
analog = board.get_pin('a:0:i')
it = pyfirmata.util.Iterator(board)
it.start()
analog.enable_reporting()
print("Press Ctrl + c to Exit")
try:
while True:
reading = analog.read()
print(reading)
sleep(1)
except:
print("Program End!")
finally:
board.exit()
22. About MS Excel
For today`s Workshop participant must know
the following about MS Excel
• Workbook
• Worksheet
• Cell
• Rows address
• Column address
• Charts
24. Writing data in MS Excel through Python
import openpyxl
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws1 = wb.create_sheet()
ws.title = "Workshop Sheet"
ws['A1'] = "Integer"
ws['B1'] = "Float"
ws['C1'] = "String"
ws['A2'] = 1000
ws['B2'] = 50.50
ws['C2'] = "Hamdard"
wb.save(r"C:.........workshop.xlsx")
25. Handle the file open Error
import openpyxl
from openpyxl import Workbook
try:
wb = Workbook()
ws = wb.active
wb.save(r"C:.........workshop.xlsx")
except PermissionError:
print("Please close the MS Excel Workbook")
26. Add Image and Merge Cell
import openpyxl
from openpyxl import Workbook
from openpyxl.drawing.image import Image
try:
wb = Workbook()
ws = wb.active
ws.merge_cells('A1:B1') #Merge Cell
ws['A1'] = "Pakistan Zindabad"
Pak_Flag = Image(r"C:.........Pakistan.PNG")
ws.add_image(Pak_Flag, 'A2')
wb.save(r"C:.........workshop.xlsx")
except PermissionError:
print("Please close the MS Excel Workbook")