How to use CdS cells and Flex sensors

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to use CdS cells and Flex sensors

Post by Shane » Thu Jun 23, 2011 4:47 pm

A CdS (Cadmium Sulfide) Cell is a photoresistor whose variable resistance decreases as the intensity of light reaching the cell increases. This component can be used as a sensor to detect light conditions or as a sensor to detect movement - for example, when a constant light is shining on the photocell, a hand can be moving in front of the Cds Cell, creating variations of light intensity striking the cell. The CsC Cell can be used as an analogue input for the Arduino, much like a potentiometer, when a second fixed value resistor is used with the CdS to create a voltage divider. CdS Cells come in many ranges of resistance - the fixed resistor used in this circuit should be chosen to have a value near the upper limit of the CdS Cell's maximum resistance. In this example, our CdS measures a maximum resistance of about 11K Ohms, so we've used a 10K Ohm resistor.
1.jpg
1.jpg (148.73 KiB) Viewed 2925 times
The circuit is very straightforward as can be seen in the image and schematic. One lead of the CdS Cell is connected to an analogue input in the arduino (in our example, we have used A0). Also connected to the Arduino analogue input is the 10K Ohm resistor. The other lead of the resistor is connected to ground. The opposite lead of the CdS Cell is connected to +5VDC.

The arduino code necessary to use the CdS in this circuit as an analogue input is identical to the code for a potentiometer used as an analogue input. In this example, we use the input to control the flash rate of an LED connected to D13 (the same circuit and code as the potentiometer tutorial.) We have added the use of the Arduino Serial Window in this example. This feature can ba an invaluable de-bugging tool. The additions to the code are within the void setup () function we add the Serial.begin (9600); command, which opens the serial port for communication and also sets the baud rate (bits per second). The baud rate needs to be set to a value that the device the Arduino is communicating with expects (standard baud rates are 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200). In the top of the Arduino programming window on the far right is an icon for "Serial Monitor" (next to Upload to I/O Board). Select this option after you have uploaded the sketch into the Arduino and the serial window will be opened. Be sure to select 9600 as the baud rate from the drop down menu in this window.

The second addition to the code is the Serial.print () command used in the void loop () function. This command will print in the Arduino serial window whatever data is contained within the brackets. We are using a variation of the the Serial.print () command called the Serial.println () command. The only difference is that the Serial.print () command will print a string of data that will wrap through the Arduino serial window, whereas the Serial.println () command will insert a return after each data group, which can helpful for viewing the data in the window.

Code: Select all

// ANALOG INPUT - SENSOR
 // This example flashes an LED on and off at a rate determined by the analog
 // input value read on ANALOG IN 0 based on the light striking the CdS Cell. 
 // This value will be used to determine one cycle of delay time for the LED to 
 // be on and then off. The value of CdS_PIN is printed into the Arduino Serial 
 // Window so its value change can be monitored along with the change in flash
 // rate of the LED

int CdS_PIN = 0; // select the input pin for the CdS Cell
 int ledPIN = 13; // select the output pin for the LED
 int CdS_VAL = 0; // variable to store the value coming from the CdS Cell

void setup() {
 Serial.begin(9600); // set the baud rate for the serial window
 pinMode(ledPIN, OUTPUT); // declare the ledPin as an OUTPUT
                          // ANALOG IN is an input by default
 }

void loop() {
 CdS_VAL = analogRead(CdS_PIN); // read the value from the CdS Cell
 CdS_VAL = (CdS_VAL/2 + 16); // shift the CdS_VAL to be a useful flashrate
                             // (barely visible to 511ms)
 digitalWrite(ledPIN, HIGH); // set the ledPIN HIGH, which turns on the LED
 delay(CdS_VAL);             // do nothing for (CdS_VAL) milliseconds
 digitalWrite(ledPIN, LOW);  // set the ledPIN LOW, which turns off the LED
 delay(CdS_VAL);             // do nothing for (CdS_VAL) milliseconds
 Serial.println(CdS_VAL);    // print CdS_VAL in the serial arduino serial window
 }
There are many types of analogue sensors available that work on this same principle and can be used in this same way. One common and interesting example is the "flex sensor". Like the CdS Cell, a flex sensor is a variable resistor, though instead of responding to light intensity on its surface, its resistance varies with the amount that the sensor is bent. Also like the CdS cell, a resistor is needed in the circuit to complete the voltage divider. It should be chosen based upon the maximum resistance of the flex sensor - in our case, the same 10K Ohm resistor is an appropriate value. This flex sensor circuit can be run with exactly the same arduino code as for the CdS Cell. The flex sensor can be a very effective way to construct an interactive user interface .
3.jpg
3.jpg (137.12 KiB) Viewed 2925 times
4.jpg
4.jpg (196.38 KiB) Viewed 2925 times
5.jpg
5.jpg (157.24 KiB) Viewed 2925 times
2.jpg
2.jpg (161.57 KiB) Viewed 2925 times
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”