Intro to Electronics in Python

25
Intro to Electronics in Python Anna Gerber Intro to Electronics in Python

description

Some into slides for PyconAU 2014 workshop

Transcript of Intro to Electronics in Python

Page 1: Intro to Electronics in Python

Intro to Electronics in Python

Anna Gerber Intro to Electronics in Python

Page 2: 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 thenegative side, i.e. from + (power) to – (ground)

Anna Gerber Intro to Electronics in Python

Page 3: 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

Page 4: 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

Page 5: 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

Page 6: 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

Page 7: 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

Page 8: 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

Page 9: Intro to Electronics in Python

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

Page 10: 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  

Page 11: Intro to Electronics in Python

Control

•  Arduino-compatible Microcontroller co-ordinates robot inputs (sensors) and outputs (actuators)

•  See http://arduino.cc/

Anna Gerber Intro to Electronics in Python

Page 12: 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

Page 13: 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

Page 14: Intro to Electronics in Python

BLINKING AN LED

Anna Gerber Intro to Electronics in Python

Page 15: 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

Page 16: 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

Page 17: 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

Page 18: 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

Page 19: 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

Page 20: 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

Page 21: 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

Page 22: 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

Page 23: 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

Page 24: 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

Page 25: 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