CR201 Tutorial – Class 1

Welcome to the first tutorial for the CR201 course. This tutorial will introduce a few basic electronics concepts (voltage, current, resistance, charges), circuits, the breadboard, coding with the Arduino IDE, and using decision making/potentiometers with the IDE.

If you’ve already read the previous tutorials, it’s worth saying that this course is very unlike the previous one. Instead of using blocks, we will be actually typing out code into the IDE. This means that it’ll likely be harder to visualize what your code will do, and you will be liable to make many syntax errors if you don’t know what you are doing. 

Still interested? Well, let’s get into it.

The Basics of Electricity

Simply put, electricity is just a type of energy. In physics terms, however, electricity is the flow of charges, with tiny particles called electrons and protons carrying these charges. Protons have a positive charge, and electrons have a negative one. When those particles start moving, they become an electric current, or electricity

The amount of moving electric charge is called the current, which is measured in Amperes [A] and the willingness of the charges to move is called potential difference and is measured in Voltage [V].

But what reason do the charges have to move? The answer to this lies in the charges themselves – like magnets, opposite charges attract and same charges repel. Because of this, electrons want to move closer to protons but want to move away from other electrons, and vice versa. To let them move, we need a medium for them to move through. Anything that electrons can freely move through, like metal, is called a conductor. Anything that they cannot move through, like plastic, is called an insulator.

Electricity can only exist if there is a path for charges to flow through. This path is called a circuit. In order for electricity to flow through the circuit, it must be closed, meaning that it cannot have a gap or break in it. If there is a gap produced by an insulator, electricity will fail to flow through the circuit.

CR201 Tutorial - Class 1

In the diagram above, you can see that the electrons move towards the positive end of the battery because they are attracted to the protons and repelled from the other electrons.

Also important is the concept of resistance, or the opposition to the flow of charge. Resistance is measured in ohms (Ω). Keep in mind that electricity will always take the path of least resistance.

Ohm’s Law gives a relationship between voltage, current, and resistance (R). The formula is: V= I * R.

Circuits and Arduino

We can use our Arduino board as the battery for our circuit. One wire will provide power to our breadboard, while the other will “ground” our circuit, providing a route for the electricity to travel back to the board.

The breadboard itself looks like a white board with many holes in it. Pins for wire and components go into these holes. Every hole is connected to the rest of the holes in its row and column, allowing electricity to travel along them.

Exercise 1: Simple Circuit

CR201 Tutorial - Class 1

We do not need the SwissCHEESE board for this task. If it’s attached to your Arduino board, remove it and put it aside for now.

  1. Plug a wire into the 5V pin on the Arduino, and plug the other end into the red rail on the breadboard. This is your power source.
  2. Take another wire and plug it into the GND pin on the Arduino. Plug the other end into the blue rail on the breadboard. This is your ground wire, which carries electricity back into the Arduino to complete the circuit.
  3. Set up a wire, LED, and 330Ω resistor in the formation pictured above. Electricity goes from the power rail to the wire, through the LED (powering it), and through the resistor to the ground rail. The resistor is there to
  4. Once you are done, plug in your board. Your LED should turn on. This is an example of a simple circuit.

Task: Find the current of the circuit. Remember Ohm’s law!

Beyond Blocks

For the next exercise, you will actually be writing out code. Keep the wiring from the last exercise, but instead of having a wire going from the power rail to the LED, make it so that that it goes from pin 11 to the LED. In addition, disconnect.

Like the last tutorials, make sure that your Board and Port are selected in the Tools menu at the top of the IDE. In addition, make sure you disconnect your USB cable while doing any wiring.

It is important to note that this is not CarduBlock. The Arduino IDE uses C-Programming, and it is not forgiving when it comes to syntax. If you make mistakes, the IDE will not allow you to upload your code.

Here is the general rule: each function name must be followed by a pair of round brackets () and curly brackets {}. Within the curly brackets are the codes to be executed inside the function. Each line, other than the first line with the function name must end with a semicolon (;).

Exercise 2: Blinking

Once you’re done wiring your board, input the following code into the IDE. Follow it exactly!

void setup () {

pinMode(11, OUTPUT);

}

void loop () {

digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
delay(1000);

}

A bit of explanation:

  • void setup() is the setup function, for anything that has to be set up before the code is run. It only needs to run once.
  • pinMode(11, OUTPUT); declares pin 11 on the Arduino as an output pin – meaning that it can now be used for things like LEDs, and cannot be used for input devices like buttons. As this is “setting up” the board/code, pins are declared in the setup function.
  • void loop() is the loop function, where most of the code is usually located. Anything in here will repeat indefinitely.
  • digitalWrite is used to set a digital (only two states) output to high or low. In the first instance of this command, it would set pin 11 to a HIGH state.
  • delay tells the code above it how long to run for, in this case 1000 milliseconds.

Plug in your board and upload the code to the Arduino. If you’ve done this right, your LED will now turn on for one second, turn off for one second, and repeat this indefinitely.

Exercise 3: Return of the SwissCHEESE

Remember the SwissCHEESE from the previous tutorials? Well, we’ll be using it again this class. The breadboard can get pretty confusing to wire sometimes, which is why we can use the SwissCHEESE to make life a little easier.

In order to use the SwissCHEESE with the Arduino IDE, you must include #include <CAROBOT_SwissCHEESE.h> at the top of your code. This will import the SwissCHEESE library and allow the code to use it.

Now, instead of needing to specify pin 11 when programming, we can just plug the LED from the SwissCHEESE kit into O0 and specify that instead. After attaching the SwissCHEESE onto the Arduino board, repeat the previous task, but include the SwissCHEESE library and replace all instances of 11 with O0.

CHALLENGES:

  1. Make the LED turn on for 2 seconds and off for 1.5 seconds.
  2. Program the LED to flash morse code for SOS. Use 100 millisecond (ms) for dot, 300 ms for dash, 100 ms for pulse, and 300 ms between letters.

Hint: If you’ve already done these in CarduBlock, think about how you did it last time, and try to find the code equivalents to the blocks you used. 

Variables and if/else

If you’ve read the previous tutorials, you should already know what a variable and an if/else statement is. If you need a little refresher, an if/else statement lets the computer make decisions, while a variable saves an integer value under a name for future use.

However, we’ll now be learning how to use those with the Arduino IDE, instead of just with CarduBlock.

Variables are declared by stating the type of variable, followed by its name, and finally followed by an “equals” statement defining its value.

(type) (name) = (value);

For example, if you wanted to create an integer variable with the name “number” and the integer value of 12, it would be declared like this:

int number = 12;

On the other hand, an if statement would be programmed like this:

if (condition) {

}

else {

}

Anything in the curly braces is the code to be run when the condition is met and when the condition is not met, respectively.

Exercise 4: Using the LED with the Button

Now, we are going to add some inputs. The simplest input is a push button. When the button is pushed, it closes a circuit, letting the Arduino read this and react accordingly. For the SwissCHEESE button module, when the button is pressed, the signal becomes HIGH, and when the button is not pressed, the signal is LOW.

First, connect a button to port “I0” on the input ports of the SwissCHEESE shield. Then, write the following code in your program:

#include <CAROBOT_SwissCHEESE.h>

void setup () {

pinMode(O0, OUTPUT);
pinMode(I0, INPUT);

}

void loop () {

int state = 0;
state = digitalRead(I0);
if (state == HIGH) {

digitalWrite(O0, HIGH);
delay(1000);

}

else {

digitalWrite(O0, LOW);

}

}

Upload your program. You’ll know if you are successful when the LED turns on for one second whenever you press the button.

CHALLENGES:

  1. Modify the program so the LED is normally ON but turn off once you press the button.
  2. Add more LED modules to your assembly and write a “traffic light” program that will normally display the red light. Once you press the button, it will turn the green light on, then switch to yellow after a bit, and finally switch back to red.

Digital vs. Analog

An important concept to understand while using Arduino is the difference between digital and analog. A digital device is one that only has two possible states – high/low, on/off, 1/0, etc. Buttons and LEDs are common examples of digital devices. On the other hand, analog devices refer to things that have more states than just on and off. Temperature levels, control knobs, light levels – they all have a continuous range of values between on and off.

When coding, it is important to remember which is which. For example, you might use digitalWrite if you want an LED to turn on or off, but if you want to be able to adjust its brightness, you need to use analogWrite instead. Similarly, for a button you may use digitalRead to get its input, but for something like a potentiometer or a sensor you would need analogRead instead.

Exercise 5: The Potentiometer and LED Brightness

A potentiometer is a device that provides an analog input signal as mentioned above. The potentiometer is really just a variable resistor that changes resistance as you turn the knob. The Arduino then takes that signal and interprets it into something a program can use.

But there is a problem: the input value has a range of 0-1023 but the output value has a range of 0-255. If we use the input value directly, the program will overflow. In this case, we need to do some math to convert the input range to the same size as the output range using the following formula:

input/1023 = output/255

Your eyes may be rolling at the prospect of doing math, but fear not! Arduino already does the math for you in the form of a function called map. An example of this is:

reading = map(reading, 0, 1023, 0, 255);

The above code takes the value of “reading”, convert it from 0-1023 to 0-255, and then saves the answer back to “reading”.

So, without further ado, here’s the code you need to put into Arduino:

#include <CAROBOT_SwissCHEESE.h>

void setup () {

pinMode(O0, OUTPUT);
pinMode(I0, INPUT);

}

void loop () {

int reading = 0;
reading = analogRead(I0);
reading = map(reading, 0, 1023, 0, 255);
analogWrite(O0, reading);

}

If you are successful, you should be able to brighten and dim the light by turning your potentiometer different ways.

CHALLENGE: Combine and modify the program you wrote above and the one in Exercise #3 to control the blinking speed with a potentiometer, instead of the light having a fixed 1 second blink.

That’s all for this tutorial! Hope you continue to read onward!