Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
170 commits
Select commit Hold shift + click to select a range
7097073
added images
elenafalomo Aug 23, 2017
ca44dfa
updated for remot3.it
elenafalomo Aug 23, 2017
90af13c
minor changes to md
elenafalomo Aug 24, 2017
9e61362
minor changes, altered the index, added the RPi-Arduino section
elenafalomo Aug 24, 2017
98c85e4
updated initial instructions and screens
elenafalomo Aug 24, 2017
9b306c6
minor fixes on setup
elenafalomo Aug 24, 2017
627c272
fixed directories
elenafalomo Aug 24, 2017
903825c
files added
elenafalomo Aug 24, 2017
1f3c814
other minor fixes
elenafalomo Aug 24, 2017
186ba2c
updated arduino setup guide for raspberry, added images
elenafalomo Aug 24, 2017
cbe666b
modified rpi-setup, added minor changes 29th august
elenafalomo Aug 31, 2017
222d23d
additional changes, added TODOs in comments
elenafalomo Aug 31, 2017
27fb875
updated all the text, some image-related TODOs are still missing
elenafalomo Aug 31, 2017
aec1cdc
corrected coments syntax
elenafalomo Sep 1, 2017
141c029
added windows screenshots
elenafalomo Sep 1, 2017
337e41c
Merge branch '2017-updates' of https://github.com/ICL-DE/Gizmo into 2…
elenafalomo Sep 1, 2017
edcc2f7
minor changes in windows rpi setup
elenafalomo Sep 1, 2017
e00fbc2
arudino warmup improvement
elenafalomo Sep 4, 2017
49eae0f
fixed arduino hardware
elenafalomo Sep 4, 2017
a8aa4c3
image formatting test
elenafalomo Sep 4, 2017
3800c4d
added more material to arduino section
elenafalomo Sep 4, 2017
eba7583
minor changes
elenafalomo Sep 4, 2017
0897e66
added TODOs, minor changes
elenafalomo Sep 4, 2017
9fd15f7
added TODOs, minor changes
elenafalomo Sep 4, 2017
0ae2941
added files, tidied up directories accordingly to the changes of the …
elenafalomo Sep 6, 2017
4ddc584
added files
elenafalomo Sep 6, 2017
d9f544f
Merge branch '2017-updates' of https://github.com/ICL-DE/Gizmo into 2…
elenafalomo Sep 6, 2017
194933d
added screen cheatsheet
elenafalomo Sep 6, 2017
e97f84c
added md cheatsheet
elenafalomo Sep 11, 2017
6e0e06a
updated formating
elenafalomo Sep 11, 2017
01e3713
fixed formatting issues
elenafalomo Sep 11, 2017
6dced13
fixed directory tree, updated rpi to arduino
elenafalomo Sep 11, 2017
7903c9d
minor changes and updates
elenafalomo Sep 12, 2017
7f531a4
fully integrated rpi to arduino over cli
elenafalomo Sep 12, 2017
1622306
added serial communication between arduino and rpi
elenafalomo Sep 12, 2017
492d450
wifi setup updated, repo structure fixed
elenafalomo Sep 13, 2017
2d2abd3
improvements + added images
elenafalomo Sep 28, 2017
5f9d597
Merge pull request #1 from ICL-DE/2017-updates
elenafalomo Sep 28, 2017
a4d82c0
added git cheatsheet, renamed directories
elenafalomo Sep 28, 2017
d2e625e
Changed links
elenafalomo Sep 28, 2017
01785b2
changed links
elenafalomo Sep 28, 2017
e78ef99
Minor bug fix and comments
JSP110 Sep 28, 2017
e5121b9
Adjustment to text.
JSP110 Sep 28, 2017
7989ed4
Include tips for remot3.it.
JSP110 Sep 28, 2017
4ffd9d6
Minor text update
JSP110 Sep 28, 2017
2ba6230
Add links to advanced remote networking
JSP110 Sep 28, 2017
ab5891f
changed the links in READMEs files
elenafalomo Sep 28, 2017
5c77627
added syllabus
elenafalomo Sep 28, 2017
481e907
Include Master Link List
JSP110 Sep 28, 2017
d981bd3
added newimages for workflow
elenafalomo Sep 28, 2017
f5acd45
Merge branch '2017-updates' of https://github.com/ICL-DE/Gizmo into 2…
elenafalomo Sep 28, 2017
2bd83e4
Merge branch '2017-updates' of https://github.com/ICL-DE/Gizmo into 2…
elenafalomo Sep 28, 2017
30ed129
minor corrections
elenafalomo Sep 28, 2017
fe3882d
link change
elenafalomo Sep 28, 2017
233191e
link change
elenafalomo Sep 28, 2017
157535d
Change of Book structure
JSP110 Sep 28, 2017
6a4dab9
Fix Typo
JSP110 Sep 28, 2017
ee92a62
Restructure the syllabus
JSP110 Sep 28, 2017
c3f737b
Minor structure update
JSP110 Sep 28, 2017
c0fb728
fixed directories
elenafalomo Sep 28, 2017
c36c056
Added flex sensor tutorial
elenafalomo Sep 29, 2017
a81ee45
added photocell tutorial
elenafalomo Sep 29, 2017
ec10d3b
fixed first lab notes
elenafalomo Oct 9, 2017
a153942
fixed links
elenafalomo Oct 9, 2017
977421c
img fix
elenafalomo Oct 9, 2017
63e492a
image fix
elenafalomo Oct 9, 2017
c28968d
image fix
elenafalomo Oct 9, 2017
ba29a26
img fix
elenafalomo Oct 9, 2017
033104f
fixed images
elenafalomo Oct 9, 2017
ddb7b05
added images
elenafalomo Oct 10, 2017
896d673
minor fixes
elenafalomo Oct 10, 2017
3e37deb
fixed links
elenafalomo Oct 10, 2017
d5c5382
fixed links
elenafalomo Oct 10, 2017
994e212
final rpi setup
elenafalomo Oct 10, 2017
9941786
final rpi setup
elenafalomo Oct 10, 2017
b24425a
final remote
elenafalomo Oct 10, 2017
1d3c919
final python
elenafalomo Oct 10, 2017
d32319f
Update DE2-Gizmo-2017.md
elenafalomo Oct 10, 2017
f5e03be
updated links
elenafalomo Oct 10, 2017
b27eb12
fixed typo
elenafalomo Oct 10, 2017
c976887
updated links
elenafalomo Oct 10, 2017
85ad04e
Update RPi_setup.md
JSP110 Oct 11, 2017
0840e9b
Update RPi_setup.md
JSP110 Oct 11, 2017
cadc536
Update Remote_Rpi.md
JSP110 Oct 11, 2017
108d93f
Create Backup FIle
JSP110 Oct 11, 2017
31f2714
Fix 1
JSP110 Oct 11, 2017
17b53ec
Merge pull request #3 from ICL-DE/2017-update2-fix
JSP110 Oct 11, 2017
81e0141
Include Week 1 Presentation to Syllabus
JSP110 Oct 11, 2017
5087710
Update DE2-Gizmo-2017.md
JSP110 Oct 11, 2017
5f4afbc
Update DE2-Gizmo-2017.md
JSP110 Oct 11, 2017
19f95bf
Update DE2-Gizmo-2017.md
JSP110 Oct 11, 2017
6089770
Rename Slides
JSP110 Oct 11, 2017
2702d98
Merge branch 'master' of https://github.com/ICL-DE/Gizmo
JSP110 Oct 11, 2017
16ba79a
Merge remote-tracking branch 'refs/remotes/origin/master' into 2017-u…
JSP110 Oct 11, 2017
cd4f8cd
fixed rpi blink
elenafalomo Oct 15, 2017
89bf3a8
renamed folders
elenafalomo Oct 15, 2017
98426b7
fixed rpi pwm
elenafalomo Oct 15, 2017
00b2113
added rpi button
elenafalomo Oct 15, 2017
c15bd82
added rpi combo
elenafalomo Oct 15, 2017
bc30033
fixed some arduino notes
elenafalomo Oct 16, 2017
bfc9ff8
Renamed old header layout to have suffic RPi2
JSP110 Oct 16, 2017
5350fac
include a new RPi3 GPIO layout
JSP110 Oct 16, 2017
ec2d2d9
Include credits and acknowledgement
JSP110 Oct 16, 2017
1efaee0
fix link for BackupRPi
JSP110 Oct 16, 2017
ceefcff
Fix typo in link
JSP110 Oct 16, 2017
7a0140d
Fix link typo
JSP110 Oct 16, 2017
34d2438
added arduino blink
elenafalomo Oct 16, 2017
b0a9702
added arduino fade
elenafalomo Oct 16, 2017
ee35f0c
finished arduino phyz comp intro
elenafalomo Oct 16, 2017
2051b2a
fixed rpi-arduino
elenafalomo Oct 16, 2017
6cb198f
updated links
elenafalomo Oct 16, 2017
51af3b8
Merge branch '2017-update-3' of https://github.com/ICL-DE/Gizmo into …
elenafalomo Oct 16, 2017
4f452d5
updated links
elenafalomo Oct 16, 2017
d4f727f
updated links
elenafalomo Oct 16, 2017
22cadf0
added rpi scheme
elenafalomo Oct 17, 2017
d977959
Update RPi_GPIO.md
JSP110 Oct 17, 2017
4214215
Update Arduino_warmup.md
JSP110 Oct 17, 2017
c51a806
fixed broken reference
elenafalomo Oct 17, 2017
b4c94f3
Update Arduino_warmup.md
JSP110 Oct 17, 2017
ee13de2
Update Arduino_warmup.md
JSP110 Oct 17, 2017
fef6084
Update RPi_to_Arduino.md
JSP110 Oct 17, 2017
bce3922
Update DE2-Gizmo-2017.md
JSP110 Oct 17, 2017
34e9aa7
Merge pull request #5 from ICL-DE/2017-update-3
JSP110 Oct 17, 2017
9199842
Add Slides
JSP110 Oct 17, 2017
6deae11
Add link to week 2 slides
JSP110 Oct 17, 2017
d18913a
name fix
nebbles Oct 17, 2017
93336c6
fixes for links/text/formatting
nebbles Oct 17, 2017
b103251
Merge pull request #6 from ICL-DE/2017-update-3
JSP110 Oct 18, 2017
cc48525
Update RPi_to_Arduino.md
JSP110 Oct 18, 2017
668ce0b
Update RPi_GPIO.md
JSP110 Oct 18, 2017
cc01b41
Merge pull request #7 from ICL-DE/2017-update-3
JSP110 Oct 18, 2017
bcc8281
change to button example
nebbles Oct 20, 2017
fad55db
Updates to rpi2arduino
nebbles Oct 20, 2017
a8e2be0
minor changes
elenafalomo Oct 22, 2017
21c123e
Merge branch '2017-update-3' of https://github.com/ICL-DE/Gizmo into …
elenafalomo Oct 22, 2017
93e5a79
Merge branch '2017-update-3' of https://github.com/ICL-DE/Gizmo into …
elenafalomo Oct 22, 2017
bfb9c04
sensors fixed
elenafalomo Oct 22, 2017
1c2ee4c
added buzzer
elenafalomo Oct 22, 2017
94130ae
added stepper
elenafalomo Oct 22, 2017
0657187
added dc
elenafalomo Oct 22, 2017
f3b64ce
added driver specs
elenafalomo Oct 23, 2017
fde3708
added serial rpi to arduino
elenafalomo Oct 23, 2017
0f0e695
updated links
elenafalomo Oct 23, 2017
dfe4f68
Update Actuators.md
JSP110 Oct 23, 2017
f19409c
Update Sensors.md
JSP110 Oct 23, 2017
ce4555e
added references
elenafalomo Oct 23, 2017
d45bfe3
Update Actuators.md
JSP110 Oct 23, 2017
f4c76b0
added images, fixed references
elenafalomo Oct 24, 2017
c78b9a2
added rpi ref
elenafalomo Oct 24, 2017
ccff205
Fix to code block format
nebbles Oct 24, 2017
1dc90eb
minor formatting fixes
nebbles Oct 24, 2017
3d099c2
minor formatting fixes
nebbles Oct 24, 2017
e979a79
Merge pull request #8 from ICL-DE/2017-update-3
JSP110 Oct 25, 2017
c355f5c
Merge branch '2017-update-3' of https://github.com/ICL-DE/Gizmo into …
elenafalomo Oct 25, 2017
a429209
minor fixes
elenafalomo Oct 25, 2017
c295fad
Fixed bug in python code
nebbles Oct 25, 2017
2e25e0e
Added clarification on directory change
nebbles Oct 25, 2017
ec47281
Merge pull request #9 from ICL-DE/2017-update-3
nebbles Oct 25, 2017
67d9a77
fixes to directory commands
nebbles Oct 25, 2017
65e469e
Merge pull request #10 from ICL-DE/master
nebbles Oct 25, 2017
c638917
Clarification in Arduino2Pi Over Serial
nebbles Oct 25, 2017
f513c0e
Clarification on reading sensor information
nebbles Oct 25, 2017
365ca2f
Refinement to reading sensor data
nebbles Oct 25, 2017
961900d
Added serial_monitor_button.jpg
nebbles Oct 25, 2017
c71190f
added sudo to make command
nebbles Oct 25, 2017
fcbc32a
Update image
nebbles Oct 25, 2017
b086ff9
typo fix
nebbles Oct 25, 2017
0a43152
fix to arduino serial write commands
nebbles Oct 25, 2017
6030041
added exit python prompt command
nebbles Oct 25, 2017
8962975
Merge pull request #11 from ICL-DE/master
nebbles Oct 25, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
_site
.idea/misc.xml
.idea/encodings.xml
.idea/modules.xml
.idea/Gizmo.iml
.idea/workspace.xml
.idea/vcs.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Introduction to Python


#### About Python Language
#### About Python Language

Remember that you are intelligent, and you can learn, but the computer is simple and very fast, but can not learn by itself. Therefore, for you to communicate instructions on the computer, it is easier for you to learn a computer Language (e.g. Python) than for the computer to learn English.

Expand Down Expand Up @@ -200,7 +199,7 @@ print x
When a program is running, it flows from one step to the next. As programmers, we set up “paths” for the program to follow.

<p align="center">
<img src="Flow_1.png" alt="Drawing" width="300">
<img src="../img/Flow_1.png" alt="Drawing" width="300">
</p>

Close the text editor, and then you can execute it in two ways:
Expand All @@ -219,7 +218,7 @@ $ ./example_fllow.py
Now let's do an example where we have a __conditional__ that implies a decision-making about a situation. Decision making is the anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions. The following diagram illustrates the conditional:

<p align="center">
<img src="decision_making.jpg" alt="Conditional" width="300">
<img src="../img/decision_making.jpg" alt="Conditional" width="300">
</p>

``` bash
Expand All @@ -245,13 +244,13 @@ $ ./example_conditional.py
Flow of the code:

<p align="center">
<img src="Flow_Conditional.png" alt="Conditional" width="300">
<img src="../img/Flow_Conditional.png" alt="Conditional" width="300">
</p>

A __loop statement__ allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement:

<p align="center">
<img src="loop_architecture.jpg" alt="Conditional" width="300">
<img src="../img/loop_architecture.jpg" alt="Conditional" width="300">
</p>

__While loop__ repeats a statement or group of statements while a given condition is _TRUE_. It tests the condition before executing the loop body.
Expand All @@ -277,7 +276,7 @@ $ ./example_while_loop.py
Flow of the code:

<p align="center">
<img src="while_loop.png" alt="Conditional" width="300">
<img src="../img/while_loop.png" alt="Conditional" width="300">
</p>

Loops (repeated steps) ha‰ve _iteration variables_ that change each time through a loop (like _n_). Often these _iteration variables_ go through a sequence of numbers.
Expand Down
Binary file added Chapter_1_Python/Python_warmup.pdf
Binary file not shown.
282 changes: 282 additions & 0 deletions Chapter_2_RPi/RPi_GPIO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
# Raspberry Pi GPIOzero

1. [Materials needed](#materials-needed)
2. [GPIO pinout](#gpio_pinout)
3. [Analog vs. Digital](#analog-vs-digital)
4. [Blink](#blink)
5. [LED PWM](#led-pwm)
6. [Button](#button)
7. [Combining everything](#combining-everything)

Your Raspberry Pi is more than just a small computer, it is a hardware prototyping tool! The RPi has **bi-directional I/O pins**, which you can use to drive LEDs, spin motors, or read button presses. To drive the RPi's I/O lines requires a bit or programming. You can use a [variety of programing languages](http://elinux.org/RPi_GPIO_Code_Samples), but we decided to use a really solid, easy tools for driving I/O: **Python**.

### Materials needed

* Raspberry Pi 3 B
* [Breadboard](https://www.sparkfun.com/products/12002?_ga=1.251311686.1915117394.1476705504)
* [Jumper Wires(M/F)](https://www.sparkfun.com/products/12794)
* [Momentary Pushbutton Switch](https://www.sparkfun.com/products/9190?_ga=1.213562324.1915117394.1476705504)
* [Resistors](https://www.sparkfun.com/products/11507?_ga=1.213562324.1915117394.1476705504)
* [2 LEDs](https://www.sparkfun.com/products/9590?_ga=1.213548756.1915117394.1476705504)

### GPIO Pinout

Raspberry has its GPIO over a standard male header on the board. From the first models to the latest, the header has expanded from 26 pins to 40 pins while maintaining the original pinout.

<p align="center">
<img src="../img/rpi_old_new_pin.jpg" alt="rpi" width="500">
</p>

There are (at least) two, different numbering schemes you may encounter when referencing **Pi pin numbers**:

1. **Broadcom (SoC) chip-specific** pin numbers.
2. **P1 physical** pin numbers.

You can use either number-system, but when you are programming how to use the pins, it requires that you declare which scheme you are using at the very beginning of your program. We will see this later.

The next table shows all 40 pins on the P1 header, including any particular function they may have, and their dual numbers:

<p align="center">
<img src="../img/header_pinout.jpg" alt="pin" width="500">
</p>

In the next table, we show another numbering system along with the ones we showed above: **Pi pin header numbers and element14 given names, wiringPi numbers, Python numbers, and related silkscreen on the wedge**. The Broadcom pin numbers in the table are related to RPi Model 2 and later only.

<p align="center">
<img src="../img/Pi_pin_header_numbers.png" alt="pin" width="500">
</p>

This table shows that the RPi not only gives you access to the bi-directional I/O pins, but also [Serial (UART)](https://learn.sparkfun.com/tutorials/serial-communication), [I2C](https://learn.sparkfun.com/tutorials/i2c), [SPI](https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi), and even some Pulse width modulation ([PWM](https://learn.sparkfun.com/tutorials/pulse-width-modulation) — “analog output”).

There is a useful online guide for finding the assignment and numbering of each pin, along with other guides, that you may find useful at: [pinout.xyz](pinout.xyz)

##### Analog vs. Digital

Before starting with our practise, we will revise the difference between **analog** and **digital** signals. Both are used to transmit information, usually through **electric signals**. In both these technologies, the information, such as any audio or video, is transformed into electric signals. The **difference between analog and digital**:

* In **analog technology**, information is translated into electric pulses of varying amplitude.

* In **digital technology**, translation of information is into binary format (zero or one) where each bit is representative of two distinct amplitudes.

<img src="../img/analog_digital.png" alt="pin" style="width: 300px;"/>

##### Comparison chart

||Analog |Digital|
|:------|:-------|:-------|
|**Signal**| Analog signal is a continuous signal which represents physical measurements.| Digital signals are discrete time signals generated by digital modulation.|
|**Waves**| Denoted by sine waves.| Denoted by square waves.|
|**Representation**| Uses continuous range of values to represent information.| Uses discrete or discontinuous values to represent information.|
|**Example**| Human voice in air, analog electronic devices.| Computers, CDs, DVDs, and other digital electronic devices.|
|**Technology**| Analog technology records waveforms as they are.| Samples analog waveforms into a limited set of numbers and records them.|
|**Data transmissions**|Subjected to deterioration by noise during transmission and write/read cycle.|Can be noise-immune without deterioration during transmission and write/read cycle.|
|**Response to Noise**| More likely to get affected reducing accuracy| Less affected since noise response are analog in nature|
|**Flexibility**| Analog hardware is not flexible.|Digital hardware is flexible in implementation.|
|**Uses**|Can be used in analog devices only. Best suited for audio and video transmission.| Best suited for Computing and digital electronics.|
|**Applications**|Thermometer|PCs, PDAs|
|**Bandwidth**| Analog signal processing can be done in real time and consumes less bandwidth.| There is no guarantee that digital signal processing can be done in real time and consumes more bandwidth to carry out the same information.|
|**Memory**| Stored in the form of wave signal.|Stored in the form of binary bit.|
|**Power**| Analog instrument draws large power.| Digital instrument drawS only negligible power.|
|**Cost**|Low cost and portable.| Cost is high and not easily portable.|
|**Impedance**| Low |High order of 100 megaohm|
|**Errors**|Analog instruments usually have a scale which is cramped at lower end and give considerable observational errors.| Digital instruments are free from observational errors like parallax and approximation errors.|

### Blink
We will start with a very easy example, the classic "Blink" example, later we will do the same with our Arduino and see the differences.

##### Hardware Setup
We start assembling the circuit as shown in the diagram below.

![Wiring](../img/raspi-blink-wiring.png)

##### Code
For the code we are going to use the [GPIOzero](https://gpiozero.readthedocs.io/en/stable/) library which is a on the GPIO library (https://sourceforge.net/p/raspberry-gpio-python/wiki/Home/).

1. From your laptop's terminal connect to the RPi
2. Create a folder called "code" and inside it a file called "blinker.py":
```bash
$ mkdir code
$ cd code
$ nano blinker.py
```
Note: you may not need to create a new folder everytime for the script. I.e. once you have created the folder code, you can create the scripts within the folder with nano <script name>. The command nano in this case is to open the nano editor.

3. Copy and paste this code:
```python
#!/usr/bin/env python

from gpiozero import LED
from time import sleep

led = LED(17)

while True:
led.on()
sleep(1)
led.off()
sleep(1)

```

4. Save and exit

5. Run this script with the command:
```bash
sudo python ./blinker.py
```
6. To stop the script from running press CTRL+C

7. To make the script an executable run:
```bash
$ sudo chmod u+x blinker.py
```
Now you can execute it with just this command:
```bash
$ ./blinker.py
```
8. Yay! The LED is blinking!

##### Understanding the "Blink" example

```python
#!/usr/bin/env python
```
This line is used to tell which interpreter (in our case Python) to use when the file is made into an executable.



When we use Python to control our GPIO pins, we always need to import the corresponding Python module, which goes at the top of the script:
```python
import gpiozero as gpio
```
Here, we are giving a shorter name to the module “GPIOzero”, in order to call the module through our script. This line is fundamental for every script requiring GPIO functions. If you want to import only certain classes from GPIOzero you could also sepcify the components. As an example, let's say if you are interested in only the LED:
```python
from gpiozero import LED
```
Or if want to use the Button and LED class.
```python
from gpiozero import LED, Button
```
And if we are just importing the function sleep from the [time library](https://www.tutorialspoint.com/python/time_sleep.htm), we will later use it to make the LED blink.
```python
from time import sleep
```

In the next line:
```python
led = LED(17)
```
Here we are creating a variable called `led` and we are initialising it with an object of the class [LED](https://gpiozero.readthedocs.io/en/stable/api_output.html#led). On object of the class LED to be initialised takes as a parameter the pin number to which the LED is connected to, in our case the pin number is 17 (GPIO17, not physical pin number 17).
**Note:** GPIOzero uses ONLY Broadcom (BCM) pin numbering, instead of physical pin numbering and it is not configurable, so when referring to pins in one of your scripts always use this numbering:

![Broadcom](../img/broadcom_pin_layout.svg)



```python
while True:
```
Here we are are basically asking to Python to loop forever. In fact the `while` statements loops through its code until the initial condition becomes false, in our case never.



```python
led.on()
sleep(1)
led.off()
sleep(1)
```
Here we are using two methods of the class [LED](https://gpiozero.readthedocs.io/en/stable/api_output.html#led) pf GPIOzero. `on()` switches the device on and `off` turns it off. We are calling the two functions with a 1 second interval, in fact the function `sleep` suspends execution for the given number of seconds.

### LED PWM
Use the same layout for the electronics as before.

##### What is PWM?
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (3.3 Volts for RPi and 5 Volts for Arduino) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.

For more information check out [this link](https://learn.sparkfun.com/tutorials/pulse-width-modulation).

##### Code
Repeat the same steps of "Blink" to upload the code below, this time call the file *led-pwm.py* and save it in the *code* folder that we have previously created. It's up to you to make the code executable or not.
```python
#!/usr/bin/env python

from gpiozero import PWMLED
from time import sleep

led = PWMLED(17)

while True:
led.value = 0 # off
sleep(1)
led.value = 0.5 # half brightness
sleep(1)
led.value = 1 # full brightness
sleep(1)
```

##### Understanding "LED PWM" code

The main difference here is that we are using the [class PWMLED](https://gpiozero.readthedocs.io/en/stable/api_output.html#gpiozero.PWMLED) instead of the class LED. The PWMLED class has an extra parameter that we can tweak which is `value`. `value` indicates the duty cycle of this PWM device. 0.0 is off, 1.0 is fully on. Values in between may be specified for varying levels of power in the device.

### Button

##### Hardware Setup
We start assembling the circuit as shown in the diagram below.

![Wiring](../img/button-wiring.png)

##### Code
Repeat the same steps of "Blink" to upload the code below, this time call the file *button.py* and save it in the *code* folder that we have previously created. It's up to you to make the code executable or not.

```python
#!/usr/bin/env python

from gpiozero import Button

button = Button(2)
buttonWasPressed = False # 1st flag
buttonWasReleased = False # 2nd flag

while True:
if button.is_pressed:
buttonWasReleased = False
if not buttonWasPressed:
print("Button is pressed")
buttonWasPressed = True
else:
buttonWasPressed = False
if not buttonWasReleased:
print("Button is released")
buttonWasReleased = True
```

##### Understanding "Button" code
Here we are using the [class Button](https://gpiozero.readthedocs.io/en/stable/api_input.html#button) from GPIOzero. This class has many functions and parameter, so make sure you check out the reference. Here we are using the `is_pressed` property of the class. `is_pressed` returns True if the device is currently active and False otherwise.

In this example, we also introduce the concept of flags. Flags are a way to help us keep track of the binary state of a particular thing by storing them as boolean variables (1/0 or True/False). In this case, we need to keep track of the binary state of whether the button is was pressed, and the binary state of whether the button was released.

> **Note** that we need to keep track of the _actions_ that occured, not the state of the button itself as this is already monitored `is_pressed` property of the `Button()` class.

In the code above, we use these flags to prevent the repetition of a print statement. i.e. If we did not have them, then the _"Button is released"_ statement would print repetitively until the button was pressed, and vice versa.

Flags allow us to ensure the print statement is only _printed_ on the first iteration of the loop. Every iteration thereafter will skip the print statement. This occurs until there is a change in the `is_pressed` property, at which point the respective flag is reset to `False`.


### Combining Everything

Now we challenge you to combine all the previous three scripts to create one. Make the script in order that:
* when the button is pressed one of the two leds fades to 25% of its brightness and the other one blinks once
* when the button is released the pwmled goes back to 100% brightness.

##### Hardware Setup
We start assembling the circuit as shown in the diagram below.

![Wiring](../img/practise_1.png)

##### Code Tips
Use the `when_pressed` and `when_released` properties of the [Button class](https://gpiozero.readthedocs.io/en/stable/api_input.html#button).
[Here](https://gpiozero.readthedocs.io/en/stable/recipes.html#button-controlled-led) you can find the code to control one LED with the button.

###### Acknowledgement:
<small>Based on the GPIOzero library [notes](https://gpiozero.readthedocs.io/en/stable/index.html) and [this reference](http://www.diffen.com/difference/Analog_vs_Digital) and [this intro](https://learn.sparkfun.com/tutorials/raspberry-gpio). </small>
Loading