Breadboard - Getting Started with Spark Core and Photon (2015)

Getting Started with Spark Core and Photon (2015)

Chapter 4. Breadboard

In this chapter we will deal with attaching some simple external electronics to the Photon or Core, including LEDs and switches. To connect electronic components like LEDs to the Photon/Core you will use something called solderless breadboard.

Solderless breadboard (or just breadboard) was invented as a tool for electronics engineers to prototype their designs before committing the projects to a more permanent soldered form. They do, however, make an excellent tool for you to experiment with electronics and make your own projects without the need for any soldering.

When you buy a Photon or Core, one of the options available to you is for it to include a breadboard and header pins. If you did not select this option, but rather bought a bare Photon, then don’t worry, you will find sources for the header pins and breadboard that you will need in Appendix A.

How Breadboard Works

Behind the holes in the front plastic face of a breadboard there are metal clips designed to grip wires and component leads. These clips are arranged into rows of five in two banks of 30 rows. At least they are in the fairly standard “half-sized" breadboard that is included as an option when you buy a Photon.

Down each side of the breadboard are pairs of long clips that are marked with red and blue lines on the front of the breadboard. These are often used when connecting power to various parts of your breadboard design.

Attaching an LED

LEDs emit light when a current passes through them. Unfortunately, LEDs are kind of greedy for current and have so little restraint that, when fed with a source of voltage like an output pin on a Photon/Core, they will draw as much current as they can. This can lead to their overheating, shortening their life, or even damaging the Photon.

To prevent such problems, LEDs are used with the electronic equivalent of a gastric-band that restricts the flow of current into the LED. The component that restricts the flow of current in this way is called a resistor.

Figure 4-3 shows a schematic diagram for using a resistor with an LED.

Figure 4-3. Figure 4-3. Limiting the Current to an LED with a Resistor

The two pins of the Photon that are used are GND and D7. GND or ground is the base voltage against which any other voltages on the Photon are measured. For example the 3V3 pin is 3.3V above (more positive than) GND. Current will only flow from a higher voltage to a lower one, so if the digital output D7 is at 0V (called LOW) no current will flow, but if it is at 3.3V (HIGH) then the current can flow out of D7 through the resistor (zig-zag line) through the LED and down to GND. You may find that it helps to think of current flowing like water from somewhere high down to sea-level.

Digital Outputs

The pins on your Photon/Core labelled D0 to D7 as well as the pins A0 to A5 can all be used as digital outputs. That is, you can write some commands in your program that will turn them on or off. More accurately the pins are either set to 3.3V or 0V (GND).

Because the pins can be used as either inputs or outputs, the first thing you need to do in your setup function, for all the pins you are using, is to specify whether that pin is to be used as an input or an output. In the Digital Inputs section in this chapter, you will find out how to specify a pin as being an input. To specify that a pin is going to be used as an output, include the following command in your setup function:

pinMode(pin, OUTPUT);

When it comes to actually setting the pin HIGH or LOW, you use the command digitalWrite. So, assuming that the variable pin is set to one of the pins (say D7), then to set the pin HIGH you would use:

digitalWrite(pin, HIGH);

and to set it low, you would use:

digitalWrite(pin, LOW);

In Project 4, you will use these commands to make an external LED blink Morse Code.

Project 4. Morse Flasher (External LED)

Now that you know how to use an LED with a Photon or Core, you can change the Morse Code flasher of Project 3 to use an external LED rather than the built-in RGB LED.


To build this project, you are going to need the following parts in addition to your Photon or Core.



Appendix A code


Red or for that matter any other color LED



220Ω resistor


Male to Male jumper wire


Half-sized breadboard


Table 4-1. Parts Bin

These components may be included in the Spark Maker’s Kit.


We can convert the schematic of Figure 4-2 into a breadboard layout as shown in Figure 4-3.

Figure 4-4. Figure 4-4. Breadboard Layout for LED and Photon

Start by pushing one end of the resistor (it does not matter which) into the same row as D7 on the Photon/Core and then put the other leg of the resistor into row 4 of the breadboard.

The LED will have one leg longer than the other. The longer leg is the positive leg and this is the leg that should go to the same row as the resistor.

Finally, connect a jumper wire from the row of the breadboard on the lefthand side, which is connected to one of the Photon/Core GND pins, to the top row that the LED is connected to.


If you just want to check that your hardware is working, then you can flash the “Blink an LED” example app from the Examples section of the Web IDE onto your Photon/Core. This is the same program that you ran in Project 1.

When this is running, you should see both the built-in LED and the LED on the breadboard blinking in time.

If it doesn’t work, then check over the connections carefully and make sure that the LED is the right way around. Note that if you put the LED in the wrong way around it will not do any harm, it just won’t work.

Once you have the basic blink working, you can load up the app from this project, which you can find with all the projects for this book in the public Library called PHOTON_BOOK. The files is called P_04_Morse_External_LED.

There are just a few differences between this program and that of Project 3. They are all concerned with the change from using the built-in RGB LED and pin D7. The first change is the addition of a new variable to specify which pin to use:

int ledPin = D7;

The setup function is also slightly different, because now you need to set the ledPin to be an output.

void setup() {

pinMode(ledPin, OUTPUT);


The only other code comes right at the end of the program in the flash function:

void flash(int duration) {

digitalWrite(ledPin, HIGH);


digitalWrite(ledPin, LOW);


Now, the flash function will turn the ledPin on and off rather than the built-in RGB LED.

Note that both the built-in tiny blue LED and the LED on the breadboard will both flash the message. The internal LED on pin D7 consumes very little current and has negligible effect on the use of that pin as an output.

Attaching a Switch

Whereas an LED is probably the most common use of a digital output on a microcontroller like the Photon, a switch is probably the most common input.

Figure 4-5 shows the schematic diagram for attaching a switch to digital pin D3 of a Photon.

Figure 4-5. Figure 4-5. Connecting a Switch to a Photon

The symbol for the switch is a little strange. You can see the switch contacts in the center of the switch and the two left-most terminals of the switch are used. One terminal going to the pin that will act as a digital input and one to GND. So when the switch is pressed, the digital input D3 will be connected to GND (0V).

The type of switch shown here, and used in the next project, is called a tactile push switch and usually has four pins. This gives it greater mechanical strength when its soldered onto a PCB (or used with breadboard). The two unused connections of the switch on the right, just represent these extra pins.

In some schematic diagrams for using switches with a microcontroller, you may find a “pull-up” resistor connected between the digital input and 3.3V. The point of this resistor is to pull the input up to 3.3V (HIGH) until the switch is pressed where the switch will override the pulling effect of the resistor and make the input LOW. This is not necessary with the Photon or Core as these devices digital pins have built-in pull-up resistors that can be enabled in your code.

Switches and Outputs

The switch connects D3 to GND, which is just fine as long as D3 is set to be an input. However, if the program currently running on the Photon/Core is a digital output and that output happened to be HIGH (3.3V) then pressing the switch would connect the 3.3V output to GND.

This is very bad and would quite likely damage the Photon or Core. So when using a Photon with digital inputs, it is a good idea to load the app up first, before you start wiring up the hardware, so that there is no danger of a digital output being short-circuited to GND.

Digital Inputs

Before you can use a pin of the Photon/Core, you have to use the pinMode function to set its mode to be an input. Specifying that a pin should be used as an input actually has three possible types of digital input:

§ INPUT - no pull-up resistors. This would typically be used for connecting the input to digital outputs of a chip or module

§ INPUT_PULLUP - pull-up resistor enabled. Use this when connecting the digital input to a switch that will switch the input to GND

§ INPUT_PULLDOWN - pull-down resistor enabled. Use this when connecting the digital input to a switch that will switch the input to 3.3V

While it might seem more logical to use the last option and connect your switches to 3.3V so that pressing the button takes the input HIGH. It is more common to use the INPUT_PULLUP option and connect the switch to GND. This is partly because the Arduino boards do not have the third option, but also if a pin is accidentally left as an output (see warning box) then it will by default be LOW, so an accidental short circuit as a result of pressing the button is reduced.

So, to use the input connected to a switch, as I recommend and as shown in Figure 4-5, you would add the following command to your setup function.

pinMode(switchPin, INPUT_PULLUP);

In project 5, the Morse Flasher project will be modified to only start flashing when a button is pressed.

Project 5. Morse Flasher with Switch

In this project Project 4 will be expanded to include a tactile push switch. When the switch is pressed, the message will be flashed out in Morse Code.


To build this project, you are going to need the following parts in addition to your Photon or Core.



Appendix A code


Red or for that matter any other color LED.



220Ω resistor



Tactile push switch


Male to Male jumper wires


Half-sized breadboard


Table 4-2. Parts Bin

These components are all included in the Inventors Kit and apart from the switch are the same as for Project 4.


You can find the software for this project in the file P_05_Morse_Switch. Get this loaded onto your Photon/Core, before you wire up the switch.

The key differences between this program and the program for Project 4 are that there needs to be a new variable to specify the input pin:

int switchPin = D3;

In the setup function, this new pin needs to be specified as an input with pull-up resistor enabled.

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(switchPin, INPUT_PULLUP);


Finally, the loop function needs to be changed so that the message is only flashed if the button is pressed. We can also do away with the delay on the end of loop, because the message will only be flashed when the button is pressed, rather than every time around the loop.

void loop() {

if (digitalRead(switchPin) == LOW) {




The if command tests to see if the result of doing a digital read on the switch pin is LOW. Remember that the input will be LOW when the switch is pressed, otherwise it will be “pulled-up" HIGH.


The breadboard layout for this project starts the same as that of Project 4. So, if you still have Project 4 on breadboard, you can just add the switch and two jumper wires that lead to it (Figure 4-6).

Figure 4-6. Figure 4-6. Breadboard Layout for Project 5

Note that the push switch will just fit across the central divide between the two banks of breadboard rows. This will also ensure that it is the right way around and that the connections will go to a pair of pins that are separated by just one breadboard hole.

Running the Project

Since you have already made all the wiring, you should find that pressing the switch will start the Morse Code flashing.

Analog Outputs

Although you have experimented with digital outputs, that is, turning something on and off, you may also want to say control the brightness of an LED or the speed of a motor. To do this, you need to use an analog output.

Certain of the Photon’s pins (pins D0, D1, D2, D3, A4, A5) and the Core’s pins (D0, D1, A0, A1, A4, A5, A6, A7) can be used as analog outputs. These outputs are not true analog outputs, but rather they are outputs that control the power to things that they are connected to by rapidly switching on and off (see the side bar on Pulse Width Modulation).


A PWM output of a Photon or Core will produce 500 pulses every second.

If the pulse is quite short (say only HIGH for 5% of the time), then a relatively small amount of energy to whatever the pin is powering, so if it is an LED, the LED will appear dim, because is actually only on for 5% of the time. It switches so quickly, that the human eye cannot tell that its actually flashing really fast and just registers it as being dim. The same principal can be used to control a motor, only a small “kick" of energy is being given to the motor with each pulse.

If the PWM output is high for half of the time, then whatever is being powered (the load) will receive half-power and if the PWM output is high for most of the time then it will get a lot more power.


To control a PWM output, the analogWrite command is used. This command takes two parameters, as illustrated in the example below:

analogWrite(ledPin, 127);

The first parameter is the pin to be used as a PWM output and the second parameter is called the duty cycle. That is how long each pulse should be high. The value of duty cycle is a number between 0, which means permanently off, and 255 which means permanently on. So a value of 127 will put the output at pretty much half power.

If you are using a pin as a PWM output, you still have to define it as an output using pinMode.

An Example

As an example of using PWM, you can modify the hardware that you used in Project 5 to use a pin that is PWM capable. To do this, just move the resistor lead currently connected to D7 to A5. The revised breadboard layout is shown in Figure 4-8.

Figure 4-8. Figure 4-8. Revised Breadboard Layout for PWM

Flash the following program that can be found in the PHOTON_BOOK examples project as ch_03_pwm_output.

print('hello world')

Initially, the LED will be off, but then each time that you press the button it will get a little brighter. After a few button presses the LED will cycle back around to being off.

A variable called brightness is used to keep track of the current duty cycle of the ledPin. This is initialized to 0.

The loop function starts with an if command that checks to see if the button is pressed. If it is then 25 is added to the variable brightness. If the value of brightness exceeds 255 then it is set back to 0.

The duty cycle of the ledPin is set to the current value of brightness and then a delay of 200 milliseconds prevents the value of brightness immediately changing again because the button is still pressed.

A Real Analog Output

The Photon has a “true” analog output that is not present on the older Core. This pin is immediately above A5 and is labelled DAC (Digital to Analog Converter). Unlike the pins that are capable of PWM, the DAC pin does not use pulses but does actually change the voltage on it output between 0 and 3.3V.

To set the voltage of the DAC pin you use analogWrite just like you would with a PWM pin, for example:

analogWrite(DAC, 127);


In this chapter, you have learned the basics of digital inputs and outputs. As yet, we have not touched on analog outputs and inputs, but you will meet these in later chapters as the book progresses.

In Chapter 5, you will learn about the Internet of Things and how to control electronics over the Internet.