Welcome to the first tutorial for the CR101 course. This lesson will introduce simple electricity concepts, the Arduino board, the SwissCHEESE add-on board, the CarduBlock interface, and simple programming concepts. By the end of this lesson, you should know how to operate buttons and LEDs on your Arduino board.
What is Electricity?
Electricity is the flow of electrons – negatively charged particles – around a closed circuit. A circuit must have a power source and a load – the power source is exactly that, while the load is what actually uses the electricity. A load can be anything from an LED light to a buzzer – anything that converts the circuit’s electrical energy into another form like light or sound is a load. A circuit can have multiple loads. Meanwhile, the circuit itself serves as a path for the electrons to take.
Your Arduino board can serve as a power source, allowing us to provide power to whatever components you might be using.
The CAROBOT SwissCHEESE
The CAROBOT SwissCHEESE is the piece of equipment that you are going to be using for this course – and no, it’s not edible, so don’t even try it.
The SwissCHEESE is an add-on board for the Arduino Uno which features eight output ports, 6 input ports, and two motor drivers, which make adding modules a lot easier. To make things even simpler, we’ll be using the CarduBlock graphical programming software for Arduino – a simplified version of Arduino’s ArduBlock, optimized for the SwissCHEESE.
The board has a port for the included USB connector cable – the USB end goes into the computer, and the other goes into the SwissCHEESE. You must connect the board to the computer if you want to upload any of your programs. Always remember to unplug the USB cable before plugging anything into the board, to eliminate the risk of being shocked.
Tip: Input pins are for anything sending signals into the board (eg. buttons, sensors), and output pins are for anything that the board sends signals to (eg. LEDs, motors). Don’t mix them up!
First of all, plug in your board. There are two things that you absolutely must do before trying to upload your code to Arduino .
- Go to the Tools menu at the top of the screen and select Board. Ensure that your board is set to Arduino/Genuino Uno
- Go to the Port option in the Tools menu. There should be a COM_# option (the number doesn’t matter). Make sure it’s selected before you try uploading your code.
If either of these have not been done, your programs won’t upload. On the flip side, if your programs are having upload errors, you now know the first place to look.
To open CarduBlock, go to the Tools menu and click on the CarduBlock Edu option. It should take you to a (mostly) blank screen.
The left side of the interface has several “bins” in it. From these bins, you can drag and drop blocks into the right side of the interface, which is your actual sketch/program. By attaching blocks together, you can create programs. If you need to delete blocks, drag and drop them back into the bins section.
Tip: You can right click blocks to select a “Clone” option. This helps a lot when doing repetitive code, like for blinking lights.
- Use one of the blue/black/red cables to connect an LED to the O0 pin on your board.
- Check if your sketch has a loop block. Your code needs one to function, but also can’t have more than one. If you don’t have one, open the Control bin and find it.
- From the red CAROBOT SwissCHEESE bin, drag and connect an LED block into the loop. Make sure the pin number on the block is the same as the pin you plugged your LED into!
- From the control bin, drag and drop a delay MILLIS below the LED block. This tells our LED how long to turn on for, in milliseconds. The delay is 1000 ms (1 second) by default, and you can leave it at that for now.
- Repeat the above process, putting the new LED and delay blocks underneath the ones that are already there. This time, set the status of the new LED block to OFF.
- Connect the USB cable to your board.
- Go to the top of the CarduBlock interface and hit Upload to Arduino. If you’ve done it correctly, the LED should turn on for one second, turn off for one second, and repeat that indefinitely.
CHALLENGE: Try to make your LED signal “SOS” in Morse code!
(Hints: S is three short blinks, and O is three long blinks. Your blinks should be 100 ms for a short blink, 300 ms for a long blink. Letters should be 300 ms apart and blinks should be 100 ms apart.)
Project 2: Multiple LEDS
- Use more wires to attach two more LEDs to pins O1 and O2.
- Repeat the steps used for project 1. You should have 6 LED blocks and 6 delays after you finish this.
- For the third and fourth LED blocks, set the pin number to O1. For the fifth and sixth ones, set the pin number to 02.
- Plug in the board and upload the sketch to Arduino. If done right, the three LEDs should blink one after another.
- Don’t delete the code – you’ll need it for the next project.
CHALLENGE: Try to make three LEDs blink like a traffic light! The red light should be on for a while, before turning to green for a short time. Once the green light’s time is up, it should turn yellow for a few seconds, then turn back to red.
Project 3: Buttons and if statements
Decision making is an extremely important part of programming. After all, how else is your program going to know how to react in situations where you want it to? If statements are the first step to programming decision making – essentially, they ask your program a yes/no question, and if that question’s answer is yes, it’ll perform a task. If/else blocks take this a step further by telling your program to perform a different task if the question’s answer is no, but that’s for a later time.
For now, you’ll be learning how to program a button to work with your LEDs.
- Use a wire and connect a button to the I0 pin on your board. Remember that a button is an input, so it has to be plugged into an input pin and not an output pin.
- From the yellow Control bin, find the if block and drag it into the loop.
- From the red CAROBOT SwissCHEESE bin, drag an LED block into the test slot of the if statement. This is the condition that the block will be testing for – whether or not the button is presssed.
- Take the code from project 2 and put it into the then slot of the if statement.
- Plug in your board and upload it to Arduino. The LEDs should be off at first, but will flash in sequence when you press the button.
- Change your code so the green LED will normally be ON, but once you press the button, the LEDs will blink in some order.
- Change your code one more time so that the red LED will normally be ON, but once you press the button, it changes to the green LED, then after a while it changes to the yellow LED, and then back to red .
That’s all for this tutorial! Stay tuned for more online course materials coming soon!