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: 


Which of these things can you do with Arduino IDE?

Parts

Do this

Find the following Parts

PartImageDescription
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 wiresYou'll need plenty of thin wires of different colors when working with Arduino!
BreadboardBreadboards 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

Transcript

Arduino must be programmed or it doesn't work. So how do you do that? Have a look at the Arduino programming interface now. Fun fact: a shorter name for such an application is IDE, which stands for integrated development environment. I'll use the short name IDE for this interface from now on. First, connect your board to the computer. Now you have to make sure the computer understands there's this board connected to it. Go to the Tools menu and check that the correct board is chosen - Arduino Uno or Genuino - and then make sure that the correct port is chosen, too. The name of the port depends on your computer but it should say Arduino-something here in the list. Here's the area where all the code is written. It's not empty: there's something already there, even some instructions. As it says, everything in here will be done just once. This line with two slashes is a comment. Comments are good for making notes inside the code. If this text bothers you, you can just remove it. The setup part is meant for setting some basic things up, so I'll do just that. pinMode... the pinMode command tells the board that there's a device connected to it. Otherwise it doesn't know it. You have to tell two things here: which pin the LED is connected to - pin 5 - and also, is this pin going to be an output or input. It's going to be an output, which means that the LED will be turned on later in the program. Make sure you write things exactly like this. I'll explain soon why it's important. This part went inside the setup of the program because it's done just once. The things you write inside the loop section will be repeated over and over again, from top to bottom. That's why it's called a loop. I'll write digitalWrite, and pin 5, and HIGH with big letters. I need to be exact and tell the board it really is that pin number 5 and it has to be turned on. And that's the information that went inside the parenthesis. Next: upload the program to the board with this button, which also checks your code is written correctly. Wow! the LED's on! Forever! Your turn: go ahead and try this too.

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?

Transcript

The nasty part about programming when you're just starting is that you have to type things exactly right or the code won't work. But believe me, you will get used to writing code and these little details become your second nature in no time at all. And remember everyone makes mistakes, so it's not so serious. I'll show you a few things to look out for. Note this semicolon at the end of the line. In many many programming languages the semicolon tells the computer that this command is finished. The code doesn't work without it, and it's so easy to forget it. After commands like digitalWrite and pinMode there's always parentheses and some values inside them. Forgetting parentheses is an easy way to mess things up. Fun fact: those values are called arguments and they can be numbers, sometimes words like output, or something else. We use those arguments to make sure commands do what we want. Like with digitalWrite: it's pin 5, not 10 or 11, and it must be turned on and not off. You wrote OUTPUT with big letters and pinMode with a capital letter M. Watch out for the big and small letters. Computers think it's a completely different word if you write output like this, instead of capital letters. Curly brackets can be tricky in the beginning. Don't delete them from setup or loop blocks. They mark where these parts of blocks start and where they end. A good rule of thumb is: whenever you have an opening curly bracket, like this, you also need a closing curly bracket somewhere. They always come in pairs. You can check your code by clicking the verify button and if there's a mistake... ooh there's a mistake!... there's a warning down here and there's also a hint where the mistake might be located. The line where the mistake is - somewhere close there - is highlighted, and there's a hint: a semicolon is expected. All right, so I go around - here seems to be a missing semicolon. And now when I click verify... wohoo! No more warnings! Yay!

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 of pinMode
  • 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 


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


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 one delay, there will always be two digitalWrite 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 while delay 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.