Transcript
Code That Light
First Look at Programming Arduino
Make sure you have the Arduino Software!
To use Arduino, you will need a program called Arduino IDE. It is the
programming environment for Arduino.
If you don’t have Arduino IDE installed:
- Go to https://www.arduino.cc/en/Main/Software
- In Download the Arduino IDE section, click the version that matches your operating system (Windows / MacOSX / Linux)
- Click “Just download” (unless you want to donate money for Arduino software development)
If needed, check out the setup instructions on the Arduino website:
- Windows
- Mac OSX
- Linux
- Portable IDE (Linux /Windows)Portable IDE does not require admin rights to the computer.
Which of these things can you do with Arduino IDE?
Parts
Do this
Find the following Parts
Part | Image | Description |
---|---|---|
Mehackit Board (or Arduino UNO) | A minicomputer you can program to control light, sound and motors. Connect sensors or switches to make interactive devices. | |
USB cable (A/B) | You upload the program you have written on the computer to Arduino through the USB cable. Arduino also gets power through it. | |
Jumper wires | You'll need plenty of thin wires of different colors when working with Arduino! | |
Breadboard | Breadboards are great for making temporary circuits. | |
LED | LEDs conduct electricity to one direction only. The longer leg is connected towards the 5V pin or later on a programmable digital pin. | |
Resistor 330Ω (or 220Ω) | Resistors resist the flow of an electric current. The value of the resistor is measured in ohms (Ω). The resistance value is coded into the colored stripes (330Ω: orange, orange, brown and gold). You can replace the 330Ω resistor with a 220Ω one. |
Why doesn't an LED work if you connect it the wrong way round?
Educator notes
- Arduino IDE is a computer program which enables you to write programs for Arduino. IDE is an abbreviation of Integrated Development Environment, and it stands for the tools with which you can write and upload code to Arduino. In Arduino IDE, programming is done by writing code. When programming Arduino you're effectively writing in C, which is a common programming language. More specifically, Arduino language consists of functions written in C, which are then called from the Arduino code.
- Mehackit exercises can also be done using the Arduino Online
Editor.
NOTE: the online editor does not work on Chromebooks - for them, you will need the Arduino Create App from the Google App Store. - It is also possible to program Arduino in a block-based environment, like BlocklyDuino. This may be a good option to check out if you are leading a shorter Arduino workshop.
Circuit
Do this
Create a circuit according to the diagram below!
- Connect the long leg of the LED to the pin 5 of your Arduino or Maker Board
- Connect a resistor between the short leg of the LED and upper row of the breadboard (marked with a blue stripe)
- Connect the upper row to a GND pin of your Arduino or Maker Board with a wire
Why did you connect the LED to pin 5 this time?
Educator notes
- It doesn't matter where exactly on the breadboard the connections are made. Some breadboards have numbered rows, but you can put the components on any row, as long as your circuits don't have gaps and you have enough space for all those parts (this is usually not a problem with beginner projects as there are only a few components).
Your First Program
Do this
Connect your board to the computer with a USB cable.
Open the Arduino programming software (Arduino IDE)
Open the Tools Menu:
- Go to Board: make sure Arduino/Genuino Uno is selected
- Go to Port: and choose the port that refers to a connected Arduino board (on Windows it could be: COM4 Arduino Uno)
Let’s program!
- define the pin 5 as an output with the
pinMode
command in the setup section - turn pin 5 (and the LED connected to it) on with the command
digitalWrite
in the loop section - Upload your program!
What happens when you upload the program to the board?
Problems?
Check your code:
- did you write big and small letters exactly like in the example?
- Did you add semicolons ( ; ) after commands?
- Does every parenthesis ( and curly bracket { have a closing pair?
Make sure your board is connected with a USB cable
Try disconnecting and connecting the cable again
close the IDE and restart it
If nothing else helps, try to restart your computer
Check Arduino Troubleshooting page
Educator notes
- If you're having problems uploading a program to Arduino, the reason is usually in Arduino's model or port settings. Make sure you've made the correct selections from menu Tools > Board and especially from menu Tools > Port.
What If Something Goes Wrong?
Do this
Open the code which lights up the LED. Change the code and click the Verify button:
Check what kind of an error message you get, when you write
pinmode
instead ofpinMode
- Remove a semicolon from the end of a line
- Remove a parenthesis: ( or )
- Remove a curly bracket: { or }
- Try to upload when Arduino is not connected
...write pinmode instead of pinMode?
...remove a semicolon from the end of a line
...remove a curly bracket
...try to upload when the board is not connected
Educator notes
The point of this section is not only to help with troubleshooting, but to make sure everyone has made at least one mistake at this point of the module! The number of mistakes made never tells how successful the final outcome of a programming project will be!
Mistakes are impossible to avoid, so getting used to making them as early on as possible is important. You can help the students in many ways, even if you don't immediately know what their problem is! Helping students to debug their code independently is pedagogically more sustainable than pointing out their mistakes directly.
Questions to help students with independent troubleshooting:
- Have you talked with your team about the problem ?
Often solutions become evident when you explain a problem to someone. - Have you checked the error messages of Arduino IDE, are there useful hints in them?
- Have you checked the code for typos and missing semicolons /
parentheses / curly brackets?
Every parenthesis and curly bracket must have a pair! - Have you checked your circuit?
It's really common to accidentally plug a wire or component to a wrong row or pin. - Search online: paste the error message or describe the problem.
It is very useful to learn to get help online with good, descriptive search parameters! Professional and hobbyist programmers alike rely on peer assistance. Pretty much every beginner question you can imagine has already been asked and answered in the Arduino Forum
Blink Blink Blink!
Do this
Write the following program in Arduino IDE
Upload the program.
- If the LED doesn’t blink:
- check for error messages, check the circuit and fix possible mistakes
- Test blinking with different
delay
values!
Code Example
What is the difference between setup and loop?
What does the command pinMode(5, OUTPUT) do?
Why do you need two delay commands inside the loop part of this program?
What does digitalWrite(3, HIGH) do?
Educator notes
- If you remove one
delay
command from this code, you will understand how the Arduino loop works. When the loop is repeated and you only have onedelay
, there will always be twodigitalWrite
commands that are executed right after each other, without a break. The LED won't have time to react between those commands and won't change its state. Delay
command means that it will stop the execution of the program for a defined time - nothing else can be done whiledelay
is active. It is not the recommended way to time operations in more advanced projects, but it is an easy concept to understand and it’s thus introduced as the first tool to time events with Arduino.- It’s impossible to control for example two LEDs independently by
using only
delay
commands. Timing events with the millis() function is introduced later on in the project modules. - There are four multiple choice questions on this page: here, the students can test how well they remember the topics covered so far in this chapter.