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
}
Courtecy of UMASS AMHERST