Maker.io main logo

Rotating Holiday Ornament with RGB LEDS and a Servo Motor

13

2016-12-01 | By SparkFun Electronics

License: Attribution-NonCommercial-ShareAlike

Courtesy of Sparkfun Electronics

This project is based off the Sparkfun Tinker kit.

Products needed:

  • Sparkfun Tinker kit with Arduino
  • Christmas Ornament of your choosing (preferably plastic - glass and electronic projects usually don’t end well for me)

Hardware Overview

Introduction to the SparkFun Tinker Kit

This SparkFun Tinker Kit Experiment Guide is your map for navigating the waters of beginning embedded electronics, robotics and citizen science using the SparkFun RedBoard while sticking to a strict budget. This guide contains all the information you will need to explore the 11 circuits of the SparkFun Tinker Kit. At the center of this guide is one core philosophy – that anyone can (and should) play around with cutting-edge electronics in a fun and playful way while not breaking the bank. When you’re done with this guide, you’ll have the know-how to start creating your own projects and experiments. From building robots and game controllers to data logging, the world will be your oyster. Now enough talking – let’s start tinkering!

Here are all the parts in the SparkFun Tinker Kit:

Make an LED flasher with a Raspberry Pi

  • SparkFun RedBoard – Our tried and true version of the Arduino UNO.
  • Breadboard – Excellent for making circuits and connections off the Arduino.
  • SparkFun Mini Screwdriver – To help you screw your RedBoard onto the holder.
  • Hobby Gearmotor Set – A set of hobby level motors with gearboxes set to 120 RPM.
  • Small Servo – Here is a simple, low-cost, high-quality servo for all your mechatronic needs.
  • TMP36 Temp Sensor – A sensor for detecting temperature changes.
  • USB A to B Cable – This 6-foot cable provides you with a USB-A connector at the host end and standard B connector at the device end.
  • Male-to-Male Jumper Wires – These are high-quality wires that allow you to connect the female headers on the Arduino to the components and breadboard.
  • Photocell – A sensor to detect ambient light. Perfect for detecting when a drawer is opened or when nighttime approaches.
  • Tri-Color LED – Because everyone loves a blinky.
  • Red, Blue, Yellow and Green LED – Light-Emitting Diodes make great general indicators.
  • Red, Blue, Yellow and Green Tactile Buttons – Go crazy with different colored buttons.
  • 10K Trimpot – Also known as a variable resistor, this is a device commonly used to control volume and contrast, and makes a great general user control input.
  • 330 Ohm Resistors – Great current-limiting resistors for LEDs, and strong pull-up resistors.
  • 10K Ohm Resistors – These make excellent pull-ups, pull-downs and current limiters.
  • SN754410 H-Bridge IC – This nifty little Integrated Circuit (IC) is perfect for controlling the speed and direction of up to two separate motors.
  • 4xAA Battery Holder – Used to power the RedBoard without being connected to your computer. Sorry! Batteries not included
  • A Red Sparkfun Cardboard Box – this comes in handy as a useful enclosure

Sparkfun RedBoard

The SparkFun RedBoard combines the simplicity of the UNO’s Optiboot bootloader (which is used in the Pro series), the stability of the FTDI (which we all missed after the Duemilanove was discontinued) and the R3 shield compatibility of the latest Arduino UNO R3. The RedBoard can be programmed over a USB Mini-B cable using the Arduino IDE: Just plug in the board, select “Arduino UNO” from the board menu, and you’re ready to upload code. RedBoard has all of the hardware peripherals you know and love: 14 Digital I/O pins with 6 PWM pins, 6 Analog Inputs, UART, SPI and external interrupts. We’ve also broken out the SDA, SCL and IOREF pins that showed up on the UNO R3, so the RedBoard will be compatible with future shields. This version adds an SMD ISP header for use with shields. You can power the RedBoard over USB or through the barrel jack. The onboard power regulator can handle anything from 7 to 15VDC.

Course Overview

This course is about using the Sparkfun Tinker kit and a few of the great components inside the kit. We will use these components to build a holiday ornament that spins with a servo motor and changes color with an RGB LED at the same time. The main goal with this project is to have students use the always popular Arduino IDE and run and compile the code. The project will utilize multiple function of the MCU and allow expansion with other example code created by Sparkfun for the Tinker kit.

Class Activity 1 – Set up the RGB LED for Vibrant PWM (pulse width modulation) colors.

After looking through the many different parts offered in the kit, pull out the RGB LED. There is only one of them cleverly hidden in one of the component bags. Along with the LED select a red, green, blue, and black male to male jumper wire.

Make an LED flasher with a Raspberry Pi

These jumper wires will need to be soldered to the RGB LED. To do this properly and easily, you will first want to put a little solder on all 4 jumpers and on all 4 legs of the LED first (called tinning). Then you can hold the jumper to the correct leg of the LED and touch them with the soldering iron. Make sure the correct color corresponds with the correct leg on the LED. The longest leg will be the cathode and the others will be as shown in the picture.

Make an LED flasher with a Raspberry Pi

Class Activity 2 – Mount the Components onto the Red Sparkfun cardboard box

Sparkfun has been known to use their box in clever ways. So we decided to add to the fun and use their nice red box as an enclosure for the kit. You can lay this out however it fits your creative mind.

The breadboard has built-in double stick tape which can be used to stick it to the box. This is how I have my box set up. You can use a scissor or a knife to cut a rectangle for the servo to sit in. Try to get this to fit very snug, this will be the base for the ornament. Also cut slots for the wires, you can see the locations where to cut in the picture. Wiring it up is the next step, but I will be helpful to have the holes ready.

Make an LED flasher with a Raspberry Pi

Make an LED flasher with a Raspberry Pi

Class Activity 3 – Wire up the Circuit

The Image above shows where the wires go from the pins on the MCU to the breadboard then out to the servo motor and RGB LED. The RGB wires will be using pins 6, 5, and 3 from the MCU. The Servo (white wire) will be using pin #9 on the MCU. The black and red wires that run from the MCU to the breadboard bring 5vdc and ground to the power and ground rails on the breadboard. The servo motor has 3 wires: red, black, and white. The red and black are simply 5vdc and ground so they can be connected directly to the power and ground rail on the breadboard. The white wire is the signal wire from the MCU (pin#9). The RGB will need a separate resistor for all 3 colors. This will be wired up directly in line with each color as seen above. We will be using 330 ohm (orange, orange, brown) resistors. The black or ground wire from the RGB will need to simply be connected to the ground rail on the breadboard (upper right corner of picture).

Class Activity 4 – Place the RGB into the Ornament and connect it to the Servo Motor (if you don’t have an ornament, you can skip this step and place the RGB directly into the breadboard)

With everything neatly wired up, you will now place the RGB into the ornament and attach a servo horn onto the “cap” of the ornament.

When inserting the LED, make sure the legs are separated enough so they will not touch each other. Remove the wire holder and screw the white servo horn centered onto the cover of the ornament with the small black screws provided.

Make an LED flasher with a Raspberry Pi

 

You will need to place the RGB into the ornament and place the cap over the wires. This will need to be either taped or zip-tied to stay in place.

This can now be placed on the servo and you are ready to start with the programming.

Class Activity 5 – Download and Setup the Arduino Software

Before you plug the RedBoard into your computer, you’ll need to install Arduino.

Installing Arduino

To begin, head over to Arduino’s download page and grab the most recent, stable release of Arduino. Make sure you grab the version that matches your operating system.

DOWNLOAD ARDUINO!

The installation procedure is fairly straight-forward, but it varies by OS. Here are some tips to help you along. We’ve also written a separate Installing Arduino tutorial if you get really stuck.

Windows Install Tips

The Windows version of Arduino is offered in two options: an installer or a zip file. The installer is the easier of the two options; just download that, and run the executable file to begin installation. If you’re prompted to install a driver during installation, select “Don’t Install” (the RedBoard doesn’t use the same drivers). Don’t forget which directory it installs to (defaults to “Program Files/Arduino”).

Make an LED flasher with a Raspberry Pi

If, instead, you choose to download the zip file version of Arduino, you’ll need to extract the files yourself. Don’t forget which folder you extract the files into! We’ll need to reference that directory when we install drivers.

Mac Install Tips

The Mac download of Arduino is only offered in a zip file version. After the download is finished, simply double-click the .zip file to unzip it.

Following that, you’ll need to copy the Arduino application into your applications folder to complete installation

Install FTDI Drivers

Once you have downloaded and installed Arduino, it’s time to connect the RedBoard to your computer! Before you can use the board, though, you’ll need to install drivers.

Windows Driver Installation

After initially plugging your RedBoard in, your computer will try to search for a compatible driver. It may actually succeed! The FTDI drivers are pretty common, so Windows Update may know a little something about them. If the drivers do automatically install, you should see a little bubble notification saying so:

Make an LED flasher with a Raspberry Pi

If your computer failed to find drivers, we’ll have to install them manually. Check out our Windows FTDI Driver install guide for driver installation instructions.

Mac Driver Installation

If you’re lucky, the FTDI drivers should automatically install on Mac OS X; otherwise you’ll have to manually install the drivers. Check out the Mac FTDI Driver install guide for help installing the drivers.

In short, the process involves heading over to the FTDI driver website, and downloading the most up-to-date VCP drivers. Then you’ll simply run the “FTDIUSBSerialDriver_v2_2_18.dmg” file you downloaded, and follow the installation prompts.

Class Activity 6 – The Code!!

Now that you have the software set up and ready to go, Below is the code for the Holiday Ornament demo. Open a new Arduino sketch. Copy the code from below (or save it from the file sent by your instructor)

Copy Code
#include “Pins.h”
#include <Servo.h>
#include “LDR.h”
#include “RGBLed.h”

extern Servo servo;
extern LDR ldr;
extern RGBLed rgbLed;


const int servoRestPosition = 0; //Starting position
const int servoTargetPosition = 180; //Position when event is detected
#define GREEN 3
#define BLUE 5
#define RED 6
#define delayTime 20

/* This code sets up the essentials for your circuit to work. It runs first every time your circuit is powered with electricity. */
void setup() {
{
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(RED, HIGH);


}
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
Serial.println("start");

servo.attach(9);
servo.write(servoRestPosition);
delay(100);
servo.detach();

rgbLed.turnOff(); // Start with RGB LED turned off
}
int redVal;
int blueVal;
int greenVal;
/* This code is the main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop. */
void loop() {

// The servo will rotate to target position and back to resting position with an interval of 500 milliseconds (0.5 seconds)
servo.attach(9); // 1. attach the servo to correct pin to control it.
servo.write(servoTargetPosition); // 2. turns servo to target position. Modify target position by modifying the 'ServoTargetPosition' definition above.
delay(800); // 3. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
servo.write(servoRestPosition); // 4. turns servo back to rest position. Modify initial position by modifying the 'ServoRestPosition' definition above.
delay(800); // 5. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
servo.detach(); // 6. release the servo to conserve power. When detached the servo will NOT hold it's position under stress.


{
int redVal = 255;
int blueVal = 0;
int greenVal = 0;
for( int i = 0 ; i < 255 ; i = 1 ){
greenVal = 1;
redVal -= 1;
analogWrite( GREEN, 255 - greenVal );
analogWrite( RED, 255 - redVal );

delay( delayTime );
}

redVal = 0;
blueVal = 0;
greenVal = 255;
for( int i = 0 ; i < 255 ; i = 1 ){
blueVal = 1;
greenVal -= 1;
analogWrite( BLUE, 255 - blueVal );
analogWrite( GREEN, 255 - greenVal );

delay( delayTime );
}

redVal = 0;
blueVal = 255;
greenVal = 0;
for( int i = 0 ; i < 255 ; i = 1 ){
redVal = 1;
blueVal -= 1;
analogWrite( RED, 255 - redVal );
analogWrite( BLUE, 255 - blueVal );

delay( delayTime );
}
}}

// The RGB LED will turn PURPLE for 500ms(half a second) and turn off
/* rgbLed.setRGB(160, 3, 255); // 1. sets RGB LED color to purple. Change the values in the brackets to (255,0,0) for pure RED, (0,255,0) for pure GREEN and (0,0,255) for pure BLUE.
delay(500); // 2. change the value in the brackets (500) for a longer or shorter delay in milliseconds.
rgbLed.setRGB(255, 55, 0); // 3. turns RGB LED off (showing no color). Change the values in the brackets to alter the color.
delay(200); // 4. change the value in the brackets (500) for a longer or shorter delay in milliseconds. */

//END

Next press the upload button and watch your servo spin the ornament and change the RGB led!

Make an LED flasher with a Raspberry Pi

Now that you have the class project up and working, I would suggest going over to https://learn.sparkfun.com/tutorials/experiment-guide-for-the-sparkfun-tinker-kit/all#. This guide has all of the examples for the individual components in the Sparkfun Inventor’s kit.

If you have any questions, please don’t hesitate to reach out to kevin.walseth@digikey.com 1-800-338-4105 x2332.

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.