SlideShare a Scribd company logo
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
Arduino and MS Excel Interface
with Python
Arduino
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
Process of Interfacing Arduino
• Configure Arduino with Standard Firmata
Program
• Interface Arduino through PC or Laptop via
PyFirmata package
Setting Arduino Board and Port
Open Standard Firmata from Example
Upload the Standard Firmata program to
Arduino Board
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
Working on PyFirmata
• PyFirmata is not a build-in package for Python
IDLE, so first install PyFirmata
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')
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)  
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()
Analog Out (PWM)
Analog Out (PWM)
import pyfirmata
board = pyfirmata.Arduino("COM4")
led_brightness = board.get_pin("d:3:p")
print("Press Ctrl + c to Exit")
try:
while True:
duty = int(input("Enter Brightness (0 to 100)= "))
led_brightness.write(duty / 100.0)
except:
print("Program End!")
finally:
board.exit()
Controlling a Servo Using PyFirmata
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()
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()
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()
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()
Python Interface MS Excel
About MS Excel
For today`s Workshop participant must know
the following about MS Excel
• Workbook
• Worksheet
• Cell
• Rows address
• Column address
• Charts
MS Excel View
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")
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")
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")
Add Sheet to Workbook
import openpyxl
from openpyxl import Workbook
try:
wb = Workbook()
ws = wb.active #First Sheet
ws.title = "First"
ws['A25'] = "First Sheet"
ws2 = wb.create_sheet() #New Sheet
ws2['A25'] = "Second Sheet"
ws2.title = "Second"
ws3 = wb.create_sheet() #New Sheet
ws3['A25'] = "Third Sheet"
ws3.title = "Third"
wb.save(r"C:.........workshop.xlsx")
except PermissionError:
print("Please close the MS Excel Workbook")
Accessing Multiple cell
import openpyxl
from openpyxl import Workbook
try:
wb = Workbook()
ws = wb.active
ws['A1'] = "Number"
ws['B1'] = "Square"
for i in range(10):
ws.cell(row = i+2 , column = 1 , value = i)
ws.cell(row = i+2 , column = 2 , value = i*i)
wb.save(r"C:.........workshop.xlsx")
except PermissionError:
print("Please close the MS Excel Workbook")
Adding Chart to Worksheet
Adding Chart to Worksheet
import openpyxl
from openpyxl import Workbook
from openpyxl.chart import
BarChart,Reference,Series
try:
wb = Workbook()
ws = wb.active
ws['A1'] = "Number"
ws['B1'] = "Square"
for i in range(1,11):
ws.cell(row = i+1 , column = 1 , value = i)
ws.cell(row = i+1 , column = 2 , value = i*i)
#Working for Chart
chart = BarChart()
chart.title = "Square Values Chart"
chart.style = 13
chart.x_axis.title = 'Number'
chart.y_axis.title = 'Square of Number'
x_axis = Reference(ws, min_col=1, min_row=2,
max_row=i+1)
y_axis = Reference(ws, min_col=2, min_row=1,
max_col=2, max_row=i+1)
chart.add_data(y_axis, titles_from_data=True)
chart.set_categories(x_axis)
ws.add_chart(chart, "D2")
#Save Workbook
wb.save(r"C:UsersHomeDesktopworkshop.xlsx")
except PermissionError:
print("Please close the MS Excel Workbook")
Chart
Workshop Exercise
• Get Analog data from Arduino, Save it in Excel
sheet and Generate its chart
Ad

Recommended

Python programming workshop session 3
Python programming workshop session 3
Abdul Haseeb
 
Python programming workshop session 2
Python programming workshop session 2
Abdul Haseeb
 
Python programming workshop session 4
Python programming workshop session 4
Abdul Haseeb
 
Python programming workshop session 1
Python programming workshop session 1
Abdul Haseeb
 
10. Recursion
10. Recursion
Intro C# Book
 
12. Exception Handling
12. Exception Handling
Intro C# Book
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
03. Operators Expressions and statements
03. Operators Expressions and statements
Intro C# Book
 
09. Methods
09. Methods
Intro C# Book
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
Intro C# Book
 
C++ Language
C++ Language
Syed Zaid Irshad
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
04. Console Input Output
04. Console Input Output
Intro C# Book
 
Advanced C - Part 2
Advanced C - Part 2
Emertxe Information Technologies Pvt Ltd
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
Naveen Kumar
 
13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Cbse marking scheme 2006 2011
Cbse marking scheme 2006 2011
Praveen M Jigajinni
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
Introduction to C++
Introduction to C++
Sikder Tahsin Al-Amin
 
Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2
Naveen Kumar
 
C++
C++
Sunil OS
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
C++ Programming Language
C++ Programming Language
Mohamed Loey
 
Functional programming in Python
Functional programming in Python
Colin Su
 
Introduction to pcDuino
Introduction to pcDuino
Jingfeng Liu
 
Fund. of IoT LAB - CO 252.pptx
Fund. of IoT LAB - CO 252.pptx
y22co015
 
Cassiopeia Ltd - standard Arduino workshop
Cassiopeia Ltd - standard Arduino workshop
tomtobback
 

More Related Content

What's hot (19)

09. Methods
09. Methods
Intro C# Book
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
Intro C# Book
 
C++ Language
C++ Language
Syed Zaid Irshad
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
04. Console Input Output
04. Console Input Output
Intro C# Book
 
Advanced C - Part 2
Advanced C - Part 2
Emertxe Information Technologies Pvt Ltd
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
Naveen Kumar
 
13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Cbse marking scheme 2006 2011
Cbse marking scheme 2006 2011
Praveen M Jigajinni
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
Introduction to C++
Introduction to C++
Sikder Tahsin Al-Amin
 
Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2
Naveen Kumar
 
C++
C++
Sunil OS
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
C++ Programming Language
C++ Programming Language
Mohamed Loey
 
Functional programming in Python
Functional programming in Python
Colin Su
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
Intro C# Book
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
04. Console Input Output
04. Console Input Output
Intro C# Book
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
Naveen Kumar
 
13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
Develop Embedded Software Module-Session 2
Develop Embedded Software Module-Session 2
Naveen Kumar
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
C++ Programming Language
C++ Programming Language
Mohamed Loey
 
Functional programming in Python
Functional programming in Python
Colin Su
 

Similar to Python workshop session 6 (20)

Introduction to pcDuino
Introduction to pcDuino
Jingfeng Liu
 
Fund. of IoT LAB - CO 252.pptx
Fund. of IoT LAB - CO 252.pptx
y22co015
 
Cassiopeia Ltd - standard Arduino workshop
Cassiopeia Ltd - standard Arduino workshop
tomtobback
 
Processing - MORE Erasmus+ PAU, 2016 February
Processing - MORE Erasmus+ PAU, 2016 February
decibeldanilo
 
Lecture-01-2020J.pptx
Lecture-01-2020J.pptx
pradeepwalter
 
Raspberry Pi Using Python
Raspberry Pi Using Python
Seggy Segaran
 
Introduction to Arduino
Introduction to Arduino
Luki B. Subekti
 
pcDuino Presentation at SparkFun
pcDuino Presentation at SparkFun
Jingfeng Liu
 
Introduction to Arduino
Introduction to Arduino
Amarjeetsingh Thakur
 
Introduction to Arduino
Introduction to Arduino
Amarjeetsingh Thakur
 
arduino
arduino
murbz
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer
 
week2aweek2aweek2aweek2aweek2aweek2aweek2a
week2aweek2aweek2aweek2aweek2aweek2aweek2a
SMARTENGRZ
 
Arduino Development For Beginners
Arduino Development For Beginners
FTS seminar
 
Arduino by bishal bhattarai IOE, Pashchimanchal Campus Pokhara, Nepal
Arduino by bishal bhattarai IOE, Pashchimanchal Campus Pokhara, Nepal
bishal bhattarai
 
Arduino
Arduino
Madugula Kumar
 
Embedded L1_notes_unit2_architecture.pptx
Embedded L1_notes_unit2_architecture.pptx
aartis110
 
DeviceHub - First steps using Intel Edison
DeviceHub - First steps using Intel Edison
Gabriel Arnautu
 
Arduino 8-step drum sequencer 3 channels
Arduino 8-step drum sequencer 3 channels
tomtobback
 
Practical basics on c++
Practical basics on c++
Marco Izzotti
 
Introduction to pcDuino
Introduction to pcDuino
Jingfeng Liu
 
Fund. of IoT LAB - CO 252.pptx
Fund. of IoT LAB - CO 252.pptx
y22co015
 
Cassiopeia Ltd - standard Arduino workshop
Cassiopeia Ltd - standard Arduino workshop
tomtobback
 
Processing - MORE Erasmus+ PAU, 2016 February
Processing - MORE Erasmus+ PAU, 2016 February
decibeldanilo
 
Lecture-01-2020J.pptx
Lecture-01-2020J.pptx
pradeepwalter
 
Raspberry Pi Using Python
Raspberry Pi Using Python
Seggy Segaran
 
pcDuino Presentation at SparkFun
pcDuino Presentation at SparkFun
Jingfeng Liu
 
arduino
arduino
murbz
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer
 
week2aweek2aweek2aweek2aweek2aweek2aweek2a
week2aweek2aweek2aweek2aweek2aweek2aweek2a
SMARTENGRZ
 
Arduino Development For Beginners
Arduino Development For Beginners
FTS seminar
 
Arduino by bishal bhattarai IOE, Pashchimanchal Campus Pokhara, Nepal
Arduino by bishal bhattarai IOE, Pashchimanchal Campus Pokhara, Nepal
bishal bhattarai
 
Embedded L1_notes_unit2_architecture.pptx
Embedded L1_notes_unit2_architecture.pptx
aartis110
 
DeviceHub - First steps using Intel Edison
DeviceHub - First steps using Intel Edison
Gabriel Arnautu
 
Arduino 8-step drum sequencer 3 channels
Arduino 8-step drum sequencer 3 channels
tomtobback
 
Practical basics on c++
Practical basics on c++
Marco Izzotti
 
Ad

Recently uploaded (20)

Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Ad

Python workshop session 6

  • 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
  • 2. Arduino and MS Excel Interface with Python
  • 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
  • 7. Open Standard Firmata from Example
  • 8. Upload the Standard Firmata program to Arduino Board
  • 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()
  • 15. Analog Out (PWM) import pyfirmata board = pyfirmata.Arduino("COM4") led_brightness = board.get_pin("d:3:p") print("Press Ctrl + c to Exit") try: while True: duty = int(input("Enter Brightness (0 to 100)= ")) led_brightness.write(duty / 100.0) except: print("Program End!") finally: board.exit()
  • 16. Controlling a Servo Using PyFirmata
  • 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")
  • 27. Add Sheet to Workbook import openpyxl from openpyxl import Workbook try: wb = Workbook() ws = wb.active #First Sheet ws.title = "First" ws['A25'] = "First Sheet" ws2 = wb.create_sheet() #New Sheet ws2['A25'] = "Second Sheet" ws2.title = "Second" ws3 = wb.create_sheet() #New Sheet ws3['A25'] = "Third Sheet" ws3.title = "Third" wb.save(r"C:.........workshop.xlsx") except PermissionError: print("Please close the MS Excel Workbook")
  • 28. Accessing Multiple cell import openpyxl from openpyxl import Workbook try: wb = Workbook() ws = wb.active ws['A1'] = "Number" ws['B1'] = "Square" for i in range(10): ws.cell(row = i+2 , column = 1 , value = i) ws.cell(row = i+2 , column = 2 , value = i*i) wb.save(r"C:.........workshop.xlsx") except PermissionError: print("Please close the MS Excel Workbook")
  • 29. Adding Chart to Worksheet
  • 30. Adding Chart to Worksheet import openpyxl from openpyxl import Workbook from openpyxl.chart import BarChart,Reference,Series try: wb = Workbook() ws = wb.active ws['A1'] = "Number" ws['B1'] = "Square" for i in range(1,11): ws.cell(row = i+1 , column = 1 , value = i) ws.cell(row = i+1 , column = 2 , value = i*i) #Working for Chart chart = BarChart() chart.title = "Square Values Chart" chart.style = 13 chart.x_axis.title = 'Number' chart.y_axis.title = 'Square of Number' x_axis = Reference(ws, min_col=1, min_row=2, max_row=i+1) y_axis = Reference(ws, min_col=2, min_row=1, max_col=2, max_row=i+1) chart.add_data(y_axis, titles_from_data=True) chart.set_categories(x_axis) ws.add_chart(chart, "D2") #Save Workbook wb.save(r"C:UsersHomeDesktopworkshop.xlsx") except PermissionError: print("Please close the MS Excel Workbook")
  • 31. Chart
  • 32. Workshop Exercise • Get Analog data from Arduino, Save it in Excel sheet and Generate its chart