STEMTera™ Breadboard was created with the help from thousands of backers on Kickstarter and distributors like SparkFun Electronics, LittleBird Electronics and ITEAD Studio. Without them, this awesome piece of work will not be available today.
Images and illustrations were drawn by JP Liew, sourced from open source projects, SparkFun SIK Guide, Wiki Media and Fritzing.
January 1st, 2017 - JP Liew
Contents
** is your guide to help you begin using the STEMTera™ Breadboard. This guide contains all the information and example circuits you will need to learn how to use STEMTera™ Breadboard and basic electronics.
1. Introduction
- What is Arduino?
- What is STEMTera™ Breadboard?
- Hardware
- Software
- Sketches
- Installing Arduino IDE
- Arduino IDE
- Connect the STEMTera™ Breadboard
- Board Selection
- Port Selection
- Uploading Your First Sketch
2. Getting to Know the STEMTera™ Breadboard
3. Introduction to Electronics
- Atoms
- Electric Charge
- Electricity
- Current
- How to Measure Current
- Resistance
- How to Measure Resistance
- Voltage
- How to Measure Voltage
- Common Electronic Components
- Common Electronic Components
4. Arduino Exercises
1. Introduction
What is Arduino?
Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online.
What is STEMTera™ Breadboard?
The STEMTera™ Breadboard is a smart electronic breadboard with an Arduino UNO R3 compatible built inside. This 2 in 1 breadboard is specifically designed to solve common issues when using Arduino with a separate breadboard especially in the lab or classroom.
The STEMTera™ Breadboard is capable of taking inputs, for example the push of a button, and translate that information to trigger different types of outputs, for example turn on a light or motor. With these capabilities, the STEMTera™ Breadboard can be used to build interactive physical systems by the use of software and hardware that can sense and response to physical world.
Making mechanical LEGO® projects with the STEMTera™ Breadboard is easy because the bottom cover of the STEMTera™ Breadboard is LEGO® compatible.
Hardware
The STEMTera™ Breadboard has 14 digital input/output pins (6 of which can be PWM outputs), 6 analog inputs, a USB connection, a power jack, and a reset button. These input/output pins are just like human fingers. With a properly written software plus hardware, they can be used to touch, sense and feel the physical environment.
Just like an Arduino UNO, the STEMTera™ Breadboard has two micro-controllers soldered on the PCB (Printed Circuit Board). The ATMEL’s ATmega32U2 and the ATmega328P. The ATmega32U2 serves as the bridge for USB communication between the computer and the ATmega328P.
The STEMTera™ Breadboard also exposed all the ATmega32U2’s pins on the breadboard’s tie points, thus providing 21 extra digital input/output pins that has the native capability to communicate with the computer via the USB port (advance topic).
Software
The STEMTera™ Breadboard by itself is just a piece of physical hardware. You need to tell it what to do by sending a set of instructions to the micro-controller on the board. This set of instructions is normally referred to as program, code or software. There are a few ways to send the program to the STEMTera™ Breadboard, one of the most popular way is by using the Arduino Integrated Development Environment (IDE).
The Arduino IDE contains a text editor for writing code, a message area, a text console, a toolbar with buttons for common functions and a series of menus. It connects to the STEMTera™ Breadboard hardware to upload programs and communicate with them.
Sketches
Programs written using Arduino IDE are called sketches. These sketches are written in the code editing area of the Arduino IDE and can compiled into a set of micro-controller instructions that can be uploaded into the STEMTera™ Breadboard.
Installing Arduino IDE
In order to start writing sketches for your STEMTera™ Breadboard, you will need to install the latest version of the Arduino IDE first from Arduino’s website. Using an internet-enabled computer, open up your favourite Internet browser and type the following URL into the browser’s address bar:
www.arduino.cc/en/main/software
This URL will display a page with download choices based on various Operating Systems.
Click and download the appropriate Operating System installation package for your computer. As soon as the download is completed, if you are familiar with installing software and drivers, please proceed with the installation, otherwise open up your browser and type the following URL into the browser’s address bar to read SparkFun’s Installing Arduino IDE guide.
learn.sparkfun.com/tutorials/installing-arduino-ide
After completing the installation, execute the Arduino IDE on your computer. The user interface of Arduino IDE is divided into 9 sections. Navigate around the user interface to get familiar with the Arduino IDE.
Connect the STEMTera™ Breadboard
After getting use with the user interface of Arduino IDE, you can now connect the STEMTera™ Breadboard to your computer. You will need a Micro USB cable for the connection.
First, connect the larger end of the Micro USB cable to your computer. Second, connect the other smaller end of the Micro USB cable to the STEMTera™ Breadboard. The green LED light on the STEMTera™ Breadboard will turned on and different types of Operating System might also produce a sound indicating that a USB device has being plugged into the computer.
On a Windows computer, if you are first time connecting the STEMTera™ Breadboard, a dialog window will pop up telling you that there is a new device detected. Windows will search for the device driver installed previously and create a serial port on your computer. If you wish to know the name of the serial port, type
Win + r
In the open dialog box, type
devmgmt.msc
then click OK. Device Manager will be launched and display a list of devices connected to the computer. Double click Ports (COM & LPT)
and the name of the STEMTera™ Breadboard’s serial port should look like Arduino Uno (COMx)
On a Mac computer and Linux computer, the STEMTera™ Breadboard is automatically detected. To see the status of this automatic detection, open a terminal screen and type
dmesg + Enter
The last few lines will tell you the name of the serial port created. The serial port name for Mac and Linux computer will normally start with /dev/tty
.
Note down the name of the serial port as you will need this to configure the Arduino IDE later.
Board Selection
After connecting the STEMTera™ Breadboard into the computer, you need to configure the Arduino IDE to know the type of board that you intend to use. This configuration is located at the Tools
menu.
Move the mouse over to the Tools
menu and click the Tools
menu once. When the Tools
menu appears, move the mouse to the Board
sub menu and another sub menu with a list of boards will appear. Click the Arduino/Genuino Uno selection. Arduino IDE is now configured to use the STEMTera™ Breadboard as the choice of development board.
The STEMTera™ Breadboard is 100% compatible with Arduino UNO but it is not listed in the Arduino Software’s Tools menu. Select “Arduino/Genuino Uno” instead.
Port Selection
The Arduino IDE also need to know the communication port of your STEMTera™ Breadboard. At the Tools -> Port
sub menu, select the appropriate port of your STEMTera™ Breadboard. Different Operating Systems will show different Port
names.
On a Windows Operating System, the Arduino IDE will show the STEMTera™ Breadboard as Port: "COMx (Arduino/Genuino Uno)"
On a Mac OS, the Arduino IDE will show the STEMTera™ Breadboard as Port: "/dev/tty.usbmodem"
On a Linux Operating System, the Arduino IDE will show the STEMTera™ Breadboard as Port: "/dev/ttyACM0"
Once the port has been configured, you can proceed to upload your first sketch.
Uploading Your First Sketch
Now, everything has been configured properly and you are ready to upload your first sketch. The quickest way to try your first sketch is to run the Blink
example.
Click File -> Examples -> 01.Basics -> Blink
The Blink
sketch will be loaded into the Arduino IDE.
Click Upload, and both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the computer is uploading the sketch to the STEMTera™ Breadboard.
When the sketch uploading is completed, both yellow LEDs will stop blinking. The message area of the Arduino IDE will display Done uploading
, indicating upload is completed.
At the same time, because the Blink
sketch instructed the STEMTera™ Breadboard to blink, you can see the red LED starts to blink.
Your first Blink
sketch is now successfully being executed by the STEMTera™ Breadboard.
2. Getting to Know the STEMTera™ Breadboard
Hello STEMTera™ Breadboard
The functions of the STEMTera™ Breadboard can be easily divided into different sections below.
USB
- This port is used to receive power and communicate with the computer.
7-12VDC Power In
- The STEMTera™ Breadboard can also be powered from this DC jack.
Port B, Port C & Port D
- These are the digital input/output pins of the ATmega32U2 micro-controller.
Status LED
- The green LED indicates power on, both yellow LED indicate communication and red LED is controlled by D13 of the Arduino.
Power
- This section provides external components with 3.3V and 5V DC power.
Power Rails
- When connected to 3.3V or 5V DC, these rails can be used to supply power to external devices.
Digital & Analog Pins
- These pins are input/output used for interfacing with external passive or active components.
Tie Points
- These tie points are used for joining components. 5 tie points (ABCDE) are joined in a group. (FGHIJ) is another group.
Tie Points
There are a total of 634 tie points in the STEMTera™ Breadboard, divided into 3 major group.
The first group is power rails. There are 2 power rails located on the left and 2 on the right of the STEMTera™ Breadboard marked with orange color in the diagram. Each power rail has 30 (1x30) tie points internally connected together in a straight line. These power rails are normally used for distributing the power supply.
The second group is just normal tie points. These tie points marked with green color in the diagram are commonly used as joints. The formation of these tie points is 1x5, for example 1->ABCDE
is one group of 1x5 tie points internally connected together. 1->FGHIJ
is another group of 1x5 tie points.
The last group is a formation of 1x2 tie points connected to the micro-controllers’ pins inside the STEMTera™ Breadboard. They can be seen marked with black color in the diagram.
Inside the holes of the tie points are spring clips. These spring clips manufactured in the formation of 1x2, 1x5 and 1x30. When you insert a component inside a tie point, a resistor for example, the leg of the resistor will be held by the two arms of the clip inside the tie point, thus making a mechanical connection as shown on the left diagram. Because the clips are made from copper or stainless steel, electricity can flow from the resistor to the clip.
By using mixtures of components connecting to the tie points, complex electronic circuits can be then formed.
3. Introduction to Electronics
Atoms
We all like to take things apart, and always find something smaller inside, for example, if we take a small remote controlled car apart, there are motor, gears and wires inside. If we continue breaking the motor into smaller and smaller pieces, eventually we will find that all matter is made from different types of atoms, i.e., everything is made of atoms . A single atom is about 100,000 times thinner than a human hair and can only be seen using a powerful electron microscope.
Most atoms have three different subatomic particles inside them, protons, neutrons and electrons. The protons and neutrons are packed together forming a nucleus in the center of the atom. Each proton in the nucleus of an atom has a tiny positive charge, and this effectively means the nucleus is a big clump of positive charge. The electron which has a negative charge, is so much smaller than the proton and orbit around the neucleus of the atom. Atoms are electrically neutral because they have the same numbers of protons and electrons.
Electric Charge
Sometimes atoms can gain or lose electrons. Loss of electrons leaves an atom with a net postitive charge and gain of electrons leaves an atom with a net negative charge. The presence of this electric charge, either positive or negative, produces an electric field. These type of charged atoms is called an ion. In a battery, the positive side has positive ions and negative side has negative ions.
Electricity
Electrons being negative in nature, are attracted to positive charged. By making use of this nature, electrons can be made to move from one atom to another. When electrons move between the atoms, a current of electricity is created. In certain gases and liquids, atoms with extra protons can also flow and creates electric current too.
Current
Despite current is the flow of electrons (negative) through a conducting medium to positive charged atoms, current flow is often or normally defined as Conventional current, where it flows from positive terminal to the negative. In this guide, we will be referencing Conventional current as current.
Current can only flow when there is an energy source that produces voltage. Without this energy source, electrons move randomly and fairly evenly within a wire, and current cannot flow. Energey source creates pressure that drives electrons in a single direction thus creating current flow.
With just energy source alone is not enough to make current flows. The circuit with combination of the energy source (in our example is AA batteries) and load (in our example light bulb) must form a closed loop, a conducting loop which electrons can flow, providing energy to the load connected to the circuit.
The unit for electric current is Ampere (amp) with a unit symbol of A. When working with formula, the symbol for current is I.
Formula, I = V ÷ R ( Current = Voltage ÷ Resistance) - Ohm’s law.
How to Measure Current
Set the multimeter’s knob to the highest possible current setting first, and also make sure that the current fuse rating of the multimeter is high enough to support the current you wish to measure. In the above example, the rough estimated current is only a few milliamp (1 milliamp is 1/1000 amp) based on Ohm’s law, 3V ÷ 1000 Ohm ( 2 x 1.5V battery ÷ 1000 Ohm resistor). Therefore, setting the knob to 200mA setting is a good start point.
In order to measure the current, we need to break the circuit’s loop and let the current flow into the multimeter. The above example shows current flow from the batteries passing through the 1K (1000 Ohm) resistor and into the multimeter then back to the negative terminal of the bottom battery. This forms a closed loop. The reading is 0.003A = 3 mA.
Formula,
I = V ÷ R
I = 3V ÷ 1000Ω
I = 0.003A
Resistance
Electrons move through a conductor when electric current flows. All material impede flow of electric current to some extend. Some materials hold their electrons very tightly, and due to this, electrons will not move through them very well. These materials are called insulators. Rubber, plastic, cloth and dry air are good insulators. Materials that allow many electrons to flow freely are called conductors, examples are, copper, silver, aluminium, hydrochloric and sulphuric acid and saltwater.
The characteristic of materials impede flow of electric current is called resistance. Insulators have high resistance and conductors have low resistance and resistance is measured in Ohms (Ω).
How to Measure Resistance
Turn the multimeter’s knob to resistance range. Touch both RED probe and BLACK probe together, the reading on the multimeter should be 0 or near 0. Touch both ends of the material to be measured with the two probes from the multimeter. Check the reading. In the example above, the resistance for the 1K Ohm resistor is 1K.
Voltage
Voltage is the pressure or force from an electrical circuit’s power source that “pushes” charged electrons through conductors. The greater the voltage, the greater is its ability to “push” the electrons through a given circuit. Voltage is also called potential. The difference in voltage between any two points or terminals in a circuit is known as the potential difference, commonly called the voltage drop. There are two different voltages, AC (alternating current) voltage and DC (direct current) voltage. AC flows in both directions and is commonly used in electrical wall plug. DC flows in one direction and is commonly used by battery operated circuits. By using a special circuit, AC can be coverted into DC and vice versa. In this guide, we will only be learning circuits in DC.
The unit for voltage or potential difference is volt with a unit symbol of V. When working with formula, the symbol for voltage is V or E.
Formula, V=IR or E=IR ( Voltage = Current x Resistance) - Ohm’s law.
How to Measure DC Voltage
By using a multimeter, turn the mulimeter’s knob to the voltage range. Touch the RED (positive) probe on one of the point and the BLACK (negative) probe to the other point. The display tells you the potential difference (voltage) of the two points in Volts.
In the above example, two 1.5V AA batteries connected in series has a voltage of 3V.
If your multimeter is not an auto-ranging multimeter, always set the range to the highest first, then work downwards until the desired reading is achieved.
Common Electronic Components
Jumper wire, also known as DuPont wire is an electrical wire with pin at each end, which is normally used to interconnect the components on a breadboard or other prototype circuit without soldering.
Resistor is a passive two-terminal component that implements electrical resistance. In electronic circuits, resistors are used to reduce current flow, divide voltage, bias active components and many more.
Capacitor is a passive two-terminal component that stores electrical energy in an electric field, which are normally used in electronic circuits for blocking direct current while allowing alternating current to pass. In analog filter networks, they smooth the output of power supplies and stabilize voltage.
LED (light-emitting diode) is a two-terminal semiconductor light source, which will emits light when a suitable voltage is applied to the leads. LEDs are very energy efficient and have a longer lifetime over incandescent light sources.
Diode is a two-terminal component that conducts primarily in one direction, It has very low resistance to the flow of current in one direction and extremely high resistance in the other.
Push button is a simple switch mechanism that allows current to flow when pressed.
Common Electronic Components
Piezo buzzer is an electro-acoustic transducer that will transform AC voltages to sound pressure. In electronic circuits, it is normally driven with high-low pulses from and output pin or PWM (pulse width modulation) pin.
A photoresistor or light-dependent resistor (LDR) is a light-controlled variable resistor. The resistance of a photoresistor decreases with the increasing light intensity.
A temperature sensor is a sensor that converts temperature into electrical signal. There are many types of temperature sensor that can output different types of signal. The TMP36 is one of the most common temperature sensor what coverts temperature into voltage, and will produce an output of 10mV per Celsius.
A transistor is a semiconductor device used to amplify or act as a switch for electronic signals. Common transistors have 3 leads, however some special transistors do have 4 or more leads.
A potentiometer or pot is a three-terminal resistor with a sliding or rotating contact that can be used as a variable voltage divider or as a variable resistor.
A DC motor is an electro-mechanical machine that converts DC electrical energy to a mechanical energy. DC motor works on a principal, when a current carrying conductor is placed in a magnetic field, it experiences a torque and has a tendency to move.
4. Arduino Exercises
Exercise 1 - Blinking LED
LEDs (light-emitting diodes) are small and energy efficient lights that are used in many applications. In this exercise we will start with one of the easiest LED blinking circuit.
In this excercise, PIN3 is used to drive the LED on and off. The 220Ω resistor act as a current limiter to limit the current flowing through the LED. Depending on the specifications, LEDs need about 8mA to 20mA of current to light up.
- Connect a JUMPER WIRE to GND and 33J.
- Connect a JUMPER WIRE to PIN3 and 38J.
- Connect a 220Ω RESISTOR to 34H and 38H.
- Connect the anode (longer) pin of the RED LED to 34G and cathode (shorter) pin to 33G.
Write and Upload the Blinking LED Sketch
In order to execute the sketch for this exercise, launch Arduino IDE then
Click File -> New.
A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty setup()
and loop()
that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.
void setup() {
pinMode(3, OUTPUT);
}
void loop() {
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3, LOW);
delay(1000);
}
Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter Excercise_1
into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.
Understanding the Blinking LED Sketch
From the circuit, we know that PIN3 of the STEMTera™ Breadboard is connected to the 220Ω resistor and the Red LED. In order to turn on the LED, we need to instruct the STEMTera™ Breadboard to set PIN3 to OUTPUT in the setup()
function.
pinMode(3, OUTPUT);
The pinMode()
function shown above takes two parameters. The first parameter is the pin number and the second parameter is the mode of the pin. The above code instructs the STEMTera™ Breadboard to set PIN3 as OUTPUT.
Once PIN3 is configured as OUTPUT, we can then instruct the STEMTera™ Breadboard to either set PIN3 HIGH or LOW.
digitalWrite(3, HIGH);
The digitalWrite()
function shown above takes two parameters. The first parameter is the pin number and the second parameter is HIGH or LOW. When a PIN is set to HIGH, the PIN will OUTPUT 5V. When a PIN is set to LOW, the PIN will OUTPUT 0V (no voltage and connected to GROUND).
In order to set an ON time or OFF time, we can add a delay in between ON and OFF using the following line of code after setting a PIN HIGH or LOW.
delay(1000);
This line of code instruct the STEMTera™ Breadboard to delay for 1000 ms (milliseconds).
When ON, DELAY, OFF, DELAY are executed in a loop, the result of this exercise is the LED blinking in 1000ms ON and 1000ms OFF pattern.
Exercise 2 - Pulse Width Modulation (PWM)
Pulse width modulation is a modulation technique used to encode a message into a pulsing signal. Even though this is a modulation technique for encoding information for transmission, it is widely used in controlling the brightness of an LED and the speed of a motor.
A PWM signal consists of two main components that define its behavior: frequency and duty cycle. The frequency determines how fast the PWM completes a cycle (i.e. 1000 Hz frequency would be 1000 cycles per second). The duty cycle describes the amount of on-time as a percentage of the total time of a cycle.
In digital electronics, when a signal is at 5V, we call this ON, and when a signal is at 0V, we call this OFF. From the above image, the first signal is shown as 10% duty cycle, which means its ON time is 10% of a cycle and its OFF time is 90% of a cycle. The second signal is 30% duty cycle because its ON time is 30% of a cycle and its OFF time is 70% of a cycle. A 100% duty cycle means the signal is ON all the time.
By varying the duty cycle of a signal, we can control the percentage of ON time for a circuit, hence indirectly control, for example, the brightness of an LED. 100% duty cycle will have the LED turned ON in full brightness and a 50% duty cycle will have the LED turned ON in half brightness.
Not every PIN on the STEMTera™ Breadboard can be used for PWM. Only PIN with a label ~ can perform as a PWM pin. PIN 3, 5, 6, 9, 10 and 11 only.
For this exercise we will be using the same circuit as the one we used for exercise 1. If you have already done exercise 1, just skip to Write and Upload section.
- Connect a JUMPER WIRE to GND and 33J.
- Connect a JUMPER WIRE to PIN3 and 38J.
- Connect a 220Ω RESISTOR to 34H and 38H.
- Connect the anode (longer) pin of the RED LED to 34G and cathode (shorter) pin to 33G.
Write and Upload the PWM Sketch
In order to execute the sketch for this exercise, launch Arduino IDE then
Click File -> New.
A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty setup()
and loop()
that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.
void setup() {
pinMode(3, OUTPUT);
}
void loop() {
analogWrite(3, 255); // 255 is 100% duty cycle
delay(200); // wait for 100ms
analogWrite(3, 127); // 127 is 50% duty cycle
delay(200); // wait for 100ms
analogWrite(3, 64); // 64 is 25% duty cycle
delay(200); // wait for 100ms
analogWrite(3, 32); // 32 is 12.5% duty cycle
delay(200); // wait for 100ms
analogWrite(3, 16); // 16 is 6.25% duty cycle
delay(200); // wait for 100ms
analogWrite(3, 0); // 0 is 0% duty cycle
delay(200); // wait for 100ms
}
Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter Excercise_2
into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.
Understanding the PWM Sketch
From the circuit, we know that PIN3 of the STEMTera™ Breadboard is connected to the 220Ω resistor and the Red LED. In order to turn on the LED, we need to instruct the STEMTera™ Breadboard to set PIN3 to OUTPUT in the setup()
function.
pinMode(3, OUTPUT);
The pinMode()
function shown above takes two parameters. The first parameter is the pin number and the second parameter is the mode of the pin. The above code instructs the STEMTera™ Breadboard to set PIN3 as OUTPUT.
Once PIN3 is configured as OUTPUT, we can then use analogWrite()
as a PWM pin. For example
analogWrite(3, 127);
The analogWrite()
function shown above takes two parameters. The first parameter is the pin number and the second parameter is the duty cycle from 0 to 255 representing 0% to 100% duty cycle. In the example, the value 127 translate to 50% duty cycle.
In the sketch, we set the PWM from 100% duty cycle to 0% duty cycle in half step with a 200ms delay between each step in a loop.
When PWM, DELAY, PWM, DELAY are executed in a loop, the result of this exercise is the brightness of the LED dimming from full brightness to off and repeat.
Exercise 3 - Knight Rider LED
The Knight Rider LED is inspired by the advanced artificial intelligent self-aware car from the 80’s Knight Rider TV series. The car was a customized 1982 Pontiac Trans-Am easily identified by its light scanning from left to right. This exercise is to simulate the scanner light using LEDs.
- Connect a JUMPER WIRE to GND and 31J.
- Connect a JUMPER WIRE to 31G and 33G.
- Connect a JUMPER WIRE to 33H and 35H.
- Connect a JUMPER WIRE to 35G and 37G.
- Connect a JUMPER WIRE to 37H and 39H.
- Connect a 220Ω RESISTOR to PIN2 and 40J.
- Connect a 220Ω RESISTOR to PIN3 and 38J.
- Connect a 220Ω RESISTOR to PIN4 and 36J.
- Connect a 220Ω RESISTOR to PIN5 and 34J.
- Connect a 220Ω RESISTOR to PIN6 and 32J.
- Connect the anode (longer) pin of a RED LED to 40F and cathode (shorter) pin to 39F.
- Connect the anode (longer) pin of a RED LED to 38F and cathode (shorter) pin to 37F.
- Connect the anode (longer) pin of a RED LED to 36F and cathode (shorter) pin to 35F.
- Connect the anode (longer) pin of a RED LED to 34F and cathode (shorter) pin to 33F.
- Connect the anode (longer) pin of a RED LED to 32F and cathode (shorter) pin to 31F.
Write and Upload the Knight Rider Sketch
In order to execute the sketch for this exercise, launch Arduino IDE then
Click File -> New.
A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty setup()
and loop()
that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int timer = 100;
void setup(){
// Configure PIN2 - PIN 6 as OUTPUT
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
}
void loop() {
digitalWrite(pin2, HIGH); // Turn ON LED on PIN2
delay(timer); // delay
digitalWrite(pin2, LOW); // Turn OFF LED on PIN2
delay(timer);
digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);
digitalWrite(pin6, HIGH);
delay(timer);
digitalWrite(pin6, LOW);
delay(timer);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);
digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);
digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
}
Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter Excercise_3
into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.
Understanding the Knight Rider Sketch
The sketch start by making PIN2 to PIN6 as OUTPUT so that they can drive the LED ON or OFF. The concept of Knight Rider LED is to turn ON and OFF each LED in sequence from right to left. PIN2 ON, delay, PIN2 OFF then PIN3 ON, delay, PIN3 OFF,…..PIN6 ON, delay, PIN6 OFF. As soon as the last LED on PIN6 is turned OFF, the ON and OFF sequence is preformed in a reverse sequence from PIN6 to PIN2.
When the right to left and left to right sequence is performed in a loop, the LEDs will look like the Knight Rider’s scanner light.
Exercise 4 - Push Button Controlled LED
In this exercise we will learn how to use the STEMTera™ Breadboard to read the input from a push button and translate the input into a toggling state that can be used to control an LED.
- Connect the anode (longer) pin of the RED LED to 38G and cathode (shorter) pin to 39G.
- Connect the push button switch to 32F, 34F, 32E and 34E.
- Connect the 220Ω resistor to PIN2 and 38J.
- Connect a jumper wire to GND and 39I.
- Connect a jumper wire to GND and 32J.
- Connect a jumper wire to PIN6 and 34J.
Write and Upload the Push Button Controlled LED Sketch
In order to execute the sketch for this exercise, launch Arduino IDE then
Click File -> New.
A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty setup()
and loop()
that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.
boolean LEDState=false;
int LED = 2;
int BUTTON = 6;
void setup() {
pinMode(LED, OUTPUT);
// set BUTTON pin as INPUT with an Internal PULLUP
pinMode(BUTTON, INPUT_PULLUP);
}
void loop() {
// wait for BUTTON Pin LOW
if (digitalRead(BUTTON)==LOW){
delay(50); // wait for 50 ms
// if BUTTON Pin still low, then it is a solid press
if (digitalRead(BUTTON)==LOW) {
LEDState = !LEDState; // toggle the LED state
digitalWrite(LED, LEDState);
}
// NOTE: this is a bad way to debounce a button
// if the BUTTON is stuck at LOW the sketch will
// forever stays in the while
while(digitalRead(BUTTON)==LOW) {
// wait until BUTTON is read HIGH
};
}
}
Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter Excercise_4
into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.
Understanding the Push Button Controlled Sketch
In order for the sketch to keep track of the state of the LED, we need to first declare a boolean
variable with a default as false
.
boolean LEDState=false;
Each time the LED changes its state, we will store the state in this variable. We then declare two more variables, one for the LED pin and the other one for BUTTON pin, which are 2 and 6 respectively.
int LED = 2;
int BUTTON = 6;
The pin where the LED is connected to will be set as OUTPUT
using pinMode(LED, OUTPUT)
function and the pin where the push button is connected to will be set as INPUT_PULLUP
using pinMode(BUTTON, INPUT_PULLUP)
function. The constant
INPUT_PULLUP
means, other than making the pin as INPUT
, it also enable the internal pull-up resistor of that pin, so that the pin will be connected to the 5V via the internal pull-up resistor and always in a HIGH
state. When the push button that is connected to the BUTTON
pin is pressed, it connects the BUTTON
pin to ground (GND) thus making the BUTTON
pin reads LOW
.
When in the loop()
, the code
if (digitalRead(BUTTON)==LOW)
is waiting for the BUTTON
pin to go LOW
, once it is low, it wait for 50ms and read the BUTTON
pin again for low. This act as a simple debounce
to eliminate any noise when the push button first make a contact. If the BUTTON
pin is still low after 50ms, then the contact is solid and therefore the state of the LED is being toggled (false
to true
, or true
to false
) using this code.
LEDState = !LEDState;
After toggling the LEDState
, we can now write the state to the LED
pin. In order to prevent the toggling repeatedly fired while the push button is being depressed, we add another line of code to wait for the BUTTON
pin to go HIGH when the push button is released.
while(digitalRead(BUTTON)==LOW)
This code is a lazy hack and a bad coding practice because if the push button is shorted, the sketch will forever stays in the while
loop and will never execute any other code.
CHALLENGE! Write a sketch to perform the same task with button debounce, LED toggling and will never locks up even when the push button is shorted.
Exercise 5 - Buzzer Music
The STEMTera™ Breadboard can be used to produce musical note by sending out digital pulses in frequency corresponding to that musical note. When this frequency is being sent to a buzzer, the internal of the buzzer vibrates and thus producing a sound.
- Connect the positive terminal of the buzzer to 33I.
- Connect the negative terminal of the buzzer to 34I.
- Connect a wire to GND and 34J.
- Connect a wire to PIN8 and 33J.
NOTE: If you have a different buzzer, connect the positive terminal of the buzzer to PIN8 and the negative terminal to GND. It is not required to follow exactly as per above’s connection.
Write and Upload the Buzzer Music Sketch
In order to execute the sketch for this exercise, launch Arduino IDE then
Click File -> New.
A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty setup()
and loop()
that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
// e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.
}
Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter Excercise_5
into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.
Understanding the Buzzer Music Sketch
Arduino has a built-in function called tone()
that is used in this sketch to produce musical note. The sketch starts by including a header file called pitches.h
. In this file, there are constants defining the pitches of music notes, for example
#define NOTE_C4 262
This defines a middle C (C4) with a frequency of 262 Hz. In reality middle C is 261.625565 Hz. In the next block of codes,
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
8 musical notes is being stored in an array called melody[]
. The reason to store these notes in an array is because it will be easier to write a for loop
to play all the notes in melody[]
.
The second block of codes also uses an array, this time to store the note duration.
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
In the array noteDurations[]
, a 4
represents quarter note and an 8
represents eighth note. In the setup()
function, the notes are being played in a for loop
that start from 0 to 7 (total 8 notes ) incrementing one at a time with function
tone(8, melody[thisNote], noteDuration);
This instructs the STEMTera™ Breadboard to send digital pulse to PIN 8, with a frequency of melody[thisNote]
and play for a duration for noteDuration
. In order to distinguish the notes, a delay is required in between each note. The author of the Tone Library found out that the delay of 130% of noteDuration
seems to work well.
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
After the required delay between notes,
noTone(8)
is called to stop pulses being sent to PIN8 after playing the 8 notes store in the melody[]
array.
CHALLENGE! Write a sketch to play Tetris melody and blinks an LED at the same time.
Exercise 6 - Potentiometer
In this exercise, we will learn how to use a potentiometer. A potentiometer is variable resistor that comes in a few different sizes and shapes. The most common shape is a three terminal round knob. The resistance of the potentiometer will change when the knob is turned. In the schematic below, one terminal of the potentiometer is connected to 5V and the other is connected to GND. This creates a potential difference in the middle terminal and forms a voltage divider circuit.
- Connect the terminal 1,2 and 3 of the potentiometer to 34C, 35C and 36C respectively.
- Connect a jumper wire to 5V and 34A.
- Connect a jumper wire to A0 and 35A.
- Connect a jumper wire to GND and 36A.
Write and Upload the Potentiometer Sketch
In order to execute the sketch for this exercise, launch Arduino IDE then
Click File -> New.
A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty setup()
and loop()
that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.
int sensorValue;
void setup() {
pinMode(A0, INPUT);
pinMode(13, OUTPUT);
}
void loop() {
sensorValue=analogRead(A0);
digitalWrite(13, HIGH);
delay(sensorValue);
digitalWrite(13, LOW);
delay(sensorValue);
}
Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter Excercise_6
into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.
Turn the knob on the potentiometer and notice the RED L LED on the STEMTera™ Breadboard blinks corresponding to the knob.
Understanding the Potentiometer Sketch
The first line of the sketch
int sensorValue;
declares an integer variable called sensorValue
. This variable will be used to store the value of the voltage read from the middle terminal of the potentiometer.
In the setup()
function, PIN A0 is configured as INPUT
and PIN13 is configured as OUTPUT
.
void setup() {
pinMode(A0, INPUT);
pinMode(13, OUTPUT);
}
The STEMTera™ Breadboard has 6 exposed analogue to digital converter (ADC). The ADC is commonly used to convert analogue voltage to digital value. In this example
sensorValue=analogRead(A0);
the analogue voltage of PIN A0 is being read using analogRead()
function and stored into the variable sensorValue
. The code then proceed to turn PIN 13 HIGH
by executing
digitalWrite(13, HIGH);
after that a delay of a digital value (sensorValue
) which corresponds to the analogue voltage read, will be executed. This delay demonstrates the blinking period of the LED as the knob of the potentiometer is being turned.
delay(sensorValue);
With the potentiometer’s both left and right terminals connected to 5V and GND respectively, the middle terminal of the potentiometer will have a voltage swing between 5V and 0V when the knob is being turned from one side to the other side. When the voltage of the middle terminal is read by an ADC, the corresponding digital value of the ADC in this case is 0 to 1023, where 0 is 0V and 1023 is 5V. This digital value when is passed to the delay()
function, it represents time in a unit of milliseconds. After the delay, PIN 13 is turned LOW
followed by a delay with a value of sensorValue
again.
When these instructions are executed in a loop, the RED LED on the STEMTera™ Breadboard will turn ON and OFF in a period corresponding to the position of the potentiometer’s knob.
CHALLENGE! Write a sketch to read the potentiometer value and play a pitch relative to the value.
Exercise 7 - Automatic Night Lamp
An automatic night lamp is basically a lamp that will automatically turn on when it sense darkness in the environment. In this example, we will be using a photoresistor to demonstrate how to convert the ambient level into a lamp’s brightness.
A photoresistor or photocell or a light dependent resistor (LDR) is a component that has a variable resistance with the light intensity that falls upon it. The brighter the light is, the lower the resistance will be. The photoresistor is commonly used to turn on a light and also used to control the shutter speed on a camera.
In the circuit below, a voltage divider is formed by a 10K resistor and the photoresistor. When light shines on the photoresistor, the resistance of the photoresistor become lower, thus the voltage drop across the photoresistor will be lower. When the photoresistor is being covered and away from light, the resistance of the photresistor will become higher and caused the voltage drop across it to be higher. By knowing the voltage drop across the photoresistor, we can obtain the relative light level value converted from the voltage read across the photoresistor.
- Connect the 10K resistor to 5V and 30A.
- Connect the photoresistor to 30E and 30F.
- Connect the 220Ω resistor to PIN9 and 32J.
- Connect the anode (longer pin) of the LED to 32I and the other pin to 33I.
- Connect a jumper to GND and 30J.
- Connect a jumper to GND and 33J.
- Connect a jumper to A0 and 30C.
Write and Upload the Automatic Night Lamp Sketch
In order to execute the sketch for this exercise, launch Arduino IDE then
Click File -> New.
A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty setup()
and loop()
that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.
int brightness;
int lightLevel;
int ambient=120;
int totalDark=240;
void setup() {
pinMode(9, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
lightLevel = analogRead(A0);
brightness = map(lightLevel, ambient, totalDark, 0, 255);
if (brightness<0) brightness=0;
analogWrite(9, brightness);
Serial.print("Light Level=");
Serial.print(lightLevel);
Serial.print(", ");
Serial.print("Brightness=");
Serial.println(brightness);
}
Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter Excercise_7
into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.
Calibrating the Automatic Night Lamp
When we first execute the sketch, the STEMTera™ Breadboard does not know the ambient light level, but this ambient light level is important because we need to tell the STEMTera™ Breadboard the ambient light so that it will be able to turn the brightness of the LED according to the ambient lighting.
In order to find out the current ambient light level measured by the photoresistor, we need to open the serial monitor window.
When the status bar of the Arduino IDE shows Done uploading
, click serial monitor. A new serial monitor windows will be displayed. The serial monitor is a way for the STEMTera™ Breadboard to communicate with the computer to send or receive data, most commonly the debugging messages.
On this windows, we can see a continuous stream of messages is being displayed. In this messages, we are interested in the Light Level
and Brightness
.
Place the STEMTera™ Breadboard at a place where you would like it to read the ambient light level from. Without anything blocking light to the photoresistor, write down the value of Light Level
from the serial monitor. This value is the digital value of the corresponding voltage dropped on the photoresistor (see explanation below for details). Add about 10 to this value, for example, if the value is 115, then add 10 to become 125. Switch back to the Arduino IDE and edit the following line of code
int ambient=120;
change it to the value we have just calculated
int ambient=125;
Now take a dark cloth or use your palm to cover the photoresistor, you will notice the Light Level
value on the serial monitor changing to a larger value as you place the cover over the photoresistor. While maintaining the cover over the photoresistor, write down the Light Level
value. This should be a larger value than the previous value. Switch back to the Arduino IDE and edit the following line of code
int totaldark=240;
if the value is 260, then change that line to
int totaldark=260;
By changing these two lines of code, we are telling the STEMTera™ Breadboard the ambient light and the total darkness light level in your environment.
Click and upload your sketch again.
Move you palm over and then away from the photoresistor, you will see the brightness of the LED changing as you move your palm over the photoresistor.
Understanding the Automatic Night Lamp Sketch
We first declare two integer variables to store the brightness and light level as below
int brightness;
int lightLevel;
Variable lightLevel
is use to store the current light level and variable brightness
is for storing the brightness of the LED. Another two variables are ambient
and totalDark
that were already explained in the calibration section.
In the setup()
section PIN9 is setup as OUTPUT
and A0
as INPUT
. In this exercise we will introduce a debugging or data transfer feature of the STEMTera™ Breadboard, the Serial
object.
Serial.begin(9600);
The line of code tells the STEMTera™ Breadboard to initialize its serial port to 9600 baud rate and start using it. Baud rate is the rate at which data is transferred between serial devices. Serial devices need to set the same baud rate to talk to each other.
In the loop()
function we first call
lightLevel = analogRead(A0);
to read the analogue value from pin A0 and then store into the variable lightLevel
. Because the value of the light level (125 to 260 from our previous calibration) is different to the analogueWrite()
function’s 0 to 255 PWM value, we need to have them converted into a relative value that is corresponding to each other. By using the map()
function
brightness = map(lightLevel, ambient, totalDark, 0, 255);
we are converting the ambient value 120 to PWM value of 0, to the max range of ambient value or 260 to a PWM value of 255. The map()
function then return the converted value and stored into the variable brightness
.
Because the ambient light is not always consistent, the map()
function might sometimes return a negative value which is not a valid value for brightness when using analogWrite()
function, we need to perform a conditional check using
if (brightness<0) brightness=0;
This means, if the brightness
variable has a value that is less than zero, store zero to the variable brightness
. By doing this, we can ensure that the variable brightness
will not have a negative value.
The following codes instruct the STEMTera™ Breadboard to print out the variables message via the serial monitor window.
Serial.print("Light Level=");
Serial.print(lightLevel);
Serial.print(", ");
Serial.print("Brightness=");
Serial.println(brightness);
When executed in a loop, the current ambient light level is continuously being converted into a relative brightness value and thus making the LED automatically controlled by the ambient light.
CHALLENGE! Connect another LED to the circuit and add codes into the currect sketch to do the reverse of the current LED’s behavior.