The servo motor included in your Photon kit can rotate back or forth to any position between 0° and ~180° and hold its position.
The servo motor comes with 4 different plastic mounts called "horns" that can be attached to the motor axis (the white part sticking out of the top of the motor – this is what actually rotates). There is a single-arm horn, a double-arm horn, a four-point horn, and a circular horn. Each horn slips onto the motor axis (the horn and axis have matching "teeth"). Each horn has holes, which can allow you to attach something to the horn using the included screws.
This servo motor can rotate to a specific angle (up to ~180°) and hold its position. It does NOT rotate continuously, like a motor used in a fan or an engine. If your IoT device requires a motor that can rotate continuously, then you need a gear motor or a continuous rotation servo motor.
The servo motor has a built-in 3-wire connector. You'll plug 3 jumper wires into the connector, and then plug the other end of each wire into the breadboard or directly into a pin on the Photon circuit board.
The white wire of the servo motor must be connected to an I/O pin capable of pulse-width modulation (PWM), which is a process used to make a digital output signal (which has only two values: HIGH or LOW) act like an analog output signal (which has a range of values).
These Photon I/O pins are capable of PWM output: A4, A5, D0, D1, D2, D3, RX, TX, WKP.
To connect a servo motor to your Photon using the breadboard, you will need:
Servo Motor (with 3-wire connector)
3 jumper wires (use different colors to help identify them; it may help to match the motor wires)
5V REQUIRED: The servo motor requires 5V of power to operate.
If your Photon is being powered through the barrel jack, connect to the VIN pin.
If your Photon is being powered through the Micro-USB port, connect to the V-USB pin.
Here are the steps to connect the servo motor to your Photon using the breadboard:
Plug one end of a jumper wire into the motor's white wire connector. Plug the other end of this jumper wire into a PWM-capable I/O pin on the Photon circuit board.
Plug one end of a second jumper wire into the motor's red wire connector. Plug the other end of this jumper wire into either the VIN pin or V-USB pin on the Photon circuit board (or to a positive power rail on the breadboard that is connected to VIN or V-USB). If your Photon is being powered through the barrel jack, connect to the VIN pin. Otherwise, if your Photon is being powered through the Micro-USB port, connect to the V-USB pin.
Plug one end of a third jumper wire into the motor's black wire connector. Plug the other end of this jumper wire into a pin hole connected to GND: either plug it into a negative power rail on the breadboard (which is connected to GND via a different jumper wire), or plug it directly into a GND pin on the Photon circuit board.
Here's a wiring diagram showing a possible way to connect a servo motor (ignore the wiring for the push button):
Keep in mind that your connection can look different than this example diagram:
Your servo motor's white wire could connect to a different I/O pin capable of PWM output. (The example connects to the D0 pin on the Photon circuit board).
Your servo motor's red wire could connect to either the VIN pin or V-USB pin – or to a positive power rail connected to one of these pins. (The example connects directly to the V-USB pin.)
Your servo motor's black wire could connect either to a different GND pin or to a negative power rail connected to a GND pin. (There are three available GND pins.)
Alternatively, you could connect the servo motor's wires into different terminal strip rows on a breadboard, and then use 3 additional jumper wires to connect to the Photon circuit board.
Be sure to attach one of the horns to your servo motor. Otherwise, rotating the motor won't provide much use to your device.
Later, once you test out your code to rotate the servo motor, you may need to remove and re-position the horn, so it's lined up where you want it to be as it rotates to specific angles. The best way to do this is to rotate the servo motor to 0° (or another specific angle, such as 90°) and then remove & re-position the horn to be pointed correctly for this particular angle.
The basic steps to control a servo motor in your app code are:
Declare a global variable to store the I/O pin number for the servo motor.
Create a Servo
object assigned to a global variable called servo
.
Use a sequence of servo.attach()
, servo.write()
, and servo.detach()
statements to rotate the motor to a specific angle.
The servo motor requires a code library that defines a class called Servo
which has built-in methods (functions) for controlling the motor. However, this library is already included in the Particle firmware on your Photon device, so you do not need an #include
statement to add the library to your app.
You should declare a global variable to store the I/O pin number that the servo motor is connected to. This will make it easier to understand your code (and easier to modify the code if you were to connect the servo motor to a different pin number).
You will also need to create an object using the Servo
class included in the Particle firmware, and assign this object to a global variable.
Add this code (modify if necessary) before the setup()
function:
The first line of code does 3 things (in order):
It declares a data type for the variable's value. In this case, int
stands for integer (whole number). Photon pin numbers are always treated as int
values (even though they have letters).
It declares the variable's name. In this example, the variable will be called motor
. You can change the variable name, but choose a name that will make sense to anyone reading the code.
It assigns a value to the variable. In this example, the variable's value will be equal to D0
. If necessary, modify this value to match the actual I/O pin that your servo motor is connected to.
The second line of code creates a new object using the Servo
class, and assigns the object to a global variable named servo
.
NOTE: The Servo
class name starts with an uppercase letter "S", while the servo
object variable name starts with a lowercase letter "s".
NO PIN MODE: Because you are controlling the servo motor using an object, you do not have to set a pin mode for the servo motor within the setup()
function.
The servo motor can rotate back or forth to any position between 0° and ~180° and hold its position.
Rotating the servo motor to a specific angle requires a sequence of four steps:
Turn on the motor using the servo.attach()
method.
Rotate the motor to a specific angle using the servo.write()
method.
Allow time for the motor to finish rotating by using the delay()
method.
Turn off the motor using the servo.detach()
method.
You'll add a custom function named rotateServo()
that will contain all these steps. The function will accept a parameter representing the desired angle of rotation.
Add this rotateServo()
custom function after the loop()
function:
The rotateServo()
function requires a parameter for the specific angle of rotation. The parameter value must be an integer (whole number) and will be stored in a local variable named angle
.
Here's what the code inside the rotateServo()
function does:
The servo.attach()
method turns on the motor. This method requires the servo motor I/O pin number. In this case, the global variable named motor
stores this value. If you used a different name for the global variable storing your servo motor pin number, then insert that name instead.
The servo.write()
method rotates the motor. This method requires an integer value from 0-180 representing the angle for the rotation. In this case, the parameter variable named angle
stores the value that will be used.
A delay()
of 500
ms (0.5 seconds) is included to give the motor enough time to physically rotate before turning the motor off again.
The servo.detach()
method turns off the motor. Otherwise, if the motor isn't turned off, the motor will be "jittery" as it tries to hold its position. After you turn off the motor, you may notice that it "debounces" (rotates backward slightly). This is normal.
The rotateServo()
function can be called within the setup()
function, loop()
function, or another custom function.
When calling the rotateServo()
function, you must include the desired angle of rotation (0-180) within the parentheses after the function name.
For example, to make the servo motor rotate to an angle of 90°, insert this code statement to call the rotateServo()
function:
APPROX. 180°: In reality, this servo motor can only physically rotate to about 160°, even if it is told to rotate to 180°. Just keep this in mind as you design and build your device.
CHECK HORN POSITION: Once you've got your servo motor working, you may need to remove and re-position its horn, so the horn is lined up where you want it to be as it rotates to specific angles. The best way to do this is to rotate the servo motor to 0° (or another specific angle, such as 90°) and then remove and re-position the horn to be pointed correctly for this particular angle.
ROTATE SERVO IN SETUP: In order to ensure that your device functions correctly, you should include a code statement to rotate your servo motor to a specific starting angle (such as 0°, 90°, 180°, etc.) in the setup()
function. Otherwise, the servo motor will start at whatever angle it was at when the device was last powered off.
If necessary, the servo.read()
method can be used to get the current angle of the servo motor. The method will return an integer value (whole number) between 0-180 representing the last angle used in the servo.write()
method.
Add this code statement wherever you need to read the current angle of the servo motor:
This code statement creates a local variable named angle
to store the integer value returned by servo.read()
.
LEDs (light-emitting diodes) are small, bright, long-lasting, energy-efficient lights. LEDs have been commonly used in electronic products for decades. More recently, incandescent light bulbs used in homes and other buildings are being replaced with LED light bulbs due to their energy efficiency.
Your Photon kit has a set of LED lights in different bulb colors: red, yellow, green, and blue.
Each LED in your Photon kit has a positive leg and a negative leg. These can be identified visually by length: the negative leg has been made slightly shorter.
You will use a breadboard to help connect the LED to your Photon circuit board. The negative leg will be connected to GND (-) through a resistor. The positive leg will be connected to an I/O pin, which will serve as the voltage source (+).
To make it easier to insert the LED into a breadboard, you can carefully bend the positive leg as shown below, so both legs have the same height. You can still identify them visually: the straight leg is the negative leg, and the bent leg is the positive leg.
Your Photon kit contains a set of resistors with a resistance rating of 330 Ohms. In order to insert a resistor into the pin holes of a breadboard, you will need to bend both resistor legs into ~90° angles:
To connect an LED light to your Photon using the breadboard, you will need:
LED light with bent positive leg
Resistor with bent legs
2 jumper wires (use different colors to help identify them)
RESISTOR REQUIRED: Connect the LED's negative leg to GND using a resistor, which will limit the amount of current flowing through the LED. Otherwise, you will burn out the LED.
Here are the steps to connect the LED light to your Photon using the breadboard:
Insert the positive and negative legs of the LED into different terminal strip rows on the breadboard. (Different terminal strip rows have different row numbers.)
Plug one end of a jumper wire into the same terminal strip row as the bent positive leg of the LED. Plug the other end of this jumper wire into an I/O pin on the Photon circuit board.
Insert one end of the resistor into the same terminal strip row as the negative leg of the LED. Insert the other end of the resistor into a pin hole of the negative column of the closest power rail on the breadboard.
If the negative power rail isn't already connected to a GND pin on the Photon circuit board, then plug one end of the other jumper wire into another pin hole in the negative power rail, and plug the other end of this jumper wire into a GND pin on the Photon circuit board.
Here's a wiring diagram showing a possible way to connect an LED light:
Keep in mind that your connection can look different than this example diagram:
Your LED legs could be inserted into different row numbers on the breadboard. (The example connects the positive leg to row 20 and the negative leg to row 21.)
Your LED legs could be inserted into a different column on the breadboard. (The example connects the LED legs into column I of the terminal strip rows.)
The positive LED leg could connect to a different I/O pin. (The example connects to the D0 pin.)
The negative power rail on your breadboard could connect to a different GND pin. (There are three available GND pins.)
The basic steps to control an LED light in your app code are:
Declare a global variable to store the I/O pin number for the LED.
Set the pin mode for the LED pin in the setup()
function.
Use digitalWrite()
statements to turn the LED on or off. (Another option is to use analogWrite()
statements to control the brightness of the LED.)
You should declare a global variable to store the I/O pin number that the LED is connected to. This will make it easier to understand your code (and easier to modify the code if you were to connect the LED to a different pin number).
Add this code statement (modify if necessary) before the setup()
function:
This line of code does 3 things (in order):
It declares a data type for the variable's value. In this case, int
stands for integer (whole number). Photon pin numbers are always treated as int
values (even though they have letters).
It declares the variable's name. In this example, the variable will be called LED
. You can change the variable name, but choose a name that will make sense to anyone reading the code.
It assigns a value to the variable. In this example, the variable's value will be equal to D0
. If necessary, modify this value to match the actual I/O pin that your LED is connected to.
If you have multiple LED lights connected to your Photon, then be sure to give each LED a unique variable name by adding an adjective or number to the variable names. For example:
You need to set the pin mode for the LED to be used as an output.
Add this code statement (modify if necessary) within the setup()
function:
The pinMode()
method requires two parameters inside its parentheses (in this order):
The I/O pin number, which can be the actual pin number (such as: D0
, etc.) or a variable that stores a pin number. In this example, a variable named LED
is listed. If necessary, change this to match the variable name for your LED.
The mode value, which will always be OUTPUT
for an LED light because your app will send "on" and "off" signals (or brightness signals) to the LED light.
If you have multiple LED lights connected to your Photon, then be sure to set the pin mode for each LED pin variable. For example:
The digitalWrite()
method is used to turn an LED on or off.
Add this code statement (modify if necessary) to your app within the setup()
function, loop()
function, or a custom function:
The digitalWrite()
method requires two parameters inside its parentheses (in this order):
The I/O pin number, which can be the actual pin number (such as: D0
, etc.) or a variable that stores a pin number. In this example, a variable named LED
is listed. If necessary, modify this to match the variable name for your LED.
The signal value, which can be HIGH
or LOW
. Your Photon uses this value to send an electrical signal through the pin: HIGH
is a signal of 3.3 volts which represents "on," while LOW
is a signal of 0 volts which represents "off." Modify this value to either turn the LED on or off.
DIM LED WHEN PHOTON STARTS: Depending on which I/O pin an LED is connected to, the LED light might be on (at a dim brightness) when your Photon app first starts.
If you need an LED light to be turned off when your app first starts running, then be sure to include code to turn off the LED within the setup()
function after setting its pin mode.
Alternatively, you can use the analogWrite()
method to adjust the brightness of an LED from minimum ("off") to maximum – or any value in-between.
These Photon I/O pins are capable of PWM output: A4, A5, D0, D1, D2, D3, RX, TX, WKP.
Add this code statement (modify as necessary) to your app within the setup()
function, loop()
function, or a custom function:
The analogWrite()
method requires two parameters inside its parentheses (in this order):
The I/O pin number, which can be the actual pin number (such as: D0
, etc.) or a variable that stores a pin number. In this example, a variable named LED
is listed. If necessary, modify this to match the variable name for your LED.
The signal value, which can be any integer value (whole number) between 0-255. Your Photon uses this value to send a PWM electrical signal through the pin: 0
is minimum brightness (LED will be off) while 255
is maximum brightness. In this example, a value of 128
represents 50% brightness. Modify this value based on the brightness needed.
The analogWrite()
and digitalWrite()
methods are both capable of setting an LED to either minimum or maximum brightness:
Setting an LED to minimum brightness ("off") using analogWrite(LED, 0)
produces the same result as using digitalWrite(LED, LOW)
Setting an LED to maximum brightness using analogWrite(LED, 255)
produces the same result as using digitalWrite(LED, HIGH)
However, the analogWrite()
method is the only way to set an LED to a brightness that's in-between these two extremes:
analogWrite(LED, 64)
would set the LED to 25% brightness
analogWrite(LED, 128)
would set the LED to 50% brightness
analogWrite(LED, 192)
would set the LED to 75% brightness
etc.
Here are references for how to connect and code each physical output in your SparkFun Photon kit:
The small speaker included in the Photon kit can produce tones or play simple music (note by note). The speaker can produce tones ranging in frequency from 20Hz (very low pitch) to 20KHz (very high pitch), covering the full range of sounds that humans can hear.
The speaker in your Photon kit has a positive leg and a negative leg. These can be identified by labels on the bottom of the speaker:
Also, there is a positive symbol ( ⊕ ) printed on the side of the speaker to identify which side has the positive leg. This allows you to verify the polarity while the speaker is plugged into a breadboard.
These Photon I/O pins are capable of PWM output: A4, A5, D0, D1, D2, D3, RX, TX, WKP.
To connect a speaker to your Photon using the breadboard, you will need:
Speaker
2 jumper wires (use different colors to help identify them)
Here are the steps to connect the speaker to your Photon using the breadboard:
Insert the positive and negative legs of the speaker into different terminal strip rows on the breadboard. (Different terminal strip rows have different row numbers.)
Plug one end of a jumper wire into the same terminal strip row as the positive leg of the speaker. Plug the other end of this jumper wire into a PWM-capable I/O pin on the Photon circuit board.
Plug one end of the other jumper wire into the same terminal strip row as the negative leg of the speaker. Plug the other end of this jumper wire into a pin hole connected to GND: either plug it into a negative power rail on the breadboard (which is connected to GND via a different jumper wire), or plug it directly into a GND pin on the Photon circuit board.
Here's a wiring diagram showing a possible way to connect a speaker:
Keep in mind that your connection can look different than this example diagram:
Your speaker legs could be inserted into different row numbers on the breadboard. (The example connects the negative leg to row 3 and the positive leg to row 5.)
Your speaker legs could be inserted into a different column on the breadboard. (The example connects the LED legs into column F of the terminal strip rows.)
The positive leg of your speaker could connect to a different I/O pin capable of PWM output. (The example connects to the D2 pin.)
The negative leg of your speaker could connect (through a jumper wire) either to a different GND pin or to a negative power rail connected to a GND pin. (There are three available GND pins.)
The basic steps to control a speaker in your app code are:
Declare a global variable to store the I/O pin number for the speaker.
Set the pin mode for the speaker pin in the setup()
function.
Use tone()
statements to produce sounds.
You should declare a global variable to store the I/O pin number that the speaker is connected to. This will make it easier to understand your code (and easier to modify the code if you were to connect the speaker to a different pin number).
Add this code statement (modify if necessary) before the setup()
function:
This line of code does 3 things (in order):
It declares a data type for the variable's value. In this case, int
stands for integer (whole number). Photon pin numbers are always treated as int
values (even though they have letters).
It declares the variable's name. In this example, the variable will be called speaker
. You can change the variable name, but choose a name that will make sense to anyone reading the code.
It assigns a value to the variable. In this example, the variable's value will be equal to D2
. If necessary, modify this value to match the actual I/O pin that your speaker is connected to.
You need to set the pin mode for the speaker to be used as an output.
Add this code statement (modify if necessary) within the setup()
function:
The pinMode()
method requires two parameters inside its parentheses (in this order):
The I/O pin number, which can be the actual pin number (such as: D0
, etc.) or a variable that stores a pin number. In this example, a variable named speaker
is listed. If necessary, change this to match the variable name for your speaker.
The mode value, which will always be OUTPUT
for a speaker.
The tone()
method is used to produce a sound of a specific frequency (pitch) for a specific duration of time. The speaker can produce tones ranging in frequency from 20Hz (very low pitch) to 20KHz (very high pitch), which covers the full range of sounds that humans can hear.
Add this code statement (modify as necessary) to your app within the setup()
function, loop()
function, or a custom function:
The tone()
method requires three parameters inside its parentheses (in this order):
The I/O pin number, which can be the actual pin number (such as: D2
, etc.) or a variable that stores a pin number. In this example, a variable named speaker
is listed. If necessary, modify this to match the variable name for your speaker.
The frequency for the tone, which can be an integer value (whole number) or a variable that stores an integer. The value can be between 20-20000 hertz. Lower numbers will have a lower pitch, while higher numbers will have a higher pitch. In this example, the frequency will be 2000
hertz. Modify this value to the frequency you want for your sound.
The duration for the tone, which can be an integer number (whole number) or a variable that stores an integer. The value represents the number of milliseconds that the tone will be played (1000 ms = 1 second). In this example, the duration will be 250
ms (0.25 seconds).
VOLUME: There isn't a way to change the volume of a tone produced by your Photon. However, you will notice that certain frequencies will naturally seem louder to your ears.
If you want to produce a continuous tone that keeps playing, you can either use a negative value for the duration – or you can leave out the duration parameter entirely:
To turn off a continuous tone, use the noTone()
method when you're ready to stop the sound:
An LED light is a part, meaning there is one way to correctly connect its positive and negative terminals. If a polarized part is connected incorrectly (by switching the positive and negative), the part may not work or could become damaged.
An LED can be easily burned out if it receives too much power. Therefore, a must be used to limit the amount of current flowing through the LED. The resistor is used to connect the negative leg of the LED to a GND (-) pin.
However, the LED light must be connected to an I/O pin capable of PWM output. PWM stands for , which is a way to make a digital output signal (which has only two values: HIGH or LOW) act like an analog output signal (which has a range of values).
A speaker is a part, meaning there is one way to correctly connect its positive and negative terminals. If a polarized part is connected incorrectly (by switching the positive and negative), the part may not work or could become damaged.
The positive leg of the speaker must be connected to an I/O pin capable of (PWM), which is a process used to make a digital output signal (which has only two values: HIGH or LOW) act like an analog output signal (which has a range of values).
Servo Motor
Photon Pin
White – Data
any I/O pin capable of PWM output
Red – Power (4.8-6V)
5V through VIN or V-USB
Black – Ground
GND
LED Light | Photon Pin |
Positive Leg (bent) = Power | any I/O pin |
Negative Leg = Ground | GND using resistor |
Speaker | Photon Pin |
Positive Leg | any I/O pin capable of PWM output |
Negative Leg | GND |
The Micro OLED display included in your Photon kit is a monochrome (single-color) screen that is 64 pixels in width and 48 pixels in height. It can be used to display text, simple graphics, or a combination. You can even create simple animations by drawing an object, leaving it on the screen briefly, erasing it, and repeating these steps with the object drawn at a new position on the screen.
Although the Micro OLED display is small, it can be useful for displaying information. Since the display is so small, design your information to be "glanceable" – a person should be able to very quickly and easily read and understand the information.
To connect the Micro OLED display to your Photon using the breadboard, you will need:
Micro OLED display
7 jumper wires (use different colors to help identify them)
The Micro OLED has pins located along the top edge of the display. There are labels for each pin printed on the underside of the Micro OLED circuit board.
If the pins along the top edge were numbered left to right as 1-8, the wiring connections would be:
3.3V MAXIMUM: The Micro OLED display operates at 3.3V of power. Connect it to the 3.3V pin on your Photon, or connect it to a positive power rail that's connected to the 3.3V pin.
Do NOT connect it to VIN or V-USB because the higher voltage could damage the display.
TWIN PINS: Analog pins A2, A3, A4, and A5 are each represented by two pins on the Photon board. The duplicate pins are labeled as: SS/A2, SCK/A3, MISO/A4, MOSI/A5.
However, the Micro OLED has to connect to only one A2 pin – not both. The same goes for connecting the Micro OLED to the A3 and A5 pins.
The only limitation is that once you connect the Micro OLED, you will not be able to have a different part (such as an LED, etc.) connected to the other A2, A3, or A5 pins.
Here are the steps to connect the Micro OLED to your Photon using the breadboard:
Insert the 8 pins of the Micro OLED into different terminal strip rows on the breadboard. (Different terminal strip rows have different row numbers.)
Plug one end of a jumper wire into the same terminal strip row as the first OLED pin (CS). Plug the other end of this jumper wire into its corresponding pin on the Photon circuit board.
Repeat step 2 with the other jumper wires until each OLED pin is connected to its correct Photon pin. NOTE: The fourth OLED pin (SDO) will not be connected to anything.
Here's a wiring diagram showing a possible way to connect a servo motor (ignore the wiring for the three push buttons):
Keep in mind that your connection can look different than this example diagram:
Your Micro OLED pins could be inserted into different row numbers on the breadboard. (The example connects the OLED pins to rows 1-8 on the right side of the breadboard.)
Your Micro OLED pins could be inserted into a different column of the breadboard. (The example connects the OLED pins into column F of the terminal strip rows.)
Your Micro OLED could connect (through jumper wires) to the other A2, A3, and A5 pins located on the lower left side of the Photon circuit board.
Your Micro OLED 3V3 pin could connect (through a jumper wire) either directly to the 3.3V pin or to a positive power rail connected to the 3.3V pin.
Your Micro OLED GND pin could connect (through a jumper wire) either directly to a GND pin or to a negative power rail connected to a GND pin. (There are three available GND pins.)
The basic steps to control the Micro OLED display in your app code are:
Include the SparkFun Micro OLED library in your app.
Define the I/O pin numbers for certain Micro OLED pins.
Create a MicroOLED
object assigned to a global variable called oled
.
Use the oled.begin()
method to start the display in the setup()
function.
Use various oled
methods to display text or simple graphics on the screen.
Your Photon app must include a code library that will allow you to control the Micro OLED display.
In Particle Build, click on the Libraries icon to open the Libraries menu panel.
Type oled
into the search field. Select the result called: SparkFunMicroOLED
Click the button to "Include in Project"
Select the title of your Photon app, and then click the "Confirm" button
Particle Build will automatically insert this #include
statement at the beginning of your app code:
You need to define the I/O pin numbers for certain Micro OLED pins. This is similar to declaring global variables (except defined values are not allowed to change).
You will also need to create an object using the MicroOLED
class in the included library, and assign this object to a global variable.
Add this code before the setup()
function:
The first three lines of code define the I/O pin numbers for three specific OLED pins.
The fourth line of code creates a new object using the MicroOLED
class, and assigns the object to a global variable named oled
.
The oled.begin()
method is used to start the Micro OLED display.
Add this code statement within the setup()
function to start the OLED display:
The oled.begin()
method will initialize the settings (including pin modes) for the Micro OLED and turn it on.
Displaying text on the Micro OLED screen requires a sequence of five steps:
Clear the screen using the oled.clear()
method.
Set the font type using the oled.setFontType()
method.
Set the cursor position using the oled.setCursor()
method.
Print text to the screen using the oled.print()
and/or oled.println()
methods.
Display the printed text using the oled.display()
method.
For example:
CREATE CUSTOM FUNCTION: Rather than listing all your OLED code statements within the loop()
function, it may be better to create a custom function that contains all the code to display your text or graphics. Then call the custom function within the loop()
function.
To clear the screen before adding text or graphics:
The SparkFun Micro OLED library includes 4 available font types (numbered 0-3), which differ in font size and in which characters can be displayed:
By default, the Micro OLED will be set to font type 0 by the oled.begin()
method.
You can use the oled.setFontType()
method to set the font type by including the font type number within the parentheses:
You can use a mix of different font types by setting a new font type before printing a new line of text.
For example:
The cursor position represents the starting position for printing text to the screen. The cursor can be set to any (x,y)
position on the screen. The OLED screen is 64 pixels wide by 48 pixels tall:
The x
positions are numbered left to right as 0-63
The y
positions are numbered top to bottom as 0-47
For example, to have your text start at the top-left corner of the screen:
As you print text to the screen, the OLED display will automatically move the cursor position, so the next text printed to the screen will start at the next available screen position.
However, if desired, you can adjust the text layout by setting a new cursor position before printing a line of text, so the new text will start at a specific position on the screen.
For example:
The oled.print()
and oled.println()
methods can be used to print text (or variable values) to the screen. Text that is longer than the screen column width will automatically wrap to the next line.
To print text directly, include the text inside the method's parentheses by listing the text within double quotation marks:
To print the value stored in a variable, include the variable name inside the method's parentheses.:
In this case, myName
is a variable name (presumably a person's name). Change this to the name of your variable, which could store a text string or number.
If a variable stores a decimal number (data type is float
or double
), you have the option of indicating how many digits after the decimal point to show when printing the variable's value:
In this case, roomTemp
is a variable name that stores a decimal value (presumably a temperature sensor measurement). Its value will be displayed to 1
digit after the decimal point. So if the actual variable value were 70.1584275
, it would be printed on the screen as: 70.2
(the last digit is automatically rounded up if necessary).
There are two different print methods that can display text on the Micro OLED screen:
oled.print()
will print text to the screen, but the cursor will remain on the same line. The next text printed to the screen will start where the last text ended.
oled.println()
will print text to the screen, and then move the cursor to the start of a new line. The next text printed to the screen will start on the new line.
The oled.print()
command is useful for combining text and variable values on the same line.
For example, if you had a variable named hour
that stored the hour of the current time and another variable named minutes
that stored the minutes, you could combine these together on the screen:
Notice that the last code statement used the oled.println()
method to move the cursor to a new line for any text that might be printed after the time.
To add a blank line, just use the oled.println()
method without anything inside its parentheses:
Any text or graphics will NOT be shown until the OLED is instructed to display the screen:
GLANCEABLE: Because the Micro OLED display is small, be sure your text and graphics will be "glanceable" – a person should be able to quickly and easily understand the information.
Displaying simple graphics on the Micro OLED screen requires a sequence of three steps:
Clear the screen using the oled.clear()
method.
Draw graphics (dots, lines, rectangles, circles) on the screen using various methods.
Display the graphics using the oled.display()
method.
For example:
Rather than listing all these code statements within the loop()
function, you may want to create a custom function that contains all the code to display your graphics. Then you can call this custom function within the loop()
function.
The following drawing methods are available:
oled.pixel()
can be used to draw a dot
oled.line()
, oled.lineH()
, and oled.lineV()
can be used to draw a line
oled.rect()
and oled.rectFill()
can be used to draw a rectangle
oled.circle()
and oled.circleFill()
can be used to draw a circle
Each drawing method requires parameters for the (x, y) pixel position of a point (or two points if drawing a line). The OLED screen is 64 pixels wide by 48 pixels tall:
The x
pixel positions are numbered left to right as 0-63
The y
pixel positions are numbered top to bottom as 0-47
The oled.pixel()
method can be used to draw a dot (pixel) at a specific point:
For example, to draw a dot at the center of the screen:
The oled.line()
method can be used to draw a line from one point (x1, y1) to another point (x2, y2):
For example, to draw a line from the top-left corner (0,0) to the bottom-right corner (63, 47):
The oled.lineH()
method can be used to draw a horizontal line that starts from a specific point (representing the left end of the line) and has a specified width:
For example, to draw a horizontal line that starts at (12, 24) and has a width of 40 pixels:
The oled.lineV()
method can be used to draw a vertical line that starts from a specific point (representing the top end of the line) and has a specified height:
For example, to draw a vertical line that starts at (32, 9) and has a height of 30 pixels:
The oled.rect()
method can be used to draw an outline of a rectangle that starts from a specific point (representing the top-left corner of the rectangle) and has a specified width and height:
For example, to draw an outline of a rectangle with its top-left corner at (12, 14) with a width of 40 pixels and a height of 20 pixels:
The oled.rectFill()
method can be used to draw a solid (filled) rectangle that starts from a specific point (representing the top-left corner of the rectangle) and has a specified width and height:
For example, to draw a filled rectangle with its top-left corner at (12, 14) with a width of 40 pixels and a height of 20 pixels:
The oled.circle()
method can be used to draw an outline of a circle that has its center at specific point and has a specified radius:
For example, to draw an outline of a circle with its center at (32, 24) and a radius of 20 pixels:
The oled.circleFill()
method can be used to draw a solid (filled) circle that has its center at specific point and has a specified radius:
For example, to draw a filled circle with its center at (32, 24) and a radius of 20 pixels:
By default, all the drawing methods will draw using "white" pixels (which are actually light blue on your Micro OLED screen). However, you can modify each method to draw using black pixels instead, which represents erasing.
Any of the drawing methods can be modified to draw using black pixels by adding BLACK, NORM
as the last two parameters inside the method's parentheses.
For example, to draw a black dot:
For example, to draw a black line:
For example, to draw solid (filled) black circle:
The black pixels will only be visible if drawn on top of a white shape, so the black pixels are erasing white pixels.
For example, this code draws a white filled circle with its center at (32, 24) and a radius of 20 pixels, and then draws a black filled circle that also has its center at (32, 24) but with a radius of 10 pixels:
By drawing the black circle after the white circle, the black circle will erase part of the white circle.
You can also create more complex patterns by drawing a series of white and black shapes, such as:
This example code draws 4 filled circles that have the same center point but a different radius:
First, a white filled circle with a radius of 20 pixels is drawn.
Then, a black filled circle with a radius of 15 pixels erases part of the white circle.
Then, a new white filled circle with a radius of 10 pixels is drawn that fills part of the black circle.
Finally, a new black filled circle with a radius of 5 pixels erases part of the second white circle.
The overall pattern ends up looking like a "target" symbol:
You can also create simple animations by drawing something (with white pixels), leaving it on the screen briefly, erasing it (with black pixels), and then changing its position on the screen. A for
loop can be used to repeat the "animation" for a certain number of times.
For example, here is code for a simple animation that makes a circle "bounce" back and forth on the screen. The code inside each for
loop draws and displays a white filled circle, leaves the circle on the screen for a brief delay()
, and then erases the circle (by drawing and displaying a black filled circle of the same size at the same position).
The first for
loop increases the x-position of the circle's center from 10 to 53, which slowly moves the circle across the screen from left to right. The second for
loop decreases the x-position of the circle's center from 53 to 10, which moves the circle from the right back to the left.
You can also combine text and graphics on the OLED screen at the same time. For example:
The Micro OLED display is only 64 pixels in width by 48 pixels in height. Use this limited screen space effectively by designing your text and graphics to be "glanceable" – a person should be able to very quickly and easily read and understand the information.
OLED Pin
Photon Pin
Pin 1 – CS
A2
Pin 2 – RST
D6
Pin 3 – D/C
D5
Pin 4 – SDO
(None)
Pin 5 – SCK
A3
Pin 6 – SDI
A5
Pin 7 – 3V3
3.3V
Pin 8 – GND
GND
Font Type
Font Size
Characters Allowed
Used For
0
5×7 pixels (10 columns by 6 rows)
any ASCII character
General
1
8×16 pixels (6 columns by 3 rows)
any character on keyboard
General
2
10×16 pixels (5 columns by 3 rows)
only numbers and period
Numbers
3
12×48 pixels (5 columns by 1 row)
only numbers and colon
Numbers or Time