.
Arduino Disco Ball Cake Reviewed by Unknown on 13:38 Rating: 4.5

Arduino Disco Ball Cake


 

Description

This is a fun project that will surely impress anyone you make this for. If you are having a "Disco" themed party, you cannot have a boring old cake. Let me tell you, this is probably the only Arduino project that my wife has ever been willing to be a part of. She did the hard work of putting the cake together, and I, well.... I was in charge of lighting. My biggest fear was that one of the wires would come loose and ruin the event at the most critical moment... While a wire did come loose, I managed to fix it in time before the guests arrived. Ok enough of my monologue, let me show you how to make one of these things.

 


Parts Required:


Arduino Libraries and IDE

You can get the Arduino IDE from here: https://www.arduino.cc/en/Main/Software
I used version 1.6.4, which is probably way out of date... but works fine nonetheless.
 
You can get information about how to use the FastLED library here: http://fastled.io/
And you can download it from here: FastLED Library
I used version 3.0.3, which is also probably out of date.

ARDUINO CODE:

ARDUINO CODE DESCRIPTION:

  • FastLED Library: You need to make sure that you have downloaded and installed the FastLED library into your Arduino IDE. The library is included in this sketch otherwise the FastLED functions will not work.
  • The "NUM_LEDS" variable: tells the Arduino how many LEDS are in use. In this case, we have 4 LED rings, with each LED ring containing 16 LEDs, and therefore a total of 64 LEDs. If you define a lower number, for example 16, then the sketch would only illuminate the LEDs on the first LED ring.
  • The "DATA_PIN" variable: tells the Arduino which Digital Pin to use for data transmission to the LED ring. In this case, I am using Digital Pin 9.
  • Other variables: I have a couple of other variables which are used for LED randomisation and hue control. Hue is the colour of the LED. By incrementing the hue variable, you can get the LEDs to cycle in a rainbow-like pattern. The "hue" variable is a "byte", which means that it will only go up to a maximum value of 255, before it jumps back down to zero.
  • Initialisation Code: If you have a different LED ring to the one in this tutorial, you may have to modify the initialisation code. This LED ring has a WS2812-B chipset (according to the ICStation website), and so this line:
     
    FastLED.addLeds(leds, NUM_LEDS); Will tell the FastLED library which chipset is being used (NEOPIXEL), the pin used for data transmission (DATA_PIN), the LED array to be controlled (leds), and the number of LEDs to be controlled (NUM_LEDS).
  • In the "loop()": section of the code: the "hue" variable is incremented to create a rainbow effect, and a random LED is selected using the FastLED's random8() function.
  • The random8(x) function: will randomly choose a number from 0 to x.
  • The randomSeed() function: is there to help "truely randomise" the number. This is helped by reading the randomness of a floating analogPin (A0). It doesn't have to be analogPin 0, it can be any unused analog pin.
  • leds[rnd].setHSV(hue,255,255): This line sets the random LED to have a hue equal to the "hue" variable, saturation equal to 255, and brightness equal to 255. Saturation equal to zero will make the LED shine white.
    Brightness of zero essentially turns the LED OFF.
  • FastLED.show(): No physical changes will be made to the LED ring display until a message is sent from the Arduino to the Digital input pin of the LED ring. This message is transmitted when you call the FastLED.show(); function. This tells the LED rings to update their display with the information contained within the led array (leds). So if you set all LEDs to turn on, the board will not illuminate the LEDs until the FastLED.show(); function is called. This is important to know - especially when trying to design your own LED sequences.
  • The delay(50) line: will set the amount of time between flashes to 50 milliseconds. You can change the delay to increase or decrease the number of flashes per second.
  • The leds[i].fadeToBlackBy( 180 ) function: essentially fades the LEDS by 180 units. You can increase or decrease this number to achieve the desired fade speed. Be warned however, that if you forget to call this function or if you fail to fade the LEDs sufficiently, then you may end up with ALL LEDs turning on, which could potentially destroy your Arduino board - i.e. depending on the number of LED rings you have, and how you have chosen to power them.

 

The Cake


  • Slide 1 - Base Plate: It is important to create the base plate with all of the electronics fitted and in working order BEFORE you put the Cake onto it. Trying to fit wires/cables LEDs and circuits under the base plate while there is a Cake ontop is a recipe for disaster. So prepare the base plate first, and then move to the cake making part later.
  • Slide 2 - Bake Cake: You will need a couple of hemisphere cake pans to make the two sides of the ball. You have to make a relatively dense cake to withstand the overall weight of the cake, icing and fondant, and to maintain it's shape. Once cooled and chilled, you can place them ontop of each other to form a sphere. They are held together by a layer of icing between them.
  • Slide 3 - Fondant Icing: The fondant icing has to be rolled out on a special non-stick mat. We found that adding a bit of flour helped to reduce the stickiness. There are special rollers which ensure that the thickness of the fondant is consistent throughout. You then have to cut them into square pieces (about 1 cm squares worked well for us). The squares are then painted Silver with a special/edible silver fondant glaze. You may need to use a few coats, and allowing it to dry between coats.
  • Slide 4 - Iced Cake on Base: The cake can either be iced on or off the base plate... probably better to do it off the base plate. But if you decide to do it on the base plate, you will need to protect the LEDs from stray icing that may fall from the cake (in the process). Once the cake has been fully iced (with icing/frosting), you will need to place the cake into the central position on the board. There may be a chance that the cake may slide from the base... so do what you need to do to make it stay put.
  • Slides 5-7 - Place Fondant Squares: While the icing is still soft, you will then need to quickly, methodically and tirelessly place the fondant squares in a horizontal linear pattern around the cake. Work your way towards the north and south poles of the cake doing one row at a time. You can cut a fondant circle for the north pole of the cake. In slide 7, you will see a hole at the top of the cake. This was made to cold a plastic canister inside, which would be used later the hold the decorations in place at the top of the cake. Do this before placing the fondant circle at the top of the cake.
  • Slide 8 - Add Glitter: After placing all of the fondant squares onto the cake, it is very possible that some of the Silver glaze may have been wiped off some of the squares. This is where you go over it again with a few more coats of silver glaze, and on the last coat, before it dries, you can sprinkle some edible glitter all around the cake to give it that extra shine.
  • Slide 9 - The end product: The final step is to add some wire sparklers and some other decorations to the top of the cake. Push the wires through the fondant cap at the north pole into the canister within. This will hold the wires in place without ruining all of your hard work.


LED Ring pins

  • WS2812-B chipset: This LED ring uses the WS2812-B chipset, and has 4 break-out pins
    (GND, 5V, Din, Dout)
  • Power: To power this module, you need to provide 5V and up to 1A of current
  • Signals: To control the LED ring, you need to send signals to it via the Digital Input pin (Din).
    You can connect another LED ring to this one by utilising the Digital Output pin (Dout)

 

Power Usage Guide

  • General Rule: Each individual LED on the ring can transmit Red, Green and Blue light.The combinations of these colours can make up any other colour. White light is made up of all three of these colours at the same time. Each individual colour will draw approximately 20mA of current when showing that colour at maximum brightness. When shining white at maximum brightness, the single LED will draw approximately 60mA.
  • Power multiplier: If each LED can draw up to 60mA and there are 16 LEDs on a single LED ring, then 16x60mA = 960mA per LED ring. To be safe, and to make the maths easier, you need to make sure that you provide enough current to accommodate 1A per LED ring. So 4 LED rings will need a 5V 4A power supply if you want to get full functionality out of the modules.


 

Fritzing diagram

Connecting ONE LED Ring to the Arduino- (Click to enlarge)


  • 3 wires: You only need 3 wires to connect to the LED ring. If you only plan to light up a couple of LEDs at any one time this should be ok.
  • The SAFE WAY: A safer way to do this is to use an external power supply to power both the Arduino and the LED ring.
  • Electrolytic capacitor: By connecting a large 4700 uF 16V Electrolytic capacitor between the positive and negative terminals of power supply leads, with the negative leg of the capacitor attached to the negative terminal of the power supply, you will protect your LED rings from any initial onrush of current.


  • Protecting Resistor: It is also advisable to place a 300-400 ohm resistor between the Arduino's Digital Pin 9 (D9) and the LED Ring's Digital Input pin (Din). This protects the first LED from potential voltage spikes
  • Suitable wires: If you plan to chain a few of these LED rings together (see below), then you will probably want to keep the wires as short as possible and use a decent guage wire that can handle the current being drawn through them.


 

Connecting TWO LED Rings to the Arduino- (Click to enlarge)


  • Three extra wires:You only need 3 extra wires to connect an additional LED ring. A wire needs to connect the Digital output (Dout) of the first LED ring to the Digital Input (Din) of the 2nd LED ring.
  • Stay safe: Once again, a safer way to do this is to use an external power supply, a large electrolytic capacitor at the terminals, and a 300-400 ohm resistor between the Arduino and the first LED ring's digital input pin.


 

Connecting FOUR LED Ring to the Arduino- (Click to enlarge)


  • Sixty Four LEDs:You need 3 extra wires for each additional LED ring. 4 LED rings provides a total of 64 LEDs.
  • Watch the AMPS:At full brightness, this setup could potentially draw up to 4amps (or roughly 1 amp per LED ring)
  • External Supply essential: It is essential to use an external power supply to power these LEDs when there are so many of them. If you don't use an external power supply and you accidentally illuminate ALL of the LEDs, then you are likely to damage the microcontroller from excessive current draw.


Connection Tables

How to connect ONE LED Ring to the Arduino- (Click to enlarge)


How to connect TWO LED Rings to the Arduino- (Click to enlarge)


 

Concluding comments

In this tutorial I showed you how to go about decorating a Disco Ball cake and also showed you how to use the RGB LED rings from ICStation. If you look at the video you will see just how versatile these LED rings are. I would like to thank my wife for providing such an exciting project to work on, and ICStation for their collaborative efforts. Please make sure to share this project with all of your friends and family.
 






If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.


Description: Arduino Disco Ball Cake Rating: 3.5 Reviewer: Unknown ItemReviewed: Arduino Disco Ball Cake
Arduino LED Light Box Reviewed by Unknown on 07:43 Rating: 4.5

Arduino LED Light Box

Description

Long straight lines of LED luminescence is nice, but sometimes you may want to light up something that has an unusual shape, or is not so linear. This is where the 12mm diffused flat digital RGB LED Pixels can come into play. This cool strand of 25 RGB LED pixels fit nicely into 12mm pre-drilled holes of any material you like.

This tutorial is dedicated to making a LED Light Box. I wanted the box to be equally as interesting during the day as it was at night. If you decide you make your own, feel free to be as creative as you want !! However, if you lack artistic acumen, you may need to source a minion or two.


 

Arduino Libraries and IDE

Before you start to hook up any components, upload the following sketch to the Arduino microcontroller. I am assuming that you already have the Arduino IDE installed on your computer. If not, the IDE can be downloaded from here.

The FastLED library is useful for simplifying the code for programming the RGB LED pixels. The latest "FastLED library" can be downloaded from here. I used FastLED library version 3.0.3 in this project.

If you have a different LED strip or your RGB LED pixels have a different chipset, make sure to change the relevant lines of code to accomodate your hardware. I would suggest you try out a few of the FastLED library examples before using the code below, so that you become more familiar with the library, and will be better equipped to make the necessary changes.

If you have a single strand of 25 RGB LED pixels with the WS8201 chipset, then you will not have to make any modification below.


 

ARDUINO CODE:

Arduino Code Description

The code above will generate a randomised raindrop pattern on the Arduino LED Light box, however I have written code for a few more LED animations. These animations were written specifically for this light-box setup. In other words, once you have hooked everything up, you will be able to upload these other LED animations to the Arduino board without any further modification to the hardware/wiring, and yet experience a totally different light effect. You can find the code for the other animation effects by clicking on the links below:

  1. Breathing effect
  2. Ripple effect
  3. Clock effect
  4. Rotation effect
  5. Sweep effect
  6. Spiral effect
  7. Lightning effect
  8. Paparazzi in the Rain effect

Hooking it up:

Power requirements

Each LED pixel can draw up to 60 milliamps at maximum brightness (white). ie. 20 mA for each colour (red, green and blue). Therefore you should not try to power the LED strand directly from the Arduino, because the strand will draw too much current and damage the microcontroller(and possibly your USB port too). The LED strand will therefore need to be powered by a separate power supply. The power supply must supply the correct voltage (5V DC) and must also be able to supply sufficient current (1.5A or greater per strand of 25 LEDs).

Excessive voltage will damage or destroy your LED pixel strand. The LEDs will only draw as much current as they need, however your power supply must provide at least 1.5A or greater for each strand. If you chain two strands together, you will need a 5V 3A power supply.

RGB LED pixel strand connection

There are 25 LED pixels per strand. Four of the wires at each end of the strand are terminated with a JST connector. The red wire is for power (VCC), blue wire for ground (GND), yellow wire is for Data, and green wire for Clock. A spare red wire (VCC) and a spare blue wire (GND) are attached to the ends of each strand for convenience, however, I did not use either. Please double check the colour of your wires... they may be different.

If you want to attach the LED strand to a breadboard, you can cut the JST connector off and use the LED pixel strand wires. Alternatively, if you would prefer to preserve the JST connector, you can simply insert jumper wires (or some male header pins) into the JST connector, and then plug them into the breadboard as required.

Each LED pixel is individually controllable using two pins on your Arduino. The strand is directional. i.e. There is an INPUT side and an OUTPUT side. The strand should be connected such that wires from the microcontroller are attached to the INPUT side of the first LED pixel. The arrows on each LED show the direction of data flow from INPUT to OUTPUT. The arrow on the first LED pixel should be pointing towards the second LED pixel, NOT towards the breadboard.

Other considerations

As a precaution, you should use a large capacitor across the + and - terminals of the power supply to prevent the initial onrush of current from damaging the RGB LED pixels. I used a 4700uF 16V Electrolytic capacitor for this purpose. According to Adafruit, a 1000uF 6.3V capacitor (or higher) will also do the trick. You may also want to consider a 330 ohm resistor between the Arduino Digital pin and the strand's DATA pin.

If you want to power the Arduino using the regulated 5V external power supply. Disconnect the USB cable from the Arduino, and then connect the positive terminal of the power supply to the 5V pin on the Arduino. Be warned however, that excess voltage at this pin could damage your Arduino, because the 5V regulator will be bypassed.
 
Providing the USB cable is NOT connected to the Arduino, it should now be safe to plug the power supply into the wall. This setup will allow you to power the RGB LED pixel strand and the Arduino using the same power supply.
 
WARNING: Never change any connections while the circuit is powered.

For more information about these RGB LED pixel strands, you may want to visit the Adafruit site. Adafruit was the source for most of these RGB LED pixel Strand precautions.


Fritzing diagram

The following diagram demonstrates how to connect the RGB LED pixel Strand to the Arduino and to the External 5V power supply.


This diagram was created using Fritzing


Connection Instructions

These instructions will help to guide you through the process of connecting your RGB LED pixel strand to the Arduino, and to the external power supply. The instructions assume that you will be powering the Arduino via a USB cable.



LightBox assembly

You will need to drill a 12mm hole into the craft timber box for each LED on the strand. It is worth taking the time to make accurate measurements before drilling the holes.
 
I made 12 holes for the outside circle pattern (12cm diameter), 6 holes for the inside circle pattern (8cm diameter), and a hole in the centre. I also made two holes at the front of the box, two on the left side, and two on the right side. I made one last hole at the back of the box for the 2.1mm DC power line socket.
 
Therefore you should have a total of 26 holes in the box. 25 of the holes are for the RGB LED pixel LEDs and one for the external power supply socket.

The lid of the box is about 19.5cm x 14.5cm long, which makes for a very tight squeeze. Probably too tight, because you have to account for the inner dimensions of the box. The inside of the box is used to house the Arduino, breadboard, the chipset side of the LEDs and cables/components. The inner dimensions of the box are 18cm x 13cm. Therefore, the housing for the LED chipset PCB (1.8cm x 2.5cm) prevented the box from closing. I used a Dremel to carve out the space required to close the lid.

Each LED is approximately 8cm apart on the strand, however, if you are really keen, you could cut the wires and extend them to any distance you require. But keep in mind that each LED is mounted on a small PCB (with a WS2801 chipset).You will therefore need to leave a minimum of 2cm between each 12mm hole to accomodate the size of the PCB+LED. If you plan carefully, you can probably squeeze a couple of LEDs within a distance of 1cm... but I would recommend that you give yourself a bit more room, because the PCBs are not square, and there is a good chance that you will have to start all over again.

In hindsight, I could have made the circle patterns a bit smaller, however I don't know if I could have packed these LEDs any closer. The diameter of the inner circle pattern must be at least 2cm smaller than the outer circle pattern. So I think "a bigger box" would have been the best option.

Once all of the holes have been drilled, paint and decorate the box to suit your style.

When the paint is dry, insert the LEDs into the drilled holes in number order.
You can see the end result below.



Project Pictures

These pictures show the Light box after it has been drilled and painted. The LEDs have been inserted into their respective holes, and all wires + Arduino + breadboard are hidden within the box.





Concluding comments

Once you start writing LED animations for the RGB LED pixel Lightbox, it is very hard to stop. The colour combinations



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.



Description: Arduino LED Light Box Rating: 3.5 Reviewer: Unknown ItemReviewed: Arduino LED Light Box
433 MHz RF module with Arduino Tutorial 3 Reviewed by Unknown on 01:30 Rating: 4.5

433 MHz RF module with Arduino Tutorial 3



 
There are 4 parts to this tutorial:
To get the most out of this tutorial - it is best to start at tutorial Part 1, and then progress to Part 2 then Part 3 and then do Part 4 last. Doing the RF tutorials in this order will help you to understand the process better.


Project 3: RF Remote Control Emulation

In the first tutorial, I introduced the 433 MHz Transmitter and Receiver with a simple sketch to test their functionality. In the second tutorial, the 433MHz receiver was used to receive a signal from an RF remote. The RF remote signal was coded based on the pattern and length of its HIGH and LOW signals. The signals received by the remote can be described by the code below:

 
Code comparison table



The RF remote that I am using transmits the same signal 6 times in a row. The signal to turn the light on is different from that used to turn the light off. In tutorial 2, we were able to "listen to" or receive the signal from the RF remote using the RF receiver. I thought it would be possible to just play back the signal received on the Arduino's analogPin, but the time it takes to perform a digital write is different to the time it takes to do an AnalogRead. Therefore it won't work. You need to slow down the digitalWrite speed.
I would like to find out if it is possible to apply this delay to all 433 MHz signal projects, however, I only have one 433 MHz remote.

If the delay in your project is the same as mine (or different) I would be keen to know - please leave a comment at the end of the tutorial.

We are going to use trial and error to find the optimal digitalWrite delay time. We will do this by slowly incrementing the delay until the transmission is successful. The transmission is considered successful if the fan-light turns on/off. All we have to do is count the number of transmissions until it is successful, then we should be able to calculate the delay.

 

Parts Required




 

The Transmitter Fritzing Sketch



 
 

RF Calibration - Arduino Sketch


I used an array to hold the RF code for light ON and light OFF. Each number within the code represents a specific sequence of HIGH and LOW lengths. For example, 2 represents a SHORT HIGH and a LONG LOW combination. A short length = 3, a long length = 7, and a very long length = 92. You need to multiply this by the timeDelay variable to identify how much time to transmit the HIGH and LOW signals for.
The short and long lengths were identified from the experiments performed in tutorial 2 (using the RF receiver). Each code is transmitted 6 times. The LED is turned on at the beginning of each transmission, and then turned off at the end of the transmission. The timeDelay variable starts at 5 microseconds, and is incremented by 10 microseconds with every transmission.
In the video, you will notice that there is some flexibility in the timeDelay value. The Mercator Fan/Light will turn on and off when the timeDelay variable is anywhere between 75 and 135 microseconds in length. It also seems to transmit successfully when the timeDelay variable is 175 microseconds.
So in theory, if we want to transmit a signal to the fan/light, we should be able to use any value between 75 and 135, however in future projects, I think I will use a value of 105, which is right about the middle of the range.


Video




  Now that I have the timeDelay variable, I should be able to simplify the steps required to replicate a remote control RF signal. Maybe there is room for one more tutorial on this topic :)

Update: Here it is - tutorial 4
Where you can record and playback an RF signal (without using your computer).




Description: 433 MHz RF module with Arduino Tutorial 3 Rating: 3.5 Reviewer: Unknown ItemReviewed: 433 MHz RF module with Arduino Tutorial 3
Mouse Controlling Arduino LEDs Reviewed by Unknown on 15:25 Rating: 4.5

Mouse Controlling Arduino LEDs


Use a mouse to control LEDs attached to an Arduino. This project uses the processing language to transmit the mouse coordinates to the Arduino, which then uses this information to turn on some LEDs. Please see the video below to see it in action.




Components Required for this project:

  • Arduino UNO
  • Breadboard
  • 9 LEDs
  • 9 x 330 ohm resistors
  • Wires to connect the circuit
  • USB connection cable: to connect the computer to the Arduino
  • A computer: to run the processing sketch, and to compile / upload the Arduino sketch
  • Processing Program installed on computer
  • Arduino Program installed on the computer

Arduino Sketch





















This was made using Fritzing.

Arduino Code

You can download the Arduino IDE from this site.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* This program was created by ScottC on 9/5/2012 to receive serial 
signals from a computer to turn on/off 1-9 LEDs */

void setup() {
// initialize the digital pins as an output.
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// Turn the Serial Protocol ON
Serial.begin(9600);
}

void loop() {
byte byteRead;

/* check if data has been sent from the computer: */
if (Serial.available()) {

/* read the most recent byte */
byteRead = Serial.read();
//You have to subtract '0' from the read Byte to convert from text to a number.
byteRead=byteRead-'0';

//Turn off all LEDS
for(int i=2; i<11; i++){
digitalWrite(i, LOW);
}

if(byteRead>0){
//Turn on the relevant LEDs
for(int i=1; i<(byteRead+1); i++){
digitalWrite(i+1, HIGH);
}
}
}
}

The code above was formatted using this site.


Processing Code

You can download the Processing IDE from this site.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Created by ScottC on 12/05/2012 to send mouse coordinates to Arduino

import processing.serial.*;

// Global variables
int new_sX, old_sX;
int nX, nY;
Serial myPort;

// Setup the Processing Canvas
void setup(){
size( 800, 400 );
strokeWeight( 10 );

//Open the serial port for communication with the Arduino
//Make sure the COM port is correct
myPort = new Serial(this, "COM6", 9600);
myPort.bufferUntil('\n');
}

// Draw the Window on the computer screen
void draw(){

// Fill canvas grey
background( 100 );

// Set the stroke colour to white
stroke(255);

// Draw a circle at the mouse location
ellipse( nX, nY, 10, 10 );

//Draw Line from the top of the page to the bottom of the page
//in line with the mouse.
line(nX,0,nX,height);
}


// Get the new mouse location and send it to the arduino
void mouseMoved(){
nX = mouseX;
nY = mouseY;

//map the mouse x coordinates to the LEDs on the Arduino.
new_sX=(int)map(nX,0,800,0,10);

if(new_sX==old_sX){
//do nothing
} else {
//only send values to the Arduino when the new X coordinates are different.
old_sX = new_sX;
myPort.write(""+new_sX);
}
}

The code above was formatted using this site.

Description: Mouse Controlling Arduino LEDs Rating: 3.5 Reviewer: Unknown ItemReviewed: Mouse Controlling Arduino LEDs
Arduino UNO: PhotoCell - sensing light Reviewed by Unknown on 21:53 Rating: 4.5

Arduino UNO: PhotoCell - sensing light

In this tutorial, we will send the analog readings obtained from a Photo Resistor, also known as a Light Dependent Resistor (LDR), to the computer. We will display the each reading on the monitor using a simple Processing sketch.


Here is what you will need to complete the Arduino side of the project:

Parts Required:

  • PhotoCell (or PhotoResistor)
  • 10K resistor
  • Breadboard
  • Arduino UNO
  • 3-4 wires(to connect it all together)
  • USB cable to upload sketch and for Serial communication with Processing.


Fritzing sketch:



Arduino Sketch


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

/* ==========================================================
Project : Send Photo resistor values to computer
Author: ScottC
Created: 25th June 2011
Description: This sketch will make the arduino read Photo resistor
             values on analog pin A0. The analog readings will
             be dependent on the amount of light reaching the
             sensor. The Analog readings will be sent to the
             computer via the USB cable using Serial communication.
==============================================================
*/

int photoRPin = 0;
int minLight; //Used to calibrate the readings
int maxLight; //Used to calibrate the readings
int lightLevel;
int adjustedLightLevel;

void setup() {
 Serial.begin(9600);
 
 //Setup the starting light level limits
 lightLevel=analogRead(photoRPin);
 minLight=lightLevel-20;
 maxLight=lightLevel;
}

void loop(){
 //auto-adjust the minimum and maximum limits in real time
 lightLevel=analogRead(photoRPin);
 if(minLight>lightLevel){
 minLight=lightLevel;
 }
 if(maxLight<lightLevel){
 maxLight=lightLevel;
 }
 
 //Adjust the light level to produce a result between 0 and 100.
 adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100);
 
 //Send the adjusted Light level result to Serial port (processing)
 Serial.println(adjustedLightLevel);
 
 //slow down the transmission for effective Serial communication.
 delay(50);
}

Arduino Sketch Explanation:

Serial.begin(9600) : starts the serial communication between the Arduino and Processing
lightLevel=analogRead(photoRPin): take a reading from the analog pin 0.
minLight and maxLight: automatically adjust the minimum and maximum range of the sensor
adjustedLightLevel: used to map the reading from the PhotoCell to a value between 0-100.

The adjustment of the light level is not necessary, and could be handled in Processing instead.
The delay(50) at the end of the sketch, is only used to slow down the transmission of the Serial messages to Processing. Depending on what you want to do with the sensor data, you may wish to increase or decrease the amount of delay.



 Processing Sketch

Here is a very basic Processing Sketch that will allow you to receive data from the Serial port attached to the Arduino and display the reading on your computer screen. In my case, the Arduino is connected to COM13. You may need to change this if you decide to follow in my footsteps.

The processing sketch will look for the line feed character ' \n' coming from COM13, which will trigger a serialEvent(). You can get processing to look for another character if you want to: just change the character within the bufferUntil() command. The draw() method is made redundant because the screen updating is handled by the serialEvent() in response to serial data communication with the Arduino UNO.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

/* ============================================================
Processing Version: 2.2.1
Project: Display Sensor data on computer screen
Author:  ScottC
Created: 25th June 2011
Description: This Processing sketch will allow you to display
             sensor data received from the Arduino via the
             serial COM port (or USB cable).
=============================================================== */
import processing.serial.*;
Serial myPort;
String sensorReading="";
PFont font;


void setup() {
  size(400,200);
  myPort = new Serial(this, "COM13", 9600);
  myPort.bufferUntil('\n');
  font = createFont(PFont.list()[2],32);
  textFont(font);
}

void draw() {
  //The serialEvent controls the display
}  

void serialEvent (Serial myPort){
 sensorReading = myPort.readStringUntil('\n');
  if(sensorReading != null){
    sensorReading=trim(sensorReading);
  }

  writeText("Sensor Reading: " + sensorReading);
}


void writeText(String textToWrite){
  background(255);
  fill(0);
  text(textToWrite, width/20, height/2);
}

This is how the data will be displayed on the computer screen:

If this tutorial helped you, please support me here:





Description: Arduino UNO: PhotoCell - sensing light Rating: 3.5 Reviewer: Unknown ItemReviewed: Arduino UNO: PhotoCell - sensing light
Arduino UNO: LED Sensor, Part One Reviewed by Unknown on 22:30 Rating: 4.5

Arduino UNO: LED Sensor, Part One

As seen in the previous blog postings, the LED (Light Emitting Diode) was used to DISPLAY the result of various sensors. It was also used to create a variety of Light patterns or sequences.

The LED is commonly used as an OUTPUT device, however, I have since found out: there is another option.

You can use the LED as an INPUT device !!



I have only tried this with a Yellow LED and a Red LED, however, this should work with any colour. Some sites recommend using a clear/transparent/colourless LED for best effect, but it depends on what you are trying to achieve.

The LED responds better to light of the same wavelength that it emits. So a yellow LED responds better to yellow light, and a red LED responds better to Red light.

The following experiment attempts to prove this theory.
A Red and Yellow LED alternate and fade in to maximum brightness using PWM (Analog Output). Meanwhile, a separate LED is used as an INPUT device to receive the light. The value obtained is plotted using a processing sketch.

The chart above used a Yellow LED to measure the light emitted from a Red and then a Yellow LED.
As the Yellow LED gets brighter, the INPUT LED takes less time to discharge, and thus produces a lower result. On the other hand, the Red LED has little effect on the INPUT LED, despite it's brightness.
Ambient light will produce different graph patterns.

Parts Required:
  • Arduino UNO
  • 2 x Yellow LEDs
  • 1 x Red LED
  • 3 x 330 ohm Resistors (choose resistors suitable for your LEDs)
  • 5 wires to connect the circuit
  • Breadboard
  • Processing software
Here is the Sketch:

     Created with Fritzing : http://fritzing.org/


Here is the Arduino Code: which was adapted from this site.
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#define LED_Sensor_NEG 8
#define LED_Sensor_POS 7
#define Yellow_LED 10
#define Red_LED 9

int switcher=0;
int lightLevelY=0;
int lightLevelR=0;

void setup(){
pinMode(LED_Sensor_POS,OUTPUT);
digitalWrite(LED_Sensor_POS,LOW);
pinMode(Yellow_LED,OUTPUT);
pinMode(Red_LED,OUTPUT);

//Turn on Serial Protocol
Serial.begin(9600);
}

void loop()
{

//Alternate the flashing of the Yellow and Red LED
if(switcher==0){
lightLevelR+=4;
if(lightLevelR>255){
lightLevelR=0;
switcher=1;
}
} else {
lightLevelY+=3;
if(lightLevelY>255){
lightLevelY=0;
switcher=0;
}
}
analogWrite(Red_LED,lightLevelR);
analogWrite(Yellow_LED,lightLevelY);


// Charge the LED by applying voltage in the opposite direction
pinMode(LED_Sensor_NEG,OUTPUT);
digitalWrite(LED_Sensor_NEG,HIGH);

// Set pin 8 to read the input and Turn off the internal pull up resistor.
// The greater the amount of light in the room, the smaller the number represented by variable "darkness".
long darkness=0;
int outputVal=0;
pinMode(LED_Sensor_NEG,INPUT);
digitalWrite(LED_Sensor_NEG,LOW);
while((digitalRead(LED_Sensor_NEG)!=0) && darkness<80000){
darkness++;
}

outputVal=darkness/80;
//Print the darkness level in the room.
Serial.println(outputVal);
}

Here is the Processing Code:     Code Source
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* Processing code for this example */

// Graphing sketch


// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, followed by a newline, or newline and carriage return
// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
// This example code is in the public domain.

import processing.serial.*;

Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph

void setup () {
// set the window size:
size(400, 300);

// List all the available serial ports
println(Serial.list());
// I know that the second port in the serial list on my PC
// is always my Arduino, so I open Serial.list()[1].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to a float and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);

// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
Description: Arduino UNO: LED Sensor, Part One Rating: 3.5 Reviewer: Unknown ItemReviewed: Arduino UNO: LED Sensor, Part One
Back to top