Skip to content

Commit 68b4947

Browse files
committed
arduino schedule + materiaal
1 parent 812ddc7 commit 68b4947

File tree

22 files changed

+463
-0
lines changed

22 files changed

+463
-0
lines changed

Diff for: _schedules/1 Herfst/schedule.yml

+20
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,23 @@ Flask:
2121
"Versie 4 [Milestone]": /milestones/versie4
2222
Screencast: /milestones/screencast
2323
Presentatie: /milestones/presentation
24+
25+
Arduino:
26+
Basics:
27+
Blink: /arduino/blink
28+
Projectvoorstel (uiterlijk dinsdag 14 november):
29+
Projectkeuze [Milestone]: /milestones/topic
30+
Projectvoorstel [Milestone]: /milestones/proposal
31+
Projectvalidatie (1 week):
32+
Uitleg projectvalidatie: /milestones/validatie
33+
Videocollege Git: /flask/lectures/git
34+
Git opzetten [Milestone]: /milestones/git
35+
Project (3 weken):
36+
"Versie 1 [Milestone]": /milestones/versie1
37+
"Versie 2 [Milestone]": /milestones/versie2
38+
"Versie 3 [Milestone]": /milestones/versie3
39+
Afronding (uiterlijk dinsdag 19 december):
40+
"Review & Cleanup": /milestones/cleanup
41+
"Versie 4 [Milestone]": /milestones/versie4
42+
Screencast: /milestones/screencast
43+
Presentatie: /milestones/presentation

Diff for: arduino/10 Blink/10 Blink.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Blink
2+
3+
## Getting started
4+
5+
Download and install the Arduino environment ([download](https://www.arduino.cc/en/Main/Software)). Make sure to get the stand-alone version, not the web plugin.
6+
7+
Start the environment you should see an empty program (called a sketch in Arduino world):
8+
9+
![Emtpy Arduino sketch](empty.png "300px")
10+
11+
You will notice the sketch already contains two function: `setup()` and `loop()`. These two functions are the minimal requirement for any Arduino sketch, like `main()` is for C-programs. The `setup()` function will run only once, directly after starting the Arduino. Once `setup()` finishes, `loop()` will be executed and repeated forever (well, until you turn the Arduino off).
12+
13+
## Exercise
14+
15+
Let’s write some code to make a LED blink. The Arduino language is essentially C++. If you know C, you will notice that a lot of it feels very familiar.
16+
17+
In the `setup()` enter the following code:
18+
19+
pinMode(13, OUTPUT);
20+
21+
As you might have guessed, this will tell pin 13 on the Arduino to operate as output pin. This means that we can use it to control, for example, a LED.
22+
23+
Now, in the `loop()` enter the following code:
24+
25+
digitalWrite(13, HIGH);
26+
delay(1000);
27+
digitalWrite(13, LOW);
28+
delay(1000);
29+
30+
The line `digitalWrite(13, HIGH)` tells pin 13 to provide a high (5V) voltage, essentially turning the LED connected to pin 13 on. The line `digitalWrite(13, LOW)` sets the voltage to 0, turning the LED off. `delay(1000)` tells the program to do nothing for 1000 milliseconds (1 sec).
31+
32+
Save your file under the name “blink”.
33+
34+
### Compile & Upload
35+
36+
To check if your program is (syntactically) correct, press the compile button:
37+
38+
![Compile (verify)](compile.png "300px")
39+
40+
Let’s upload this sketch. Make sure the Arduino is connected to a USB-port of your computer. Before you upload you have to check if the right *board* and *port* are selected:
41+
42+
Go to Tools>Board and select your Arduino model (e.g. Arduino/Genuino Uno):
43+
44+
![Select board](board.png "300px")
45+
46+
Now, go to Tools>Serial Port and select the right port:
47+
48+
![Select port](port.png "300px")
49+
50+
Once you have done this, you’re ready to upload your sketch. Press the upload button:
51+
52+
![Upload](upload.png "300px")
53+
54+
During the programming, you’ll see a LED on your Arduino blinking erratically. If all is well, this will stop after a couple of seconds and you’ll see the LED turn on and off regularly with a period of two seconds.
55+
56+
Congrats! You’ve written your first Arduino sketch.

Diff for: arduino/10 Blink/board.png

41.7 KB
Loading

Diff for: arduino/10 Blink/compile.png

19.8 KB
Loading

Diff for: arduino/10 Blink/empty.png

36 KB
Loading

Diff for: arduino/10 Blink/port.png

40 KB
Loading

Diff for: arduino/10 Blink/upload.png

19.9 KB
Loading

Diff for: arduino/11 Circuit/20 Circuit.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Connect your own LED
2+
3+
In the previous exercise you learned to program the built-in LED of the Arduino. Now let’s try to do the same with an external LED.
4+
5+
You’ll need a LED, a resistor (1kΩ will do), a breadboard and a couple of wires:
6+
7+
![Components](images/Components-Blink.jpg "300px")
8+
9+
## Exercise
10+
11+
> **Before you get started unplug/turn off the Arduino!!!** You don't want to build a circuit when the Arduino is powered. You could short circuit it and break something.
12+
13+
Build the following circuit:
14+
15+
![LED hookup](images/BB-LED.png "300px")
16+
17+
If you're not sure how to use a breadboard have a look here: \[[Breadboard wiring](index.php?dir=30+Background&file=00+Breadboard)\].
18+
19+
> Note that a LED has a positive and negative pole, if you connect a LED the wrong way around, it doesn’t do anything. \[[More information](index.php?dir=30+Background&file=20+LED)\]
20+
21+
If you turn the Arduino on and you connected the LED correctly, you should see the LED blinking.
22+
23+
Congrats! You built your first circuit!

Diff for: arduino/13 Serial/30 Fade.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Fade
2+
3+
Instead of turning the LED on and off abruptly, we’re going to make the LED fade slowly in and out. For this we can use the function `analogWrite()`. This function does not write a binary value `HIGH` or `LOW` to the pin, but a value between 0 and 255. Which allows us to write an “analog” value to the pin. If we would replace the line
4+
5+
digitalWrite(13, HIGH);
6+
7+
with
8+
9+
analogWrite(13, 20);
10+
11+
in the 'blink' program, we can see the LED blink all the same but at a much lower intensity. Note that `analogWrite(13, 0)` corresponds to `digitalWrite(13, LOW)` and `analogWrite(13, 255)` corresponds to `digitalWrite(13, HIGH)`.
12+
13+
> Note: the function is called `analogWrite`, but the output is not really analog. It is a PWM output. \[[More information](index.php?dir=30+Background&file=30+PWM)\]
14+
15+
## Exercise
16+
17+
Write a program that lets a LED fade in and out slowly. Tip: you might need a `for` loop.

Diff for: arduino/13 Serial/40 Serial.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Hello, world! Hello, world! Hello, w…
2+
3+
You might want to be able to do some debugging. Remember how useful `prinft` in C could be for that? Bad news, no `printf` for Arduino. One reason might be that the Arduino does not have a screen to print to. You can, however, send signals over the USB-connection to your computer. For this we need to setup a serial connection. This can be done by adding the line
4+
5+
Serial.begin(9600);
6+
7+
to the setup function. This starts a connection with your PC at 9600 baud (don’t worry about the connection speed, 9600 is almost always fine).
8+
9+
Now you can send messages to your computer using the command
10+
11+
Serial.print("Hello!");
12+
13+
or
14+
15+
Serial.println("Hello!");
16+
17+
The later prints a new-line at the end of the text.
18+
19+
Where do these messages wind up? They’re sent over a virtual tty-modem to your computer. You can display these messages using the _Serial monitor_ in the Arduino IDE:
20+
21+
![Serial monitor](images/serial.png "300px")
22+
23+
## Exercise
24+
25+
Write a program that displays the text “Hello, world!” endlessly (until the Arduino is turned off):
26+
27+
Hello, world!
28+
Hello, world!
29+
Hello, world!
30+
Hello, world!
31+
...
32+
33+
Make sure there is a pause of at least 200 milliseconds between every “Hello, world!”.

Diff for: arduino/14 Button/50 Button.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Button
2+
3+
Time for some input. We're going to connect a push button to our Arduino.
4+
5+
## Getting started
6+
7+
### Circuit
8+
9+
First we need to build the circuit. For this we need a resistor (around 10kΩ) and, well, a push button:
10+
11+
![Button](images/Components-Button.jpg "300px")
12+
13+
Connect them like this:
14+
15+
![Push button hookup](images/BB-Button.png "300px")
16+
17+
### Code
18+
19+
The following code prints the state of the push button to the Serial monitor.
20+
21+
void setup()
22+
{
23+
Serial.begin(9600);
24+
pinMode(2, INPUT);
25+
}
26+
27+
void loop()
28+
{
29+
int buttonState = digitalRead(2);
30+
Serial.println(buttonState);
31+
delay(20);
32+
}
33+
34+
The button pin is configured as input with `pinMode(2, INPUT)`. The state of the button (HIGH or LOW) is read with the function `digitalRead(2)`.
35+
36+
Copy this sketch and save as `button`. Compile, upload and test this sketch.
37+
38+
> What is printed when you press the button? What is printed when you release it?
39+
40+
## Exercise
41+
42+
Use a button to turn the LED on and off. When the button is pressed, the LED is on, otherwise it is off.

Diff for: arduino/15 Debounce/60 Debounce.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Debounce
2+
3+
## Exercise
4+
5+
Use a button to toggle the LED on and off. So when you press the button the LED turns on. The LED stays on until you press the button again.
6+
7+
> Does it reliably turn on or off? What happens when you keep the button pressed?
8+
9+
It is likely you will run into a problem that makes programming a bit more challenging (and more interesting) than the protected world of your computer: noise!
10+
11+
The push button is a mechanical device. When you push it, the contact plates in the button touch. The problem is, they don’t do so cleanly. You would hope the input does something like this:
12+
13+
![Idealized effect of the button on input voltage](images/bounce1.jpg "300px")
14+
15+
However, in reality it goes more like this:
16+
17+
![Actual effect push button on input voltage](images/bounce2.jpg "300px")
18+
19+
The mechanical contacts in the button bounce, making the signal jump up and down for a couple of milliseconds after pressing or releasing the button. This is called _bouncing_. You will have to take this into account when programming the toggle button.

Diff for: arduino/16 Light Sensor/70 Light Sensor.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Light sensor
2+
3+
So far you have seen `digitalWrite` (to control a LED), `digitalRead` (to read a button state), `analogWrite` (to dim a LED). There is one function ostentatiously missing: `analogRead`. We're going to use that function to read out a _light dependent resistor_ (LDR).
4+
5+
## Getting started
6+
7+
### Circuit
8+
9+
First we need to build the circuit. For this we need a resistor and an LDR:
10+
11+
![LDR](images/Components-LDR.jpg "300px")
12+
13+
Here below there is an example of how to connect them.
14+
15+
![LDR hookup](images/BB-LDR.png "300px")
16+
17+
### Code
18+
19+
The code below shows how to use `analogRead` to read the LDR.
20+
21+
void setup()
22+
{
23+
Serial.begin(9600);
24+
pinMode(A0, INPUT);
25+
}
26+
27+
void loop()
28+
{
29+
int ldrValue = analogRead(A0);
30+
Serial.println(ldrValue);
31+
delay(20);
32+
}
33+
34+
Copy this sketch and save as `LDR`. Compile, upload and test.
35+
36+
> Contrary to `analogWrite`, `analogRead` is actually analog. No PWM trickery here. The higher the voltage on pin `A0`, the higher the value `analogRead` returns.
37+
38+
> The value that `analogWrite` returns is a *10 bit* value, not 8 bits. This means that 5V corresponds to the value 1023 and 0V corresponds to 0. What would be the value of `analogRead(A0)` when the voltage on `A0` is 1V?
39+
40+
## Exercise
41+
42+
Make an automatic light switch: Use an LDR to control a LED. When it is dark enough, make the LED turn on and when it is lighter, the LED turns off.

Diff for: arduino/17 Servo/80 Servo.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Servo
2+
3+
One of the most fun things to do with the Arduino is to make robots. To make robots, we need to control motors. A type of motor commonly used in robots are servo motors.
4+
5+
Servo motors do not rotate continuously (like normal DC motors do). You can set them to a specific angle, and they will maintain that angle until instructed otherwise.
6+
7+
In this exercise you will discover how easy it is to connect a servo to the Arduino and program it.
8+
9+
## Getting started
10+
11+
### Circuit
12+
13+
You will need a 9g micro servo:
14+
15+
![Servo](images/servo.jpg "300px")
16+
17+
This is how you connect it to the Arduino:
18+
19+
![Servo hookup](images/BB-Servo.png "300px")
20+
21+
> Pay attention to the color of the wires. Red goes to the power supply (5V), brown to ground (GND), and yellow is the signal wire (connect to pin 9).
22+
23+
### Code
24+
25+
The following code lets the angle of the servo alternate between 0 and 180 degrees.
26+
27+
#include <Servo.h>
28+
29+
Servo myservo;
30+
31+
void setup() {
32+
myservo.attach(9);
33+
}
34+
35+
void loop() {
36+
myservo.write(0);
37+
delay(500);
38+
myservo.write(180);
39+
delay(500);
40+
}
41+
42+
As you can see, we include the library `Servo.h`. This library comes with the Arduino IDE. The protocol that we use to communicate with the servo is very timing sensitive. Fortunately the servo library takes care of that, so we don't have to worry about it now.
43+
44+
Controlling the servo consists of three parts:
45+
46+
1. We need to create a `Servo` variable called `myservo`.
47+
2. The line `myservo.attach(9)` activates the servo on pin 9.
48+
3. The line `myservo.write(0)` sets the angle of the servo to 0 degrees. The line `myservo.write(180)` sets it to 180 degrees.
49+
50+
Copy the code above, and run it on the Arduino. Test to see if the servo is actually moving.
51+
52+
## Exercise
53+
54+
Use either the push button or the LDR to control the servo. Make the servo turn at the push of a button, or make it react to intensity of the light.

Diff for: arduino/18 Sonar/90 Sonar.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Sonar
2+
3+
Use a Sonar to measure distances. A sonar sends a sound pulse and measures the time it takes for the echo to return. This allows us to estimate the distance of obstacles. \[[More information](http://simonpauw.com/arduino/index.php?dir=30+Background&file=40+Sonar)\]
4+
5+
## Getting started
6+
7+
### Circuit
8+
9+
You will need a HC-SR04 sonar sensor:
10+
11+
![Servo](images/sonar.jpg "300px")
12+
13+
This is how you connect it to the Arduino:
14+
15+
![Sonar hookup](images/BB-Sonar.png "300px")
16+
17+
> Pay attention to labels of the pins. VCC goes to the power supply (5V), GND to GND, 'trig' to pin 12, and 'echo' to pin 11.
18+
19+
### Code
20+
21+
Before you can start coding, you need to download the NewPing library ([dowload](http://playground.arduino.cc/Code/NewPing)---follow the instructions on the download page).
22+
23+
Once you downloaded and installed the NewPing library, restart your Arduino IDE and enter the following code:
24+
25+
#include <NewPing.h>
26+
27+
#define TRIGGER_PIN 12
28+
#define ECHO_PIN 11
29+
#define MAX_DISTANCE 200
30+
31+
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
32+
33+
void setup() {
34+
Serial.begin(9600);
35+
}
36+
37+
void loop() {
38+
delay(2000);
39+
Serial.print("Ping: ");
40+
Serial.println(sonar.ping());
41+
}
42+
43+
The NewPing library takes care of the timing protocol. The function `sonar.ping()` returns the time between the trigger sound pulse and its echo in microseconds.
44+
45+
## Exercise 1
46+
47+
The function `sonar.ping()` essentially gives you the 'distance' to an object in microseconds. We, mortal humans prefer to think in centimeters (or inches if from _that_ corner of the world).
48+
49+
Create a program that computes the distance to an object in centimeters rather than microseconds. Tip, have a look here: [https://en.wikipedia.org/wiki/Speed_of_sound](https://en.wikipedia.org/wiki/Speed_of_sound)
50+
51+
## Exercsie 2
52+
53+
Use to sonar to blink a LED when an object is getting very close (let's say closer than 10cm).

Diff for: arduino/19 Bike Light/20 Bike Light.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Bike Light
2+
3+
Create a bike light with a button and a LED: If you press the button once, the LED turns on; when you press it a second time, the LED starts blinking; when you press the button a third time, the LED turns off again.
4+
5+
Hm… this might be trickier than it seems. You need to do multiple things at once. Blink a light and check for button input. How to deal with that?
6+
7+
The function 'delay()' is getting in the way. Every time you call 'delay()' you block the Arduino. So you cannot use it for anything, not for checking a button state or anything else. This makes it hard to do multiple things at the same time.
8+
9+
### Part 1: Blink
10+
11+
Make a LED blink, but this time without using the function 'delay()'. Instead you can use the function 'millis()'. This function returns the amount of milliseconds that have passed since the Arduino was turned on.
12+
13+
### Part 2: Button
14+
15+
Write a program that checks a button every 200 milliseconds. Again, do this without using 'delay()'. When the button is pressed, toggle a LED. Make sure that when you keep the button pressed, it does not continue toggling the LED.
16+
17+
### Part 3: Bike Light
18+
19+
Now add a the third mode: blink. So when the button is pressed once, it simply turns the LED on. But when it is pressed a second time, the LED starts blinking. Press the button a third time and the LED turns off again. Again, don't use 'delay()'.

0 commit comments

Comments
 (0)