When you’re using microcontrollers, you frequently need to control devices that need more electrical current than a microcontroller can supply. Common examples include:
Controlling a DC motor
Controlling low-voltage (12-24V) lights
Controlling addressable LEDs
For all of these applications, you’ll need a high-current power supply. For some applications where the load operates at a voltage higher than your microcontroller but less than your power supply, you’ll need a voltage regulator and to set up your circuit to power the microcontroller and the load through it. For many of these applications, you’ll also need an electrical relay or transistor to control the load. These notes explain relays and transistors as they’re used for this purpose. In order to get the most out of these notes, you should know something about how electricity works, and you should know the basics of how a microcontroller works as well.
Digital output from a microcontroller is typically a low-amperage signal. For example, when you set a pin HIGH, the voltage coming on that pin is typically +3.3V or +5V, and the amperage that it can source is around 10 milliamps. This is fine if you’re controlling an LED, whose required amperage is tiny. However, most devices you’d want to control need more current than that to operate. You need a component in between your microcontroller and the device that can be controlled with this small voltage and amperage. Relays and transistors are most often used for this purpose. A relay is a switch that’s controlled by a small electric current. Relays take advantage of the fact that when you pass an electric current through a wire, a magnetic field is generated surrounding the wire as well. This is called induction. When you place two pieces of ferrous metal near a coil of wire and pass current through the wire, the magnetic field can move the two pieces of metal towards each other. Those pieces of metal can form a switch, which can be turned on and off by putting current through the coil, as shown in Figure 1, 2 and 3.
Figure 1. Two relays, one whole and the other with the switch removed. The blue and white tube in each relay is the coil, and the glass vial, which is normally inside the tube, contains the switch
Figure 2. Detail of the switch inside the relay, magnified 20x
Figure 3. Relay connection pins In this image the four pins are visible. The two on the long end of the relay tube are the switch connections. The two pins on one end of the tube, on an axis perpendicular to the tube, are the coil connections.
Figure 4. Diagram of Relay wired to a microcontroller and a lamp with a + 9 volt battery/ The switch terminals are connected to one terminal of a lamp and to the negative terminal of a 9V battery, respectively. The other terminal of the lamp is connected to the positive terminal of the battery. One of the coil terminals is connected to the output pin of a microcontroller, and the other is connected to the microcontroller’s ground. There is no electrical connection between the microcontroller circuit and the lamp circuit.
In Figure 4, you can see that there’s no electrical connection between the microcontroller circuit that’s controlling the coil of the relay and the lamp circuit. This is one advantage that relays offer. Figure 5, a schematic of the relay circuit is the same as Figure 4, but shown with traditional schematic symbols for the relay, the battery, and the lamp:
Figure 5. Schematic of a relay wired to an arduino and a lamp with a + 9 volt battery
The current needed to move the shaft in the coil is very low (less than 10 milliamps) so the coil can be energized by an output pin of your microcontroller. The current that can flow through the switch, however, is much higher. The lamp circuit is separate from the microcontroller. It uses a separate power source, with the amperage and voltage needed to turn on the lamp. The power source, the lamp, and the switch side of the relay are all placed in series. When the coil is energized, the leaves of the switch are physically moved by the magnetic force created, the lamp circuit is completed, and the lamp turns on.
Because there is no electrical connection between the switch and the coil, relays can also control AC loads as well as DC loads. You can’t use a relay to dim a lamp or control the speed of a motor, however. The switching speed of relays is too slow to pulsewidth modulate them.
Because a relay is a mechanical switch, it can be somewhat slow. Relays take a few milliseconds to close, so they aren’t very effective when you want to turn them on and off rapidly. Sometimes you need to switch a high current circuit rapidly. In this case you would use a switching transistor. A transistor is an electronic device that can work as a switch. It allows control of a large current by a smaller current as does a relay. Unlike a relay, however, a transistor is not mechanical, and can operate much faster than a relay. There are several types of transistors and they come in two major classes: bipolar transistors, and field-effect transistors, or FETs. All transistors have some similar properties though. They all have three connections, referred to as the base, the collector, and the emitter (on FET transistors, the three connections are the gate, the drain and the source).When you apply a small voltage and current between the base of a transistor and the emitter (or the gate and the drain on a FET), you allow a larger current to flow from the collector to the emitter (or the drain and the source).
Among bipolar transistors, which are the older class of transistors, there are two types: NPN transistors, and PNP transistors. When you apply positive voltage to the base of an NPN transistor, it turns on the collector-emitter connection and allows current to flow from collector to emitter (Figure 6). The equivalent MOSFET is called an N-channel MOSFET. When you apply voltage to the base of a PNP transistor, by contrast, the collector-emitter connection turns off, and no current can flow from collector to emitter. The MOSFET equivalent is a P-channel MOSFET. One of the main differences between MOSFETS and bipolar transistors is that MOSFETS require negligible current on the base in order to activate. For the purposes of switching a load on and off, they are an excellent choice.
Figure 6. Chart comparing Bi-polar transistors and MOSFETS. NPN Transistors behave similarly to N-Channel MOSFETS. PNP Transistors are comparable to P-Channel MOSFETS
Among the bipolar transistors, there’s one type commonly used to switch high-current loads, the Darlington transistor. Darlington transistors are actually two transistors in one, combined with a diode that protects the transistors from damage in case the load’s current runs in reverse. In many of the labs on this site, you’ll see reference to one the TIP120 Darlington transistor.
Darlington transistors aren’t the only good transistors for high-current loads, though. You could use an N-Channel MOSFET with a protection diode in place of the darlington transistor. The IRF520 MOSFET is a good equivalent if you’re using a 5-volt microcontroller like the Uno. The FQP30N06L MOSFET works well for both 5V microcontrollers and 3.3V microcontrollers like the Nano 33 IoT.
Figure 7 shows the basic circuit for using a transistor to control a high-current load. You connect a DC power source to one terminal of the load, then connect the second terminal of the load to the collector of the transistor (or drain, for a MOSFET) of the transistor. The emitter (or source) is then connected to ground, and the base (or gate) is connected to the output of your microcontroller. When you take the output pin of the microcontroller high, the voltage difference between the base (or gate) and the emitter (or source) allows current to flow through the load, through the collector (or drain) to the emitter (or source) and to ground.
Figure 7. Two similar microcontroller and motor schematics. The first schematic uses a Darlington Transistor. The second uses an N-Channel MOSFET.
Note how similar this schematic is to the relay schematic. The transistor here is serving the same function as the relay. However, it can switch much faster than the relay. In addition, because there are no mechanical parts, it will reliably function for more switching operations than the relay. However, current can only flow in one direction through a transistor. If the voltage on the collector (or drain) is lower than that on the emitter (or source), you can damage the transistor. The same is not true with a relay.
There are three differences between this transistor circuit and the relay circuit above. The first is that you’re using a motor as the load, rather than an incandescent light bulb. Because motors are inductive loads (they work because of induction; for more, see the DC motor notes), they can create a reverse voltage when spinning down after you turn them off. Because of this, the second difference is the protection diode in parallel with the transistor. The protection diode routes any reverse voltage around the transistor, thereby protecting it. Most transistors designed for controlling motors have a protection diode built-in. The TIP120, the IRF520, and the FQP30N06L all have built-in protection diodes. The third difference is that the microcontroller attached to the base (or gate) and the transistor’s emitter (or source) must have a common ground. If not, then the circuit will not work.
If you are switching DC motors, solenoids, or other high-current DC devices which create motion, it’s better to use a switching transistor than a relay. The ideal way to control a motor is with an H-bridge, which is an array of transistors that lets you control not only speed but also direction. There’s more on that in the motor control notes.
Power supply is a reference to the source of electrical power. Most electronic circuits require a DC power supply. Chances are you have one at home already, and can use it for physical computing projects.
The most common operating voltages for microcontrollers and digital processors are 5V and 3.3V. You can find power supplies in many voltages, but 5V and 12V are common. To convert 12V to 5V or 3.3V, you’d need a voltage regulator. The Breadboard Lab covers how to set that up.
There are many different kinds of DC power supplies but this one shown in Figure 1 is most commonly used at ITP:
Figure 2. DC supply rating label. This is the back of the supply in Figure 1.
Most power supplies have a rating label that looks something like the one in Figure 2. Make sure you know the polarity of the plug so you don’t reverse polarity for your circuit and damage your components. The diagram in Figure 3 and Figure 4 showing positive tip polarity is on the left and negative tip polarity is on the right. The center positive drawing on the left indicates that the center (tip) of the output plug is positive (+) and the barrel of the output plug is negative (-).
Figure 3. Symbol for a center-positive power supply.
,
Figure 4. Symbol for a center-negative power supply.
Abbreviations
V : Volts A : Amperes W : Watts mA : miliAmperes VA : Volt Amperes VAC : Volts AC VDC : Volts DC DC : Direct Current AC : Alternating Current
Testing your power supply
It is always good practice to test a power supply before using it for the first time. The example below will show how to test a power supply with positive polarity. If you have a negative polarity power supply, then you will get a negative reading. You should then switch the position of the multimeter probes.
Figure 5. Red probe goes into the tip Black probe touches the barrel
Plug your power supply into an AC outlet.
Turn on your multimeter and set it to read DC voltage.
Take the red (positive) probe from your multimeter and stick it into the end of the power supply plug as shown in Figure 5.
Take the black (negative) probe from your multimeter and carefully touch it against the barrel of the plug without touching the tip or your red probe. If you make a connection, you will be creating a short circuit.
On your multimeter you should see a reading of the voltage coming from your power supply. If you are checking a 12V power supply and your multimeter shows “12.56V” everything is fine and dandy as shown in Figure 6. If you get a reading of “-12.56V” then your probes are attached in reverse. If this happens and you are positive you connected your probes correctly, double check the polarity on your power supply’s label and make sure the circuit you will be powering with this unit is designed to handle this polarity.
Figure 6. Multimeter displaying output DC voltage
If the voltage showing on your multimeter is more than half a volt or a volt off its rating, then you most likely have what is called an unregulated power supply. The 12V Jameco power supply we used in this example is a regulated one, so that is why the voltage we received was so close to the voltage it was rated for.
Powering an Arduino Project from a Mobile Phone Charger
Many people have old mobile phone chargers around the house, and wonder, “Can I use this for powering an Arduino project?” Generally, you can. Just get a USB cable with the appropriate connectors to connect the phone charger to your Arduino. Most phone chargers output 5V and a few hundred milliamps, which will power an Arduino, some sensors, and some LEDs. If you try to program your Arduino from the phone charger’s cable, though, you may be out of luck. Many phone chargers come with USB cables that contain only the power connections, not the data connections.
Matching A Power Supply to an Electronics Device
To determine whether a power supply is right for your project, you need to note the voltages that each component operates at, and the current they consume, and make sure your power supply can provide the right amount of power.
Imagine you’re making a project that includes an Arduino, a few LEDs, some pushbuttons, some potentiometers or other variable resistors, and perhaps a speaker. The Digital In and Out lab and the Analog In lab, and the Tone Output labs all describe projects that meet this description. All of the components other than the Arduino in this project are powered from the Arduino voltage output. None of the external components consume more than a few milliamps each. The whole circuit, Arduino included, will likely consume less than 200 milliamps of current. Here’s a breakdown, measured using an LED and a potentiometer:
Arduino Uno, no external components: 0.04A (40 mA)
Arduino Nano 33 IoT, no external components: 0.01A (10 mA)
LED: 4 mA
potentiometer connected as analog input: 0.29 mA
8-ohm speaker, playing a tone on the output pin: 0.5 mA
A phone charger, which supplies 5 volts and about 500 milliamps to the Arduino, would do the job fine. The Arduino Uno operates on 5 volts, and the Arduino Nano 33 IoT, which operates at 3.3 volts, has a built-in voltage regulator that will convert the 5V to 3.3V.
If you had a 12-volt supply like the one above, you could also use it for these projects. The Arduino Uno has a voltage in plug which matches it, and can take up to 15V. An on-board regulator converts the higher voltage input to 5V. The Nano 33 IoT has an on-board regulator that can accept up to 20V in its Vin pin (physical pin 15), so if you connected a DC power Jack and connected the ground of the 12-volt supply to the Arduino’s ground and the positive connection of the 12-volt supply to the Arduino’s Vin pin, your project would operate.
Arduino, Servomotor
If you’re controlling an RD servomotor from an Arduino as shown in the Servomotor lab, you need to consider the current a bit more. A servo like the Hitec HS-311 , which is popular in physical computing projects, operates at 4.8 – 6.0V, so it can get enough voltage from an Arduino’s voltage output. When it’s idle, it consumes about 160 mA with no load on it. It can consume up to 3-400 mA with a heavy load, however. It’s wise to plan your project for each component’s maximum current consumption, so a single servo and Arduino could consume up to 440 – 450 milliamps at 5 volts. That is almost the limit of what a laptop computer can supply via USB, and it’s the limit of some smaller phone chargers as well. If you were controlling multiple servos, you wouldn’t have enough current.
Arduino Uno, no external components: 0.04A (40 mA)
Arduino Nano 33 IoT, no external components: 0.01A (10 mA)
HS-311, heavy load: 400 mA
Arduino, DC Motor or Lights
When you start powering larger DC motors, DC lights, or other high-current loads, you have to calculate the voltage and current before you select a power supply. You generally work from the component that has the highest consumption and work from there.
For example, controlling an LED light bulb like this one would require a 12V DC power supply for the bulb. It consumes 11 watts of power, and watts = volts * amps, so it consumes about 917 milliamps of current at 12 volts. The transistor and Arduino that might control it could be powered via the same 12-volt power supply, and would consume the same amounts as in the examples above.
Motor projects and addressable LED projects often consume the most electrical energy and are the most complex to power. A typical addressable LED like a WS2812, aka NeoPixel LED, consumes between 60 and 80 mA of current at 5 volts. When you have a string of 60 of them, that’s 3.6 amps of current! These definitely can’t be powered from a typical DC wall supply. When you reach that level of complexity with a project, consult your components’ datasheets or your instructors for more guidance. The videos on electricity, current, and power are helpful on this subject as well.
Introduction
In the stepper motor and H-bridge lab, you learned how to control a stepper motor with a dual H-bridge driver, specifically the TB6612FNG. This is not the only driver for controlling a stepper. Step & direction stepper drivers offer a simpler approach, from the microcontroller side. They have just two control pins, one for step ...
Texas Instruments’ Bluetooth LE sensorTags contain a number of useful sensors in a self-contained, compact package: accelerometer, gyrometer, magnetometer, temperature sensors, all in a Bluetooth LE-enabled package.
Sensors convert various forms of physical energy into electrical energy, allowing microcontrollers to read changes in the physical world.
The simplest sensors read changes in mechanical energy, usually by moving electrical contacts. For example the pushbutton or switch (related video) converts mechanical energy (e.g, your finger’s press) into electrical energy by closing a connection between two metal contacts. The potentiometer (related video) shown in Figure 1 and Figure 2 is another sensor that reads mechanical energy changes: a metal contact called a wiper slides along a resistor, effectively short circuiting the resistor (related video) into two halves and creating a voltage divider circuit.
Figure 1. A potentiometer broken open, showing the carbon substrate and wiper.
Figure 2. Inside a potentiometer, the wiper, attached to the center contact, slides along the carbon resistor, forming effectively two resistors in series.
Although switches and pushbuttons typically only read an on state or an off state, most other sensors can read a wide range of possible states. In other words, they’re analog sensors, because they read a variable change. Whenever you use a new sensor, the first thing you need to understand is what the range is that it can read and deliver. When you’re using your sensor in an application, you need to know the range of possible values your application requires. Do you just need high, medium, and low (3 possible values), or a range from 0 to 10? Do you need a 100-point range? The range that you need depends on what your user can perceive. For example, if you’re making a lighting dimmer, and your user can only perceive about a dozen different light levels, then you don’t need to give her a sensor that can give her a 1000-point range of control.
Many sensors work by converting the energy they read into a changing electrical resistance by using a variably resistive material at their heart. Examples of resistive sensors are shown in Figure 3. For example, force sensors and stretch sensors are made of a partially conductive rubber. When the rubber is stretched or compressed, the resistance change. Photoresistors or light dependent resistors change their resistance when exposed to a change in light energy. In order to read changes in resistance, you typically place these sensors in a voltage divider circuit , which converts the resistance change into a changing voltage.
Light is used in many sensors in a variety of ways. Light-emitting diodes, light-dependent resistors, and phototransistors can be combined to sense dust, measure distance, or determine reflected color. Light is also used in some ranging sensors to determine distance from a sensor to a target.
Many sensors measure movement or distance indirectly, by sending out a pulse of light or sound and reading the reflected signal when it bounces off a target as illustrated in Figure 5. These are called ranging sensors (Figure 4) because they read a range of distance.
Figure 5. Ranging sensors work by bouncing energy off the target and reading the reflection.
Still other sensors work by converting the energy they read into a change in capacitance. For example, accelerometers and other miniaturized electromechanical (MEMS) sensors typically have a tiny moving conductive mass at their core, suspended on tiny springs and surrounded on both sides by electrical contacts. Because the conductive mass is parallel to the outer contacts, a capacitance builds up between the contact. When the mass is moved, the capacitance changes between the two sides, effectively creating two variable capacitors as illustrated in Figure 6. That variable capacitor is then placed in a resistor-capacitor circuit to convert the change in capacitance into a changing voltage.
Figure 6. MEMS sensors often form a variable capacitor by moving a capacitive plate in between two other capacitive surfaces.
Other than switches and variable resistors, most sensors you buy are integrated circuits that include the resistance-to-voltage or capacitance-to-voltage circuit and provide you with an analog voltage output. Some will even include an analog-to-digital converter and provide you with a serial data interface, either I2C, SPI, or asynchronous serial, so that you can connect the sensor to the serial ports of your microcontroller. Still others will provide a changing pulse output, or pulse width modulation (PWM) output, where the width of the pulse represents the sensor value.
Figure 7. A collection of sensors with digital interfaces. Clockwise, from top: AS7341 spectrometer; LIS3DH accelerometer; VL53L0X Time-of-flight distance sensor; Max3010x particle detector. These sensors are on small breakout boards which can connect to a solderless breadboard. They all connect to a microcontroller using SPI or I2C.
When you shop for sensors, you need to look at the data sheet to learn how they operate. The data sheet will usually include the following essential facts:
a text description of the sensor and its operation;
a pin diagram to tell you what pins perform what functions;
a table of electrical characteristics that tells you what the supply voltage is, what the operating current is, and what the output is, along with other electrical and physical properties;
a graph or conversion formula that relates the input energy to the output energy;
a mechanical description of the sensor itself.
Some datasheets will include other information as well, such as application circuits, reliability data, and so forth.
You should follow the datasheet’s guideline for sensor operation. Don’t exceed the maximum or minimum operating voltage, and make sure to supply adequate current to operate the sensor. Avoid supplying too much current, because the excess current will get turned into heat, which often changes the sensor’s operating characteristics. Make sure you understand the sensor’s interface. Be clear on whether it’s an analog electrical property like resistance, capacitance or voltage, or a digital serial data interface. Look for typical application circuits and microcontroller code samples online if you can find them.
Once you understand how a sensor works and what its output will be, you should connect it to your microcontroller and write a simple program to read the output. Don’t jump right into writing a complex program. Write the smallest program necessary to read the sensor’s changing values and output them for debugging purposes. Then put the sensor in the physical context in which you plan to use it and read the output values in action. The range that a sensor can read will change depending on the specific conditions or actions that you plan to read. So make sure the physical setting of the sensor is as close to reality as possible in order to test it. Once you know the range of values that it’s going to output in those conditions, then you can write a function to convert the sensor’s raw output range into a range you can use. Depending on the context, you probably won’t get the full output range that the sensor is capable of delivering. So your conversion function should be based on the range you’re actually seeing, not the total range.
You probably don’t need to convert the sensor’s readings into its output voltage or its physical property. For example, if you’re using a force sensing resistor, you probably don’t need to know how many Newtons of force are being exerted on the sensor, or what the output voltage is. Instead, you probably just need to know whether someone is pressing gently, firmly, or really firmly against the sensor. Perhaps you just need a range from 0 to 10. When you write your conversion function, consider what the relevant result is for you, and write a function that delivers that result.
In this lab, you’ll learn how to connect a variable resistor to a microcontroller and read it as an analog input. You’ll be able to read changing conditions from the physical world and convert them to changing variables in a program.
Introduction
In this lab, you’ll learn how to connect a variable resistor to a microcontroller and read it as an analog input. You’ll be able to read changing conditions from the physical world and convert them to changing variables in a program.
Many of the most useful sensors you might connect to a microcontroller are analog input sensors. They deliver a variable voltage, which you read on the analog input pins using the analogRead() command.
To get the most out of this lab, you should be familiar with the following concepts and you should install the Arduino IDE on your computer. You can check how to do so in the links below:
Flexible jumper wires. These wires are quick for breadboard prototyping, but can get messy when you have lots of them on a board.
A solderless breadboard with two rows of holes along each side. The . board is turned sideways so that the side rows are on top and bottom in this view. There are no components mounted on the board.
Photo of an 8 ohm speaker
LEDs. The long leg goes to voltage and the short leg goes to ground
220-ohm resistors. These ones are 4-band resistors
10-kilohm resistors. These ones are 5-band resistors
Pushbuttons
Potentiometer
Force Sensing Resistor (FSR)
Thermistor
Phototransistors. The short leg goes to voltage, and the long leg goes to the input pin of a microcontroller.
Figure 1-12. The parts you’ll need for this exercise. Click on any image for a larger view.
Figures 1-12 show the parts you’ll need for this exercise. Click on any image for a larger view.
Set Up the Breadboard
Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V or 3.3V (depending on your model) and any of the ground connections. Figures 13 and 14 show how to do this for an Arduino Uno and an Arduino Nano 33 IoT.
As shown in Figure 13, the Uno’s 5V output hole is connected to the red column of holes on the far left side of the breadboard. The Uno’s ground hole is connected to the blue column on the left of the board. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus.
Figure 13. An Arduino Uno on the left connected to a solderless breadboard, right.
Figure 14. Breadboard view of an Arduino Nano mounted on a breadboard
In Figure 14, the Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.
The Nano, like all Dual-Inline Package (DIP) modules, has its physical pins numbered in a U shape, from top left to bottom left, to bottom right to top right. The Nano’s 3.3V pin (physical pin 2) is connected to the left side red column of the breadboard. The Nano’s GND pin (physical pin 14) is connected to the left side black column. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus. The blue columns (ground buses) are connected together at the bottom of the breadboard with a black wire. The red columns (voltage buses) are connected together at the bottom of the breadboard with a red wire.
Add a Potentiometer and LED
Connect the wiper of a potentiometer to analog in pin 0 of the module and its outer connections to voltage and ground. Connect a 220-ohm resistor to digital pin 9. You can replace the LED with a speaker if you prefer audible output. Connect the anode of an LED to the other side of the resistor, and the cathode to ground as shown below. See Figure 15 and Figure 16 to learn how to do this with an Arduino Uno. Figure 17 shows a breadboard view of an Arduino Nano for the same circuit.
Figure 15. Schematic view of a potentiometer connected to analog in 0 of an Arduino and an LED connected to digital pin 9. Connect the voltage lead of the potentiometer to 5V for Uno, 3.3V for Nano 33 IoT.
Figure 16. Breadboard view of a potentiometer connected to analog in 0 of an Arduino and an LED connected to digital pin 9.
Figure 17. Breadboard view of Arduino Nano with an analog input and LED output.
Figure 17 shows the breadboard view of an Arduino Nano connected to a potentiometer and an LED. The +3.3 volts and ground pins of the Arduino are connected by red and black wires, respectively, to the left side rows of the breadboard. +3.3 volts is connected to the left outer side row (the voltage bus) and ground is connected to the left inner side row (the ground bus). The side rows on the left are connected to the side rows on the right using red and black wires, respectively, creating a voltage bus and a ground bus on both sides of the board. The potentiometer is mounted in the left center section of the solderless breadboard. Its outside pins are connected to the voltage and ground buses, respectively There is a wire connecting to analog in 0 of the nano (physical pin 4) to the the center pin of the potentiometer. An LED is mounted in the right center section of the board, with a 220-ohm resistor attached to its anode (long leg). The other end of the resistor connects to the Nano’s digital pin 9 (physical pin 27). The cathode of the LED (short leg) connects to ground. If you’re using a speaker instead of the LED, connect it to the same connections as the LED.
Program the Module
Now that you have the board wired correctly, program your Arduino as follows:
First, establish some global variables: one to hold the value returned by the potentiometer, and another to hold the brightness value. Make a global constant to give the LED’s pin number a name.
1
2
3
constintledPin =9; // pin that the LED is attached to
intanalogValue =0; // value read from the pot
intbrightness =0; // PWM pin that the LED is on.
In the setup() method, initialize serial communications at 9600 bits per second, and set the LED’s pin to be an output.
1
2
3
4
5
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the led pin as an output:
pinMode(ledPin, OUTPUT);
}
In the main loop, read the analog value using analogRead() and put the result into the variable that holds the analog value. Then divide the analog value by 4 to get it into a range from 0 to 255. Then use the analogWrite() command to face the LED. Then print out the brightness value. An alternate loop function for the speaker follows right after the first one.
1
2
3
4
5
6
voidloop() {
analogValue =analogRead(A0); // read the pot value
brightness =analogValue /4; //divide by 4 to fit in a byte
analogWrite(ledPin, brightness); // PWM the LED with the brightness value
Serial.println(brightness); // print the brightness value back to the serial monitor
}
If you’re replacing the LED with a speaker, here’s an alternate loop function that will play a changing tone on the speaker:
1
2
3
4
5
6
voidloop() {
analogValue =analogRead(A0); // read the pot value
frequency =(analogValue /4) *10; // divide by 4 to fit in a byte, multiply by 10 for a good tonal range
tone(pinNumber, frequency); // make a changing tone on the speaker
Serial.println(brightness); // print the brightness value back to the serial monitor
}
When you run this code, the LED should dim up and down as you turn the pot, and the brightness value should show up in the serial monitor.
Other variable resistors
You can use many different types of variable resistors for analog input. For example, the pink monkey in the photo below has his arms wired with flex sensors. These sensors change their resistance as they are flexed. When the monkey’s arms move up and down, the values of the flex sensors change the brightness of two LEDs. The same values could be used to control servo motors, change the frequency on a speaker, or move servo motors.
Figure 18. A stuffed pink monkey with flex sensors attached
Note on Soldering Sensor Leads
Flex sensors and force-sensing resistors melt easily, so unless you are very quick with a soldering iron, it’s risky to solder directly to their leads. See Figure 19-21 to learn about three better solutions:
Figure 19. Wire-wrapped connections of a force-sensing resistor
Figure 20. Screw terminal connection for force sensing resistor
Figure 21. Force sensing resistor connected to breakaway socket headers
If you’d like to read a changing light level, you can use a phototransistor for the job. Phototransistors are not variable resistors like photoresistors (which are shown in this video), but they perform similarly are made from less toxic materials. They are actually transistors in which the light falling on the sensor acts as the transistor’s base. Like photoresistors, they are sensitive to changes in light, and they work well in the same voltage divider circuit. Figure 22 shows how to connect a phototransistor and a 10-kilohm resistor as an analog input:
Figure 22. Breadboard view of an Arduino Nano connected to a phototransistor as an analog input. The long leg of the phototransistor connects to voltage, and the long leg connects to the input pin. The 10-kilohm fixed resistor then connects from the input pin to ground.
Different phototransistors will have different sensitivities to light. For example, this model from Everlight, which has a clear top, is most sensitive to 390 – 700 nm light range, with a peak at 630nm (orange-red). This model from Excelitas has a colored top to block IR light, and has a range from 450 -700nm, with a peak at 585nm (yellow). For the frequencies of the visible light spectrum, see this chart from Wikipedia.
Figure 23 and 24 shows an example circuit much like the pink monkey circuit (Figure 18) above, but with force-sensing resistors instead of flex sensors.
On the breadboard, two force-sensing resistors are mounted in rows 16 and 17 and 24 and 25, respectively, in the left center section of the board. Two red wires connects rows 16 and 24 in the left center section to the voltage bus on the left side. Two 10-kilohm resistors (orange, black, brown, and gold bands) connect rows 17 and 25 to the ground bus on the left hand side. Two blue wires connect from rows 17 and 25 to analog in pins 0 and 1 of the Arduino, respectively. Two 220-ohm resistors straddle the center divide of the breadboard, connecting to row 7 on both sides and 11 on both sides, respectively. In the left center section of the breadboard, two blue wires connect rows 7 and 11 to pins D10 and D9 of the Arduino, respectively. In the right center section, the anodes of two LEDs are connected to rows 7 and 11, respectively. The cathodes of the LED are in rows 6 and 10, respectively. Two black wire connects row 6 and 10 to the ground bus on the right side of the board.
Figure 23. Schematic view of two force-sensing resistors and two LEDs attached to an Arduino.
Figure 24. Breadboard view of two force-sensing resistors and two LEDs attached to an Arduino.
The circuit above works for any variable resistor. You could replace the force-sensing resistors with flex sensors to use the monkey toy above with this circuit. Two resistors placed in series like this are called a voltage divider. There are two voltage dividers in the circuit shown, one on analog in 0 and one on analog in 1. The fixed resistor in each circuit should have the same order of magnitude as the variable resistor’s range. For example, if you’re using a flex sensor with a range of 50 – 100 kilohms, you might use a 47-kilohm or a 100-kilohm fixed resistor. If you’re using a force sensing resistor that goes from infinity ohms to 10 ohms, but most of its range is between 10 kilohms and 10 ohms, you might use a 10-kilohm fixed resistor.
The code above assumed you were using a potentiometer, which always gives the full range of analog input, which is 0 to 1023. Dividing by 4 gives you a range of 0 to 255, which is the full output range of the analogWrite() command. The voltage divider circuit, on the other hand, can’t give you the full range. The fixed resistor in the circuit limits the range. You’ll need to modify the code or the resistor if you want a different range.
To find out your range, open the serial monitor and watch the printout as you press the FSR or flex the flex sensor. Note the maximum value and the minimum value. Then you can map the range that the sensor actually gives as input to the range that the LED needs as output.
For example, if your photocell gives a range from 400 to 900, you’d do this:
1
2
3
// map the sensor value from the input range (400 - 900, for example) to the output range (0-255):
You know that the maximum input range of any analog input is from 0 to 5 volts. So if you wanted to know the voltage on an analog input pin at any point, you could do some math to extrapolate it in your loop() like so:
1
2
3
4
5
6
7
8
9
10
voidloop() {
// read the sensor on analog pin 0:
intsensorValue =analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
floatvoltage =sensorValue *(5.0/1023.0);
// for 0-3.3V use the line below:
// voltage = sensorValue * (3.3 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}
Now write a sketch to control the red LED with the first sensor (we’ll call it the right hand sensor) and the green LED with the second sensor (we’ll call it the left hand sensor). First, make two constants for the LED pin numbers, and two variables for the left and right sensor values.
1
2
3
4
constintredLED =10; // pin that the red LED is on
constintgreenLED =11; // pin that the green LED is on
intrightSensorValue =0; // value read from the right analog sensor
intleftSensorValue =0; // value read from the left analog sensor
In the setup(), initialize serial communication at 9600 bits per second, and make the LED pins outputs.
1
2
3
4
5
6
7
voidsetup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the led pins as outputs:
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
}
Start the main loop by reading the right sensor using analogRead(). Map it to a range from 0 to 255. Then use analogWrite() to set the brightness of the LED from the mapped value. Print the sensor value out as well.
1
2
3
4
5
6
7
8
voidloop() {
rightSensorValue =analogRead(A0); // read the pot value
// map the sensor value from the input range (400 - 900, for example)
// to the output range (0-255). Change the values 400 and 900 below
analogWrite(greenLED, brightness); // set the LED brightness with the result
Serial.println(leftSensorValue); // print the sensor value back to the serial monitor
}
Mapping works for audible tones as well. Human hearing is in a range from 20Hz to 20 kHz, with 100 – 10000 Hz being a reasonable middle ground so if your input is in a range from 0 to 255, you can quickly get audible tones by mapping like so:
int pitch = map(input, 0, 255, 100, 10000);
When you run this, you should see the LEDs changing in brightness, or hear the speaker changing in pitch, as you press the sensors. This is the central function of analog sensors on a microcontroller: to allow for a variable range of input to control a variable range out output. Whether your sensor is read through an analog sensor like this, or through synchronous serial interfaces as you’ll see in the SPI and I2C labs, you always need to find out how the range of action from the user relates to the range of values that the sensor produces. Once you’re comfortable with this concept, get to know how to read the change in a sensor’s readings as well.
In this lab, you’ll connect a digital input circuit and a digital output circuit to a microcontroller. Though this is written for the Arduino microcontroller module, the principles apply to any microcontroller.
Introduction
In this lab, you’ll connect a digital input circuit and a digital output circuit to a microcontroller. Though this is written for the Arduino microcontroller module, the principles apply to any microcontroller.
Digital input and output are the most fundamental physical connections for any microcontroller. The pins to which you connect the circuits shown here are called General Purpose Input-Output, or GPIO, pins. Even if a given project doesn’t use digital in and out, you’ll often use LEDs and pushbuttons or switches during the development for testing whether everything’s working.
What You’ll Need to Know
To get the most out of this lab, you should be familiar with the following concepts and you should install the Arduino IDE on your computer. You can check how to do so in the links below:
Figures 1-8 show the parts you’ll need for this exercise. Click on any image for a larger view.
Figure 1. Arduino Nano 33 IoT
Figure 2. Jumper wires. You can also use pre-cut solid-core jumper wires.
Figure 3. A solderless breadboard
Figure 4. An 8 ohm speaker (optional).This is a good alternate to the LED if you prefer audible output.
Figure 5. LEDs. The long leg goes to voltage and the short leg goes to ground
Figure 6. 220-ohm resistors. These ones are 4-band resistors. They are colored red, red, brown and gold, which signifies 2, 2 (red, red), times 10 (brown), with a 5% tolerance (gold).
Figure 7. 10-kilohm resistors. These ones are 5-band resistors. They are colored brown, black, black, red, brown, which signifies 1 (brown), 0, 0 (black, black), times 100 (red), with a 1% tolerance (brown). Four-band 10-kilohm resistors are colored brown, black, orange (1, 0, times 1000), gold (5% tolerance).
Figure 8. A pushbutton. Any switch will do the job as well.
If you’re using a brand new breadboard, you might want to check out these videos before you get started, to prep your board and care for your microcontroller.
Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino Uno, use the 5V or 3.3V (depending on your model) and any of the ground connections. Figures 9 and 10 show how to do this for an Arduino Uno and for an Arduino Nano 33 IoT.
Figure 9. Breadboard view of an Arduino Uno on the left connected to a solderless breadboard, right.
As shown in Figure 9, the Uno’s 5V output hole is connected to the red column of holes on the far left side of the breadboard. The Uno’s ground hole is connected to the blue column on the left of the board. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus.
Figure 10 Breadboard view of an Arduino Nano mounted on a solderless breadboard.
As shown in Figure 10, the Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.
The Nano, like all Dual-Inline Package (DIP) modules, has its physical pins numbered in a U shape, from top left to bottom left, to bottom right to top right. The Nano’s 3.3V pin (physical pin 2) is connected to the left side red column of the breadboard. The Nano’s GND pin (physical pin 14) is connected to the left side black column. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus. The blue columns (ground buses) are connected together at the bottom of the breadboard with a black wire. The red columns (voltage buses) are connected together at the bottom of the breadboard with a red wire.
Connect a pushbutton to digital input 2 on the Arduino. Figures 11 and 12 show the schematic and breadboard views of this for an Arduino Uno, and Figure 13 shows the breadboard view for an Arduino 33 IoT. The pushbutton shown below is a store-bought momentary pushbutton, but you can use any pushbutton. Try making your own with a couple of pieces of metal as shown in the Switches lab.
If you’re not sure what pins are the inputs and outputs of your board, check the Microcontroller Pin Functions page for more information. The reference page on the standard breadboard layouts for the Uno, Nano series, and MKR series might be useful as well.
Figure 11. Schematic view of an Arduino connected to a pushbutton.
Figure 12. Breadboard view of an Arduino connected to a pushbutton.
Figure 13. Breadboard view of an Arduino Nano connected to a pushbutton.
Figure 13 shows the breadboard view of an Arduino Nano connected to a pushbutton. The +3.3 volts and ground pins of the Arduino are connected by red and black wires, respectively, to the left side rows of the breadboard. +3.3 volts is connected to the left outer side row (the voltage bus) and ground is connected to the left inner side row (the ground bus). The side rows on the left are connected to the side rows on the right using red and black wires, respectively, creating a voltage bus and a ground bus on both sides of the board. The pushbutton is mounted across the middle divide of the solderless breadboard. A 10-kilohm resistor connects from the same row as pushbutton’s bottom left pin to the ground bus on the breadboard. There is a wire connecting to digital pin 2 from the same row that connects the resistor and the pushbutton. The top left pin of the pushbutton is connected to +3.3V.
Note on The Pulldown Resistor
What happens if you don’t include the resistor connecting the pushbutton to ground? The resistor connecting the pushbutton is a pulldown resistor. It provides the digital input pin with a connection to ground. Without it, the input will behave unreliably.
If you don’t have a 10-kilohm resistor for the pushbutton, you can use any reasonably high value. 4.7K, 22K, and even 1 Megohm resistors have all been tested with this circuit and they work fine. See the digital input and output notes for more about the digital input circuit.
If you’re not sure about the resistor color codes, use a multimeter to measure the resistance of your resistors in ohms, and check this resistor color code calculator.
Add Digital Outputs (LEDs)
Connect a 220-ohm resistor and an LED in series to digital pin 3 and another to digital pin 4 of the Arduino. Figures 14, 15, and 16 below show the schematic view as well as the breadboard view for both the Uno and the Nano. If you prefer an audible tone over a blinking LED, you can replace the LEDs with speakers or buzzers. The 220-ohm resistor will work with LED, speaker, or buzzer.
Figure 14. Arduino connected to pushbutton and two LEDs, Schematic view.
Figure 15. Arduino Uno connected to pushbutton and two LEDs, Breadboard view.
Figure 16. Arduino Nano connected to pushbutton and two LEDs, Breadboard view.
Note on LED Resistor Values
For the resistor on the LED, the higher the resistor value, the dimmer your LED will be. So 220-ohm resistors give you a nice bright LED, 1-kilohm will make it dimmer, and 10K or higher will likely make it too dim to see. Similarly, higher resistor values attenuate the sound on a speaker, so a resistor value above 220-ohm will make the sound from your speaker or buzzer quieter.
Make sure you’re using the Arduino IDE version 1.8.19 or later. If you’ve never used the type of Arduino module that you’re using here (for example, a Nano 33 IoT), you may need to install the board definitions. Go to the Tools Menu –> Board submenu –> Board Manager. A new window will pop up. Search for your board’s name (for example, Nano 33 IoT), and the Boards manager will filter for the correct board. Click install and it will install the board definition.
Connect the microcontroller to your computer via USB. When you plug the Arduino into your computer, you’ll find a new serial port in the Tools–>Port menu (for details on installing the software, and USB-to-serial drivers for older Arduino models on Windows, see the Arduino Getting Started Guide). In the MacOS, the name look like this: /dev/tty.usbmodem-XXXX (Board Type) where XXXX are part of the board’s unique serial number and Board Type is the board type (for example, Arduino Uno, Nano 33 IoT, MKRZero, etc.) In Windows it will be called COM and a number. Figure 17 shows the tools men and its Port submenu.
Figure 17. The Arduino Tools menu showing the Ports submenu
Now it’s time to write a program that reads the digital input on pin 2. When the pushbutton is pressed, turn the yellow LED on and the red one off. When the pushbutton is released, turn the red LED on and the yellow LED off.
In your setup() method, you need to set the three pins you’re using as inputs or outputs, appropriately.
1
2
3
4
5
voidsetup() {
pinMode(2, INPUT); // set the pushbutton pin to be an input
pinMode(3, OUTPUT); // set the yellow LED pin to be an output
pinMode(4, OUTPUT); // set the red LED pin to be an output
}
In the main loop, first you need an if-then-else statement to read the pushbutton. If you’re replacing the LED with a buzzer, the code below will work as is. If you’re using a speaker, there’s an alternative main loop just below the first one:
1
2
3
4
5
6
7
8
9
10
11
12
13
voidloop() {
// read the pushbutton input:
if(digitalRead(2) ==HIGH) {
// if the pushbutton is closed:
digitalWrite(3, HIGH); // turn on the yellow LED
digitalWrite(4, LOW); // turn off the red LED
}
else{
// if the switch is open:
digitalWrite(3, LOW); // turn off the yellow LED
digitalWrite(4, HIGH); // turn on the red LED
}
}
Here’s an alternate loop function for an audible output on two speakers. If you want to use only one speaker, try alternating the tone frequency from 440Hz (middle A) to 392Hz (middle G):
1
2
3
4
5
6
7
8
9
10
11
12
13
voidloop() {
// read the pushbutton input:
if(digitalRead(2) ==HIGH) {
// if the pushbutton is closed:
tone(3, 440); // turn on the first speaker to 440 Hz
noTone(4); // turn off the second speaker
}
else{
// if the switch is open:
noTone(3); // turn off the first speaker
tone(4, 440); // turn on the second speaker to 440 Hz
}
}
Once you’re done with that, you’re ready to compile your sketch and upload it. Click the Verify button to compile your code. Then click the Upload button to upload the program to the module. After a few seconds, the following message will appear in the message pane to tell you the program was uploaded successfully. Related video: Upload the code to the Arduino
Binary sketch size: 5522 bytes (of a 7168 byte maximum)
Press the pushbutton and watch the LEDs change until you get bored. That’s all there is to basic digital input and output!
The Uno vs Newer Boards
If you’ve used an Uno r3 board before (the “classic Uno”), and are migrating to the Nano or a newer board, you may notice that the serial connection behaves differently. When you reset the MKR, Nano, Uno R4, or Leonardo boards, or upload code to them, the serial port seems to disappear and re-appear. Here’s why:
There is a difference between the Uno R3 and most of the newer boards like the MKR boards, the Nano 33 IoT and BLE, the Leonardo and the Uno R4: the Uno R3 has a USB-to-serial chip on the board which is separate from the microcontroller that you’re programming. The Uno R3’s processor, an ATMega328, cannot communicate natively via USB, so it needs the separate processor. That USB-to-serial chip is not reset when you upload a new sketch, so the port appears to be there all the time, even when your Uno R3 is being reset.
The newer boards can communicate natively using USB. They don’t need a separate USB-to-serial chip. Because of this, they can be programmed to operate as a mouse, as a keyboard, or as a USB MIDI device. Since they are USB-native, their USB connection gets reset when you upload new code or reset the processor. That’s normal behavior for them; it’s as if you turned off the device, then turned it back on. Once it’s reset, it will let your computer’s operating system know that it’s ready for action, and your serial port will reappear. This takes a few seconds. It means you can’t reset the board, and then open the serial port in the next second. You have to wait those few seconds until the Arduino board has made itself visible to the computer’s operating system again.
If you have problems with the UBS-native boards’ serial connection, tap the reset button once, then wait a few seconds, then see if the port shows up again once the board has reset itself. You can also double-tap the reset on the MKR and Nano boards to cause the processor to reset and go into a sleep mode. In this mode, the USB connection will reset itself, but your sketch won’t start running. The built-in LED will glow softly. Then upload a blank sketch. From there, you can start as if your board was brand new.
Many projects can be made with just digital input and output. For example, a combination lock is just a series of pushbuttons that have been pushed in a particular sequence. Consider the cymbal-playing monkey in Figures 18-20:
Figure 18. A mechanical toy monkey that plays cymbals. The cymbals are covered with aluminum foil. The foil is connected to wires, and the wires are connected to an Arduino and breadboard. The two wires from the cymbals act as a switch when they are hit together.
The monkey’s cymbals can be turned into a switch by lining them with tin foil and screwing wires to them:
Figure 19. Detail of the cymbal monkey’s cymbal. It is covered with aluminum foil, as described above.
Figure 20. Detail of the other cymbal. This one is also covered with aluminum foil.
Those wires can be run to a breadboard and used as a switch. Then the microcontroller could be programmed to listen for pattern of cymbal crashes, and if it sees that pattern, to open a lock by turning on a digital output.
Consider the project ideas from Project 1 for more applications you can do with simple input and output.
In this lab you will learn about different types of switches and their terminology
Introduction
In this lab you will learn about different types of switches and their terminology. You’ll use switches and pushbuttons frequently in physical computing projects, as shown in Figure 1, and it’s helpful to be aware of the terminology used in describing them when shopping for them or trying to understand tutorials that use them.
Figure 1. Switches and pushbuttons. There are countless types of switches and pushbuttons for every purpose.
Switch Terminology
As shown in Figure 2, there are two common types of digital inputs: switches and pushbuttons. A switch is a mechanism that brings two pieces of metal together using some form of lever action. Think of everyday household light switches. A pushbutton brings two pieces of metal together when you push down on it. Think of elevator buttons.
Figure 2. Schematic drawing of a switch and a pushbutton
Switches and pushbuttons can be normally open, meaning that when the switch is in its normal position (not being touched by a person) the contacts are not touching. Normally closed means that when the switch or pushbutton is in its normal position, the contacts are touching, or closed.
As shown in Figure 3, a single switch can control more than one set of contacts. A Single throw switch has only two contacts. The switch is open or closed. Dual Throw switches have three contacts, and switching the switch moves a center contact from one outer contact to the other outer contact.
Figure 3. Single throw switch, top, and dual throw switch, bottom.
Switches can have multiple poles as well. A Single pole switch has only one set of contacts that it closes or opens when it moves. Dual Pole switches, as shown in Figure 4, have two sets of contacts being controlled by the same mechanism. With a dual pole switch, you can switch two separate circuits with the same mechanism. In a dual pole switch, the mechanism connecting the contacts is an insulator, so that the poles don’t connect. The knife switch in the image below is a dual pole, dual throw switch.
Pushbuttons(Figure 5) or momentary switches stay closed only as long as you hold them closed. Roller switches(Figure 6) are pushbuttons with a lever and a roller attached. They’re useful when you need something to push against the switch gently to close it.
Figure 5. A pushbutton
Figure 6. A roller switch. Even though this is commonly called a switch, this is really a pushbutton. The small black protrusion under the lever is pressed down by the lever arm.
Tactile Switches are pushbuttons that have a tactile click to them (Figure 7). They are usually designed to be soldered to a circuit board, and they fit into a breadboard nicely as well. They are perhaps the most common switches you’ll use in physical computing. You can get them in a variety of colors and sizes. They generally have four pins, arranged in a rectangle. If you hold the switch so that the wide side of the rectangle of pins is horizontal, then the top two pins are generally connected to each other, and the bottom two are connected to each other. The switch is between the two wide sides. The schematic in Figure 8 shows how they are wired.
Figure 7. A variety of different tactile switches. These all have different sizes and cap shapes, but they all have the same functionality and feel. Some tactile switches, like the one at the top left, have extra long caps.
Figure 8. Schematic of a typical tactile switch. The two top pins are connected to one side of the switching mechanism inside, and the two bottom pins are connected to the other side of the mechanism.
Arcade buttons (Figure 9) are popular game consoles because they are big and robust. They often have a built-in LED that you can control independently of the switch. In this way they are similar to other illuminated switches and pushbuttons.
Figure 9. Arcade buttons come in many different sizes and shapes. They are designed to be panel-mounted. Many are illuminated with an internal LED as well.
Toggle switches (Figure10) stay closed in one physical position and open in the other. Slide switches (Figure 11) are similar to toggle switches.
Figure 10. Toggle switch. Moving the lever switches the center leg’s connection from one side leg to the other.
Figure 11. Slide switch. Sliding the handle switches the center leg’s connection from one side leg to the other.
Figure 12. Magnet switch.
Figure 13. Magnet snaps. Used for clothing, wires can be connected to the two snaps so that when they are snapped together, the wires are connected. In this way, a garment fastener becomes a switch.
Magnetic switches (Figure 12) have two metal leaves in the end that are pulled together when a magnet is brought close to them. They’re useful when you can’t have wires on both sides of the switch mechanism. Magnetic snaps (Figure 13) are useful when you’re making a soft circuit and need a fastener on the garment to close a switch.
Figure 14. Whisker switch
Whisker switches (Figure 14) are made from a piece of spring steel or piano wire, and a center post. An insulator such as a piece of electrical tape or shrink-wrap holds the two separate. When the wire is touched, the spring bends and touches the metal post, and closes the switch.
Figure 15. Tilt switch
Tilt switches (Figure 15) contain a metal ball and two wires at one end. Some tilt switches have one wire contact at each end instead. When you tilt the switch, the ball touches both contacts, and closes the switch. There are also mercury switches that do the same, but with a ball of mercury inside. Avoid these, since mercury is very poisonous.
Get Creative With Switches
A switch is nothing more than a mechanism to bring two pieces of conductive material together and separate them. You can make a switch from any two conductors and a little creativity.
Figure 16. Conductive fabric
Figure 17. Copper mesh
Figure 18. Copper tape
All you need to do is arrange the two conductors in such a way that they can touch or not touch. Sometimes a spacer layered between the two conductors helps. For example, in figure 19 you see three pieces of conductive material. Two of the pieces have non-conductive layers on top of them. When the non-conductive part is sandwiched between the conductive layers, you’ve got a switch that’s pressed by touching. The conductive parts touch when they’re pressed through the holes in the non-conductive part. These two switches would have different sensitivities because the hole-to-material ratio of the non-conductive layer is different.
Figure 19. Soft switch
Make your own switch. Find a way to turn a closing door into a switch, for example, or to close a switch when a person sits down. Or figure out how to turn a hat into a switch, or a cane, or a zipper. Or perhaps the pieces of a puzzle can be switches. Come up with an everyday activity to which you can add three or four custom switches that, when combined, turn on a light. For example, maybe the light comes on when you close the door, sit down, and open a book. Or when you walk upstairs, put your keys on a side table, and remove your hat. Combine your creativity with switches with what you learned in the electronics lab and breadboard lab to make this happen. For more ideas on materials, check out How to Get What You Want. They have an excellent list of conductive materials and instructions.
Here’s mustache switch by Tak Cheung:
Arrangements of switches
Consider what happens when you arrange switches in different ways. For example, try the following circuits.
Project 1: Three switches in parallel
Three switches in parallel, as shown in Figure 20-21. Any one of the three will turn on the LED.
Figure 20. Schematic view of three parallel switches connected to an LED.
Figure 21. Switches in parallel, breadboard view.
Project 2: Three switches in series
Three switches in series, as shown in Figure 22-23. All three must be on to turn on the LED.
Figure 22. Schematic view of three switches in series connected to an LED.
Figure 23. Breadboard view of three switches in series connected to an LED.
Through a combination of series and parallel switches, you can come up with a variety of combinations that make the light turn on. Depending on where you add the LEDs, you can even have the same switches turn on different LEDs in different combinations. Try a few combinations and see what happens.
Project 3: Switching a DC motor
In a simple circuit, a DC motor* is no different than an LED as a load. You can switch it as well, as shown in Figure 24-25. Make sure your power supply can supply the current and amperage that your motor requires and you are good to go.
* DC Motor converts direct current (DC) electrical energy into mechanical energy. Check the parts and tools guide for where to get a motor. You’ll learn more about DC motors and other motors in later labs.
Figure 24. Schematic view of a DC motor switched by a pushbutton.
Figure 25. Breadboard view of a DC motor connected to a switch.
With a dual pole switch, you could control both a DC motor and an LED. The small square pushbuttons that come with many kits for Arduino are dual pole switches. The left side of the switch and the right side of the switch can switch different loads, like shown in Figure 26-27:
Figure 26. Schematic view of a DC motor and an LED switched by a dual-pole pushbutton (switch).
Figure 27. Breadboard view of a dual-pole pushbutton (switch) controlling a DC motor and an LED.
You’ll learn more about controlling motors from a microcontroller in later labs.
This lab shows how to set up a breadboard with an independent power supply (9-12V) through a 5V Voltage Regulator (7805).
Introduction
The easiest way to get started building electronic circuits is by using a solderless breadboard. A breadboard is a tool for holding the components of your circuit, and connecting them together. It’s got holes that are the right size for hookup wires and the ends of most components, so you can push wires and components in and pull them out without much trouble. This lab shows how to set up a breadboard, both with an Arduino and with an independent power supply (9-12V) through a 5V Voltage Regulator (7805). By the time you finish the lab, you should have an understanding of how the holes in a solderless breadboard are connected, and how to configure your breadboard for different microcontroller projects, and even for projects without a microcontroller.
You won’t always need a voltage regulator. For most microcontroller circuits, you’ll get power from your computer’s USB port, regulated through your microcontroller, to power sensors and LEDs. But for higher current projects or higher voltage projects involving components like motors or larger light sources, it’s good to know what a voltage regulator is and how to use it.
If you’re using a brand new breadboard, you might want to check out these videos as well before you get started, to prep your board and care for your microcontroller.
What You’ll Need to Know
To get the most out of this lab, you should be familiar with the following concepts beforehand. If you’re not, review the links below:
All about DC Power supplies – this page will introduce you to DC power supplies. You probably have several in your house already, and this lesson will tell you more about how they work and how you can use them.
Safety Warning: When inserting components on or removing components from a breadboard always unplug power supply first! You are likely to damage your components if you are changing your circuit while it is still powered.
Things You’ll Need
Figure 1. A short solderless breadboard.
Figure 2. 22AWG solid core hookup wires.
Figure 4. A DC Power Jack
Figure 5. 5-volt voltage regulator, model 7805
Figure 6. Wire stripper tool
Figure 7. Multimeter tool
Figure 8. LEDs. Shown here are four LEDs. The one on the right is an RGB LED. You can tell this because it has four legs, while the others have only two legs.
Figure 9. Arduino Nano 33 IoT. You could use any model of Arduino instead, for this lab.
Setting up the Breadboard
Figure 10. Solderless breadboard with a 7805 voltage regulator mounted on it.
Figure 10 shows a typical breadboard with a 7805 5-Volt voltage regulator mounted on it. There are several rows of holes for components. The holes on the breadboard are separated by 0.1-inch spaces, and are organized in many short rows in the center, and in two long rows down each side of the board. The short horizontal rows in the middle are separated by a center divider. The pattern varies from model to model; some breadboards have only one strip down each side, others have multiple side rows, and some have no side rows at all.
On each side of the board are two long rows of holes, with a black or a red line next to each row (on many boards, you’ll see a blue row instead of black). All the holes in each of these lines are connected together with a strip of metal in the back. In the center are several short rows of holes separated by a central divider. All of the five holes in each row in the center are connected with a metal strip as well. This allows you to use the holes in any given row to connect components together. To see which holes are connected to which, take a multimeter and a couple of wires, set the multimeter to measure continuity, stick the two wires in two holes, and measure them with the multimeter. If the meter indicates continuity, then the two holes in question are connected.
What’s Inside A Breadboard?
The image below (Figure 11) of the back of a breadboard may help to clear up how the holes on the front of the board are connected. The backing of the board has been removed (don’t remove the backing on your own board! It will make the board useless) to expose the metal strips connecting the holes. You can clearly see the short horizontal strips in the center separated by the divider, and the long strips vertically down the side. The detailed photo in Figure 12 illustrates how the holes and strips are related. Each of the holes connected to the horizontal strips are connected, and each of the holes connected to each long vertical side strips are connected to each other.
Figure 11. The back of a breadboard, shown with the backing removed.
Figure 12. A close-up picture of the front of the breadboard. The green lines indicate which holes are connected by a metal strip behind.
The reason for the center divider is so that you can mount integrated circuit chips, like a microprocessor, on the breadboard. IC chips in a DIP package (Dual In-line Package) have two rows of pins that to which you need to connect other components. The center row isolates the two rows from each other, and gives you several holes connected to each pin, so that you can connect other components.
Powering the Breadboard
Avoid adding, removing, or changing components on a breadboard whenever the board is powered. You risk shocking yourself and damaging your components.
The most common connections you need on a breadboard are voltage and ground. That’s why solderless breadboards are designed the way they are. The long rows of connected holes down either side are designed to be used as power and ground buses. A bus is a long electrical connection that has many points where you can connect to it. Figure 13 shows the typical wiring on a breadboard, connecting the bus rows on the left with those on the right.
Figure 13. A solderless breadboard with the voltage and ground buses indicated on the sides. The left and right sides of the board are connected via two wires which connect the voltage bus on the left with the one on the right, and the ground bus on the left with the one on the right.
Next you’ll see how to connect an Arduino microcontroller to your breadboard to supply power to the components on the board. Later on, you’ll see how to power a board without a microcontroller.
Using a Breadboard with a Microcontroller
For most projects in this class you’ll power your circuit via a microcontroller like the Arduino Nano 33 IoT or the Uno. To do that, your breadboard will usually be connected as shown in Figures 14 and 15. You’ll connect the voltage and ground buses on the breadboard to voltage and ground from the microcontroller. On the Arduino module, you can use the 5V or 3.3V output, depending on the circuit you are using and depending on your model. Typically, you’ll use 5V on the Uno and 3.3V on the Nano 33 IoT.
Figure 14. Breadboard view of an Arduino Uno on the left connected to a solderless breadboard, right.
As shown in Figure 15, the Uno’s 5V output hole is connected to the red side column of holes on the far left side of the breadboard; this is the voltage bus. The Uno’s ground hole is connected to the blue side column on the left of the board, or ground bus. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. With this setup, you can supply 5V to any component on the breadboard, using the Uno’s onboard 5-volt regulator. You can power the Uno either through USB or through a 9-12V power supply plugged into the Uno’s power jack. There is a limit to the current that you can supply from this regulator, though, about 1 amp at 5V.
If you’re using an Uno for this lab, connecting the circuit in Figure 14, then set your multimeter to measure DC voltage, and measure the voltage between the voltage and ground buses of your breadboard. When the Uno is plugged into a USB power supply, you should get 5V.
Figure 15. Breadboard view of an Arduino Nano mounted on a solderless breadboard.
In Figure 15, you see a similar setup for the Arduino Nano 33 IoT. The Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.
The Nano, like all Dual-Inline Package (DIP) modules, has its physical pins numbered in a U shape, from top left to bottom left, to bottom right to top right. The Nano’s 3.3V out pin (physical pin 2) is connected to the left side red column of the breadboard. This is the voltage bus. The Nano’s GND pin (physical pin 14) is connected to the left side black column, the ground bus. The blue columns (ground buses) are connected together at the bottom of the breadboard with a black wire. The red columns (voltage buses) are connected together at the bottom of the breadboard with a red wire. Wired like this, you can supply 3.3V to any component on the breadboard through the Nano’s onboard 3.3V regulator. There is a limit to the current that you can supply from this regulator, though, about 1 amp at 3.3V.
If you’re using a Nano for this lab, connecting the circuit in Figure 15, then set your multimeter to measure DC voltage, and measure the voltage between pin 2 of the Nano (3.3V out) and pin 14 (ground). When the Nano is plugged into a USB power supply, you should get 3.3V.
Powering A Breadboard Circuit From A Microcontroller Via A DC Power Supply
Quite often, you’ll want to supply power to a circuit through an Arduino. Sometimes, you’ll have components that run at the microcontroller’s voltage, and sometimes they will require a higher voltage. From both the Nano and the Uno, if you power them from a DC power supply, you can supply 5V, 3.3V and the voltage of the power supply.
When you’re not powering it via its USB connector, the Nano 33 IoT and the Nano Every can be powered through the Vin pin (pin 15, bottom left side of the module) by a voltage range from 7 – 21 volts. This wide range means that you can use a 9V battery, or a DC power supply anywhere from 7-21V. When you power the Nano this way, you get 3.3V from the 3V3 pin (pin 2) just as if you were powering from USB. If you have components that need a higher voltage, you can supply them directly from your power supply via the Vin pin. Figure 16 shows a Nano powered from a 12V DC power jack. The Jack’s positive terminal is connected to the Vin pin (pin 15) and the negative terminal is connected to the ground pin (pin 14).
Figure 16. Breadboard view of a Nano 33 IoT on a breadboard connected to a DC power jack for external DC powering. The jack’s positive terminal is connected to the Nano’s Vin pin (pin 15) and the negative terminal is connected to ground (pin 14). In this configuration, the Nano will run, and will output 3.3V between the 3V3 pin (pin 2) and ground.
When you are using a DC power supply as shown in Figure 16, you can also add a 5V regulator powered by the power supply, if you need both 3.3V and 5V for your components. Figure 17 shows a Nano supplied by a DC power jack on the Vin and ground pins, and a 5V regulator attached to the Vin pin as well, to supply 5V for components that need that voltage.
Try connecting the circuit in Figure 17, then set your multimeter to measure DC voltage, and measure the voltage between pin 2 of the Nano (3.3V out) and pin 14 (ground). When the DC power jack is plugged in, you should get 3.3V. Now move the positive (red) probe of your meter to the bottom pin of the voltage regulator (row 21 on the board). You should read 5V. Now move the positive probe to the Nano’s pin 16 (Vin). You should read whatever the voltage of the DC power supply is, somewhere between 7 and 21V.
Figure 17. Breadboard view of a Nano 33 IoT and a 7805 voltage regulator powered from a DC power jack. The DC power jack is wired as shown in the previous figure (positive terminal to Vin, negative terminal to ground. However, in this image, a 5V regulator is added on the breadboard below the Nano. Its Vin pin is connected to the Vin of the Nano (and the DC power supply’s positive terminal) and its ground is connected to the ground bus. In this circuit, you can supply 3.3V via the Nano’s 3V3 pin (pin 2), 5V from the 7805 regulator, and 7-21V from the DC power supply.
The Uno accepts 7-12V in via its DC power jack, and it has two voltage regulators on board. Pin 5 on the left side of the board supplies 5V, and pin 4 on the left side supplies 3.3V. Pin 8 on the left side is connected directly to the Vin, so you can supply 7-12V, depending on the voltage of your DC power supply, from that pin. For example, figure 18 shows an Uno connected to a 9V battery and supplying 9V to the breadboard via the Vin pin.
Figure 18. Uno connected to a 9V battery via a battery clip. The Uno’s Vin pin (pin 8 on the left side) is connected to a breadboard’s voltage bus, and one of the ground pins (pin 7) is connected to the ground bus. The Uno is thus supplying 9V for any components on that breadboard.
If you’re using an Uno for this lab, connecting the circuit in Figure 18, then set your multimeter to measure DC voltage, and measure the voltage between the voltage and ground buses of your breadboard. When the Uno is plugged into a DC power supply, you should get whatever the voltage of the DC power supply is, between 7 and 12V.
With an external power supply and a voltage regulator, you can supply almost any voltage your components might need, while still giving the Arduino the voltage it needs to run. In general, you will always connect the ground lines together, and keep the different voltages separate from each other. To see an application of this, see the Using a Transistor to Control High Current Loads with an Arduino Lab.
You can use the circuits above as templates for almost any microcontroller-driven project. Even if you’re just using the microcontroller to power the breadboard, as shown below, you can still work with these circuits as a base.
Powering the Breadboard without a Microcontroller
Figure 19. Close-up showing a 7805 5-volt voltage regulator connected to a breadboard.
Sometimes you’ll want to build circuits without a microcontroller. This section shows you how to set up a breadboard without a microcontroller. You’ll add a 5-volt voltage regulator, and power the board from a 12-volt DC power adapter which feeds the regulator.
The regulator in Figure 19 is used to supply 5 Volts to the red side columns of the breadboard. In this image, you’re looking at the board sideways, with the the back of the regulator facing you. The pins of the regulator are, from left to right: voltage out, ground, voltage in. Notice that the regulator’s voltage out pin on the left is connected to the red side row of the breadboard with a red wire. The regulator’s center pin (ground) is connected to the black side row. These will be your voltage and ground bus rows. The side rows on one side are connected to the corresponding side rows on the other side via two wires. The two black rows are connected to each other via a black wire, and the red rows are connected via a red wire.
With your board connected like this, you’ll be able to build many different 5-Volt circuits on the board. The last thing you need to add is a power connector to connect 9 – 12 volts DC to supply power for the voltage regulator. Figure 20 below shows a power connector connected to the voltage input and ground pins of the voltage regulator. This circuit is now the same as the one shown in Figure 17 above, but with the Arduino Nano removed.
Figure 20. Board with 5V voltage regulator and power connector ready to go.
Will it Light? Test Your Understanding
Figures 22-30 below show an LED and a resistor connected in a breadboard. Some are connected correctly and others are not. Take a guess as to whether the LED will light or not, then click the links below the image to find out. All of the circuits below are versions of the same circuit, shown in the schematic in Figure 21:
Figure 21. Schematic image of a 220-ohm resistor and an LED connected to a 7805 5-volt regulator. Regulator pins are numbered from left to right. One terminal of the resistor is connected to the regulator’s output pin (pin 3), and the other terminal is connected tothe LED’s anode. The LED’s cathode is attached to the regulator’s ground pin (pin 2). A 9-12V DC power source is connected to the regulator’s voltage in (pin 1) and ground (pin 2). Download the tactile schematic as SVG. Download the tactile schematic key as SVG.
These circuit images will test your understanding of how the breadboard works. You can build the circuits yourself, using any one of Figures 14, 15, 17 or 20 to supply 5V for the LED circuit.
If your board is wired as in Figure 22, will the LED below light up when you power the board? Take a guess, then click the link below the image to find out.
Figure 22. Photo of a solderless breadboard with a 7805 voltage regulator mounted on it, as in the images above. The regulator is connected to a DC power plug, and the ground and voltage output of the regulator is connected to the voltage and ground bus rows on the right side of the breadboard. The ground and voltage bus rows on the right are connected to the ground and voltage bus rows on the left with wires at the bottom of the board. A 220-ohm resistor is connected to the voltage bus on the left. Its other end is connected to row twelve in the center area. An LED is connected to another hole in row twelve. The other side of the LED is connected to a hole in row fourteen. Another hole in row fourteen connects to the ground bus on the right side of the board.
Figure 23. This image shows the same circuit as shown above, but the LED is lit up.
When wired as in Figures 22 and 23, the LED will light up because the circuit is wired correctly. The power flows from bus row through the resistor to row twelve of the board. The LED’s anode connects to the same row, and the cathode is in another row, row fourteen. Then the black wire connects that second row to the ground bus, completing the circuit. The LED will light.
Try another one. When wired as in Figure 24, will the LED below light up when you power the board? Take a guess, then click the link below the image to find out.
Figure 24. Photo of a solderless breadboard with a 7805 voltage regulator mounted on it, as in the images above. The regulator is connected to a DC power plug, and the ground and voltage output of the regulator is connected to the voltage and ground bus rows on the right side of the breadboard. The ground and voltage bus rows on the right are connected to the ground and voltage bus rows on the left with wires at the bottom of the board. A 220-ohm resistor is connected to the voltage bus on the left. Its other end is connected to row twelve in the center area. An LED’s anode is connected to a hole in row twelve on the right side. The cathode of the LED is connected the ground bus on the right side.
The photo above, Figure 24, is wired incorrectly. The power flows from bus row through the resistor to row twelve of the board. The LED’s anode connects to the same row, but on the other side of the center divide, so there is a break in between the resistor and the LED. The LED will not light.
Here’s a third one, Figure 25. Will the LED below light up when you power the board? Take a guess, then click the link below the image to find out.
Figure 25. Photo of a solderless breadboard with a 7805 voltage regulator mounted on it, as in the images above. The regulator is connected to a DC power plug, and the ground and voltage output of the regulator is connected to the voltage and ground bus rows on the right side of the breadboard. The ground and voltage bus rows on the right are connected to the ground and voltage bus rows on the left with wires at the bottom of the board. A 220-ohm resistor is connected to the voltage bus on the left. Its other end is connected to row thirteen in the center area. A red wire connects row thirteen on the left with row thirteen on the right. An LED’s anode is connected to a hole in row thirteen on the right side. The cathode of the LED is connected the ground bus on the right side.
Figure 26. This image shows the same circuit as shown above, but the LED is lit up.
The photos above, Figures 25 and 26, are wired correctly. It’s almost the same as Figure 24, but now the two sides of the junction row are connected by a wire, completing the circuit. The LED will light up.
Here’s another test. In Figure 27, will the LED below light up when you power the board? Take a guess, then click the link below the image to find out.
Figure 27. Photo of a solderless breadboard with a 7805 voltage regulator mounted on it, as in the images above. The regulator is connected to a DC power plug, and the ground and voltage output of the regulator is connected to the voltage and ground bus rows on the right side of the breadboard. The ground and voltage bus rows on the right are connected to the ground and voltage bus rows on the left with wires at the bottom of the board. A 220-ohm resistor is connected to the voltage bus on the left. Its other end is connected to row fifteen in the center area. An LED is connected to two holes in row fifteen of the left side of the board as well. A black wire connects row fifteen on the left side of the board to the ground bus on the right side of the board.
The photo above, Figure 27, is wired incorrectly. Both of the LED’s connections are in the same junction row as the end of the resistor, so the LED is bypassed by the conductor under the row. The schematic below, Figure 28, shows what’s happening electrically. The LED will not light. This is a short circuit. The schematic below shows it.
Figure 28. Schematic image of a 220-ohm resistor and an LED connected to a 7805 5-volt regulator. The LED is bypassed by a wire, however, so the current bypasses the LED entirely.
Here’s a final test. In Figure 29, will the LED below light up when you power the board? Take a guess, then click the link below the image to find out.
Figure 29. Photo of a solderless breadboard with a 7805 voltage regulator mounted on it, as in the images above. The regulator is connected to a DC power plug, and the ground and voltage output of the regulator is connected to the voltage and ground bus rows on the right side of the breadboard. The ground and voltage bus rows on the right are connected to the ground and voltage bus rows on the left with wires at the bottom of the board. A 220-ohm resistor is connected to the voltage bus on the right. Its other end is connected to row twenty in the on the right side of the center area. An LED is connected to a hole in row twenty on the right side as well. The other side of the LED is connected row twenty on the left side. A black wire connects row twenty on the left with the ground bus on the left side.
Figure 30. This image shows the same circuit as shown above, but the LED is lit up.
The photos above, Figures 29 and 30, are wired correctly. The resistor connects to power on the right, the resistor spans the center divider, and the black wire connects to ground on the left. The LED will light.
If you connected an LED and resistor just as it’s shown in Figures 29 and 30, and it didn’t light, check to make sure that the anode (the long leg) of the LED is connected to the resistor, and the cathode (the short leg) is connected to the ground wire.
Below, in Figure 31, the three LED’s are connected in parallel using two rows. They are then connected to power and ground by connecting the rows to the voltage row and the ground row. These three LEDs are in parallel with each other.
Figure 31. LEDS in parallel on a breadboard
Many options are possible using a breadboard, which is what makes them very useful and convenient for building circuits. Once you understand which holes are connected to each other (and which ones are not), you can build any circuit very quickly.
Breadboarding Best Practices
It’s a good idea to keep your circuits neat. When possible, shorten the leads on components so there is no bare metal sticking up from the breadboard. Make sure no wires cross each other with metal touching (this is the biggest source of short circuits on a breadboard). Lay things out as sensibly as possible, so each component of the circuit is near the components it needs to connect to. Use wires when needed to separate parts of the circuit that are crowded together. Use consistent colors of wires when possible; for example, use green or black for ground connections, red for power connections, white or blue for data connections, and so forth. This will make your troubleshooting much easier.