SlideShare a Scribd company logo
Intro to Electronics in Python
Anna Gerber
Intro to Electronics in Python
Electricity
•  Electricity is a form of energy
•  We can connect components that convert
electrical energy into other forms of energy:
light, sound, movement, heat etc, into a circuit
•  In a Direct Current (DC) circuit,

electrical energy flows from 

the positive side of a 

power source to the

negative side, i.e. from 

+ (power) to – (ground) 
Anna Gerber
Intro to Electronics in Python
Electrical concepts
•  Current (Amps): measures the flow of electrical
energy through a circuit
•  Voltage (Volts): measures difference in potential
energy between the positive and negative sides
of a circuit
•  Resistance (Ohms): measures a material's
opposition to the flow of energy
•  Power (Watts): the rate at which energy is
converted from one form to another
Anna Gerber
Intro to Electronics in Python
Ohm's Law
Current = Voltage / Resistance
•  Increase the voltage, and the current will
increase (i.e. speed up)
•  Increase the resistance and the current will
decrease
Anna Gerber
Intro to Electronics in Python
Sensors
•  Environmental	
  condi/ons	
  	
  	
  
(e.g.	
  temperature,	
  humidity,	
  smoke)	
  
•  Magne/c	
  (e.g.	
  hall	
  effect	
  sensor)	
  
•  Light	
  (e.g.	
  photo	
  resistor)	
  
•  Sound	
  (e.g.	
  microphone)	
  
•  Mo/on	
  (e.g.	
  accelerometer,	
  /lt,	
  pressure)	
  
•  User	
  /	
  Physical	
  Input	
  (e.g.	
  buDon)	
  
Anna Gerber
Intro to Electronics in Python
Actuators
•  Light	
  &	
  Displays	
  (e.g.	
  LED,	
  LCD)	
  
•  Sound	
  (e.g.	
  Piezo	
  buzzer)	
  
•  Mo/on	
  (e.g.	
  Servo,	
  DC	
  Motor,	
  Solenoid)	
  
•  Power	
  (e.g.	
  Relay)	
  
Anna Gerber
Intro to Electronics in Python
Digital vs Analog
•  Digital
–  discrete values (0 or 1)(LOW or HIGH)
–  Examples: tilt sensor, push button, relay, servo
•  Analog
–  continuous values
–  Examples: photo resistor, DC motor
•  Some sensors support both digital and analog
outputs
Anna Gerber
Intro to Electronics in Python
Using a Breadboard
Anna Gerber
Intro to Electronics in Python
•  Use to prototype circuits without soldering by
plugging in components and jumper wires
•  Letters and numbers for reference
•  Numbered rows are connected
•  Some have power bus along the sides
Resistors
•  Introduces resistance, so restricts the amount of
current that can flow through a circuit
•  Coloured bands indicate resistance
•  Can be connected in either direction
Anna Gerber
Intro to Electronics in Python
LEDs
•  Light Emitting Diode
•  Polarized: diodes act like one way valves so
must be connected in a certain direction
•  Emits light when a current passes through
Anna Gerber
Intro to Electronics in Python
Anode	
  (+)	
  	
  
longer	
  lead	
  
connects	
  to	
  power	
  
Cathode	
  (-­‐)	
  	
  
connects	
  to	
  ground	
  
Control
•  Arduino-compatible
Microcontroller co-
ordinates robot inputs
(sensors) and outputs
(actuators)
•  See http://arduino.cc/ 
Anna Gerber
Intro to Electronics in Python
PyFirmata
•  https://github.com/tino/pyFirmata
•  Communicates with the Arduino using the Firmata
protocol
Install using pip:
pip	
  install	
  pyfirmata	
  
Anna Gerber
Intro to Electronics in Python
Loading Firmata onto the Arduino
•  Once-off setup to prepare our Arduino for use
with PyFirmata:
–  Connect the microcontroller board via USB
–  Launch Arduino IDE and open the Firmata sketch
via the menu: File	
  >	
  Examples	
  >	
  Firmata	
  >	
  
StandardFirmata	
  
–  Select your board type (e.g. Arduino Nano w/
ATmega328) via Tools	
  >	
  Board	
  
–  Select the port for your board via Tools	
  >	
  
Serial	
  Port	
  > (the port of your Arduino) 

e.g. /dev/tty.usbserial-A9GF3L9D
–  Upload the program by clicking on Upload	
  
–  Close the IDE
Anna Gerber
Intro to Electronics in Python
BLINKING AN LED
Anna Gerber
Intro to Electronics in Python
Connecting an LED to the Arduino
•  Unplug the Arduino!
•  Attach long lead of
LED to pin 13 of
Arduino
•  Connect resistor to
cathode of resistor
and ground rail of
breadboard
•  Connect GND pin of
Arduino to ground
rail of breadboard
using a jumper wire
Anna Gerber
Intro to Electronics in Python
Creating the program
1.  Create a Python program file (e.g. blink.py)
2.  Edit it using a text editor e.g. SublimeText
3.  At the start of your program import the library

import	
  pyfirmata	
  
Anna Gerber
Intro to Electronics in Python
Creating the board
We create a Board object which corresponds to our
Arduino-compatible microcontroller board and store it
in a variable. 
We need to provide the port as a parameter:
board	
  =	
  pyfirmata.Arduino("/dev/tty.usbserial-­‐A9QPTF37")	
  
Anna Gerber
Intro to Electronics in Python
Controlling the LED
•  Then we can control the LED via the pin it is
connected to (in this case, pin 13)
•  Use a variable for the pin number to make it easier to
change later
–  PIN	
  =	
  13	
  
•  Turn on LED on pin 13
–  board.digital[PIN].write(1)	
  
•  Turn off LED on pin 13
–  board.digital[PIN].write(0)	
  
Anna Gerber
Intro to Electronics in Python
Delayed behaviour
•  Use the pass_time function to delay functions
by a certain number of seconds e.g. blink LED
on then off after one second:
	
  	
  board.digital[PIN].write(0)	
  
	
  	
  board.pass_time(1)	
  
	
  	
  board.digital[PIN].write(1)	
  
Anna Gerber
Intro to Electronics in Python
Repeating behaviour (loops)
Use a while loop to blink indefinitely:
while	
  True	
  :	
  
board.digital[PIN].write(0)	
  
board.pass_time(1)	
  
board.digital[PIN].write(1)	
  
board.pass_time(1)	
  
Anna Gerber
Intro to Electronics in Python
The entire blink program
import	
  pyfirmata	
  
PORT	
  =	
  "/dev/tty.usbserial-­‐A9QPTF37"	
  
PIN	
  =	
  13	
  
board	
  =	
  pyfirmata.Arduino(PORT)	
  
while	
  True:	
  
	
  	
  	
  	
  board.digital[PIN].write(0)	
  
	
  	
  	
  	
  board.pass_time(1)	
  
	
  	
  	
  	
  board.digital[PIN].write(1)	
  
	
  	
  	
  	
  board.pass_time(1)	
  
Anna Gerber
Intro to Electronics in Python
Running the program from Terminal
•  Open the Terminal app
•  Change directory to the location where you have
saved your code e.g. 

	
  >	
  cd	
  ~/Desktop/code/	
  
•  Run your program using Python e.g.

	
  >	
  python blink.py

•  Hit control-C to stop the program
Anna Gerber
Intro to Electronics in Python
Connecting to iPython Notebook
•  We will use iPython Notebook running on
Raspberry Pi
•  Plug into Raspberry Pi via ethernet (connect to
DHCP server on Pi)
•  Open 192.168.1.1:8888 in your browser
Anna Gerber
Intro to Electronics in Python
How to setup the software at home
•  Install Arduino IDE 
–  Optional, only required if you want to load
Firmata again or experiment with programming
the Arduino using C++
•  Install Python
•  Install PyFirmata	
  
• 	
   Install a code editor e.g. Atom (Mac only),
SublimeText if you don't already have one or
install iPython Notebook
Anna Gerber
Intro to Electronics in Python
Where to find out more
•  Electricity
–  https://www.khanacademy.org/science/physics/
electricity-and-magnetism/v/circuits--part-1
•  Arduino Playground 

–  http://playground.arduino.cc/interfacing/python
•  Sample code for Freetronics kit
–  https://gist.github.com/AnnaGerber/
26decdf2aa53150f7515
Anna Gerber
Intro to Electronics in Python

More Related Content

PPTX
Python Libraries and Modules
DOCX
C programming tutorial
PPTX
Recursion with Python [Rev]
PPTX
Typedef
PPTX
NUMPY-2.pptx
PPTX
C programming - String
PPSX
C language (Collected By Dushmanta)
Python Libraries and Modules
C programming tutorial
Recursion with Python [Rev]
Typedef
NUMPY-2.pptx
C programming - String
C language (Collected By Dushmanta)

What's hot (20)

PPTX
Pointers in C
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPTX
Python Tutorial Part 1
PPSX
Programming with Python
PPTX
Data Type in C Programming
PDF
Python recursion
PPTX
Basic array in c programming
PPTX
Object oriented programming in python
ODP
PPT
How to execute a C program
PPTX
Function overloading
PPTX
Debugging Modern C++ Application with Gdb
PPTX
Iterarators and generators in python
PPTX
PPTX
Presentation on c programing satcture
PPTX
6-Python-Recursion PPT.pptx
PPTX
PDF
An introduction to Python for absolute beginners
PDF
Introduction to python
PDF
Python Unit 3 - Control Flow and Functions
Pointers in C
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Tutorial Part 1
Programming with Python
Data Type in C Programming
Python recursion
Basic array in c programming
Object oriented programming in python
How to execute a C program
Function overloading
Debugging Modern C++ Application with Gdb
Iterarators and generators in python
Presentation on c programing satcture
6-Python-Recursion PPT.pptx
An introduction to Python for absolute beginners
Introduction to python
Python Unit 3 - Control Flow and Functions
Ad

Viewers also liked (9)

PDF
Python for-microcontrollers
ODP
MicroPython&electronics prezentācija
PDF
Python Flavors
PDF
International NodeBots Day Brisbane roundup (BrisJS)
PDF
JavaScript Robotics
PDF
Writing Secure Plugins — WordCamp New York 2009
PDF
Iot 101
PDF
"Serverless" express
PPTX
Basics of Automation, PLC and SCADA
Python for-microcontrollers
MicroPython&electronics prezentācija
Python Flavors
International NodeBots Day Brisbane roundup (BrisJS)
JavaScript Robotics
Writing Secure Plugins — WordCamp New York 2009
Iot 101
"Serverless" express
Basics of Automation, PLC and SCADA
Ad

Similar to Intro to Electronics in Python (20)

PPTX
teststststststLecture_3_2022_Arduino.pptx
PPT
Fundamentals of programming Arduino-Wk2.ppt
KEY
Intro to Arduino
PPT
Arduino wk2
PPT
Arduino is an open-source electronics platform that has an easy-to-use physic...
PPT
Arduino_CSE ece ppt for working and principal of arduino.ppt
PPT
computer_programming for grade 12 learners
PPT
13223971.ppt
PDF
Nsac l1-introduction pwm
PPT
arduino.ppt
PDF
Syed IoT - module 5
PPTX
Introduction To Arduino-converted for s.pptx
PDF
Arduino - Learning.pdf
PPTX
Intro to Arduino.ppt
PPTX
Introduction to Arduino with ArduBlock & SparkFun LilyPad
PPTX
Intro_to_Arduino_-_v30.pptx
PPT
arduino Simon power point presentation.ppt
PPTX
Basic arduino components and more things about arduino
PPT
Intro to Arduino
PDF
Intro to Arduino Revision #2
teststststststLecture_3_2022_Arduino.pptx
Fundamentals of programming Arduino-Wk2.ppt
Intro to Arduino
Arduino wk2
Arduino is an open-source electronics platform that has an easy-to-use physic...
Arduino_CSE ece ppt for working and principal of arduino.ppt
computer_programming for grade 12 learners
13223971.ppt
Nsac l1-introduction pwm
arduino.ppt
Syed IoT - module 5
Introduction To Arduino-converted for s.pptx
Arduino - Learning.pdf
Intro to Arduino.ppt
Introduction to Arduino with ArduBlock & SparkFun LilyPad
Intro_to_Arduino_-_v30.pptx
arduino Simon power point presentation.ppt
Basic arduino components and more things about arduino
Intro to Arduino
Intro to Arduino Revision #2

More from Anna Gerber (17)

PDF
Internet of Things (IoT) Intro
PPTX
How the Web works
PDF
Do you want to build a robot
PPTX
Adding Electronics to 3D Printed Action Heroes
PPTX
3D Printing Action Heroes
PPTX
3D Sculpting Action Heroes
PDF
Data Visualisation Workshop (GovHack Brisbane 2014)
PPTX
Supporting Open Scholarly Annotation
PPTX
Supporting Web-based Scholarly Annotation
PDF
Annotations Supporting Scholarly Editing (OA European Roll Out)
PDF
Annotation Tools (OA European Roll Out)
PDF
Intro to data visualisation
PPTX
Annotations Supporting Scholarly Editing
PPTX
Getting started with the Trove API
PPT
Intro to Java
PPTX
HackFest Brisbane: Discover Brisbane
PPTX
Using Yahoo Pipes
Internet of Things (IoT) Intro
How the Web works
Do you want to build a robot
Adding Electronics to 3D Printed Action Heroes
3D Printing Action Heroes
3D Sculpting Action Heroes
Data Visualisation Workshop (GovHack Brisbane 2014)
Supporting Open Scholarly Annotation
Supporting Web-based Scholarly Annotation
Annotations Supporting Scholarly Editing (OA European Roll Out)
Annotation Tools (OA European Roll Out)
Intro to data visualisation
Annotations Supporting Scholarly Editing
Getting started with the Trove API
Intro to Java
HackFest Brisbane: Discover Brisbane
Using Yahoo Pipes

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
A Presentation on Artificial Intelligence
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Electronic commerce courselecture one. Pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Building Integrated photovoltaic BIPV_UPV.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Unlocking AI with Model Context Protocol (MCP)
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Empathic Computing: Creating Shared Understanding
A comparative analysis of optical character recognition models for extracting...
A Presentation on Artificial Intelligence
Chapter 3 Spatial Domain Image Processing.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Dropbox Q2 2025 Financial Results & Investor Presentation
Electronic commerce courselecture one. Pdf
20250228 LYD VKU AI Blended-Learning.pptx
Assigned Numbers - 2025 - Bluetooth® Document

Intro to Electronics in Python

  • 1. Intro to Electronics in Python Anna Gerber Intro to Electronics in Python
  • 2. Electricity •  Electricity is a form of energy •  We can connect components that convert electrical energy into other forms of energy: light, sound, movement, heat etc, into a circuit •  In a Direct Current (DC) circuit,
 electrical energy flows from 
 the positive side of a 
 power source to the
 negative side, i.e. from 
 + (power) to – (ground) Anna Gerber Intro to Electronics in Python
  • 3. Electrical concepts •  Current (Amps): measures the flow of electrical energy through a circuit •  Voltage (Volts): measures difference in potential energy between the positive and negative sides of a circuit •  Resistance (Ohms): measures a material's opposition to the flow of energy •  Power (Watts): the rate at which energy is converted from one form to another Anna Gerber Intro to Electronics in Python
  • 4. Ohm's Law Current = Voltage / Resistance •  Increase the voltage, and the current will increase (i.e. speed up) •  Increase the resistance and the current will decrease Anna Gerber Intro to Electronics in Python
  • 5. Sensors •  Environmental  condi/ons       (e.g.  temperature,  humidity,  smoke)   •  Magne/c  (e.g.  hall  effect  sensor)   •  Light  (e.g.  photo  resistor)   •  Sound  (e.g.  microphone)   •  Mo/on  (e.g.  accelerometer,  /lt,  pressure)   •  User  /  Physical  Input  (e.g.  buDon)   Anna Gerber Intro to Electronics in Python
  • 6. Actuators •  Light  &  Displays  (e.g.  LED,  LCD)   •  Sound  (e.g.  Piezo  buzzer)   •  Mo/on  (e.g.  Servo,  DC  Motor,  Solenoid)   •  Power  (e.g.  Relay)   Anna Gerber Intro to Electronics in Python
  • 7. Digital vs Analog •  Digital –  discrete values (0 or 1)(LOW or HIGH) –  Examples: tilt sensor, push button, relay, servo •  Analog –  continuous values –  Examples: photo resistor, DC motor •  Some sensors support both digital and analog outputs Anna Gerber Intro to Electronics in Python
  • 8. Using a Breadboard Anna Gerber Intro to Electronics in Python •  Use to prototype circuits without soldering by plugging in components and jumper wires •  Letters and numbers for reference •  Numbered rows are connected •  Some have power bus along the sides
  • 9. Resistors •  Introduces resistance, so restricts the amount of current that can flow through a circuit •  Coloured bands indicate resistance •  Can be connected in either direction Anna Gerber Intro to Electronics in Python
  • 10. LEDs •  Light Emitting Diode •  Polarized: diodes act like one way valves so must be connected in a certain direction •  Emits light when a current passes through Anna Gerber Intro to Electronics in Python Anode  (+)     longer  lead   connects  to  power   Cathode  (-­‐)     connects  to  ground  
  • 11. Control •  Arduino-compatible Microcontroller co- ordinates robot inputs (sensors) and outputs (actuators) •  See http://arduino.cc/ Anna Gerber Intro to Electronics in Python
  • 12. PyFirmata •  https://github.com/tino/pyFirmata •  Communicates with the Arduino using the Firmata protocol Install using pip: pip  install  pyfirmata   Anna Gerber Intro to Electronics in Python
  • 13. Loading Firmata onto the Arduino •  Once-off setup to prepare our Arduino for use with PyFirmata: –  Connect the microcontroller board via USB –  Launch Arduino IDE and open the Firmata sketch via the menu: File  >  Examples  >  Firmata  >   StandardFirmata   –  Select your board type (e.g. Arduino Nano w/ ATmega328) via Tools  >  Board   –  Select the port for your board via Tools  >   Serial  Port  > (the port of your Arduino) 
 e.g. /dev/tty.usbserial-A9GF3L9D –  Upload the program by clicking on Upload   –  Close the IDE Anna Gerber Intro to Electronics in Python
  • 14. BLINKING AN LED Anna Gerber Intro to Electronics in Python
  • 15. Connecting an LED to the Arduino •  Unplug the Arduino! •  Attach long lead of LED to pin 13 of Arduino •  Connect resistor to cathode of resistor and ground rail of breadboard •  Connect GND pin of Arduino to ground rail of breadboard using a jumper wire Anna Gerber Intro to Electronics in Python
  • 16. Creating the program 1.  Create a Python program file (e.g. blink.py) 2.  Edit it using a text editor e.g. SublimeText 3.  At the start of your program import the library import  pyfirmata   Anna Gerber Intro to Electronics in Python
  • 17. Creating the board We create a Board object which corresponds to our Arduino-compatible microcontroller board and store it in a variable. We need to provide the port as a parameter: board  =  pyfirmata.Arduino("/dev/tty.usbserial-­‐A9QPTF37")   Anna Gerber Intro to Electronics in Python
  • 18. Controlling the LED •  Then we can control the LED via the pin it is connected to (in this case, pin 13) •  Use a variable for the pin number to make it easier to change later –  PIN  =  13   •  Turn on LED on pin 13 –  board.digital[PIN].write(1)   •  Turn off LED on pin 13 –  board.digital[PIN].write(0)   Anna Gerber Intro to Electronics in Python
  • 19. Delayed behaviour •  Use the pass_time function to delay functions by a certain number of seconds e.g. blink LED on then off after one second:    board.digital[PIN].write(0)      board.pass_time(1)      board.digital[PIN].write(1)   Anna Gerber Intro to Electronics in Python
  • 20. Repeating behaviour (loops) Use a while loop to blink indefinitely: while  True  :   board.digital[PIN].write(0)   board.pass_time(1)   board.digital[PIN].write(1)   board.pass_time(1)   Anna Gerber Intro to Electronics in Python
  • 21. The entire blink program import  pyfirmata   PORT  =  "/dev/tty.usbserial-­‐A9QPTF37"   PIN  =  13   board  =  pyfirmata.Arduino(PORT)   while  True:          board.digital[PIN].write(0)          board.pass_time(1)          board.digital[PIN].write(1)          board.pass_time(1)   Anna Gerber Intro to Electronics in Python
  • 22. Running the program from Terminal •  Open the Terminal app •  Change directory to the location where you have saved your code e.g. 
  >  cd  ~/Desktop/code/   •  Run your program using Python e.g.
  >  python blink.py
 •  Hit control-C to stop the program Anna Gerber Intro to Electronics in Python
  • 23. Connecting to iPython Notebook •  We will use iPython Notebook running on Raspberry Pi •  Plug into Raspberry Pi via ethernet (connect to DHCP server on Pi) •  Open 192.168.1.1:8888 in your browser Anna Gerber Intro to Electronics in Python
  • 24. How to setup the software at home •  Install Arduino IDE –  Optional, only required if you want to load Firmata again or experiment with programming the Arduino using C++ •  Install Python •  Install PyFirmata   •    Install a code editor e.g. Atom (Mac only), SublimeText if you don't already have one or install iPython Notebook Anna Gerber Intro to Electronics in Python
  • 25. Where to find out more •  Electricity –  https://www.khanacademy.org/science/physics/ electricity-and-magnetism/v/circuits--part-1 •  Arduino Playground –  http://playground.arduino.cc/interfacing/python •  Sample code for Freetronics kit –  https://gist.github.com/AnnaGerber/ 26decdf2aa53150f7515 Anna Gerber Intro to Electronics in Python