Tuesday, January 11, 2011

Light Sensor on Arduino

The next board I'd like to write about, is the light sensor mini board. The situation is similar to the temperature board. Easy to connect, but this time there was no datasheet at all. I couldn't find out which photoresistor was used, so the value which was read had to be approximated as well. I found a good resource of the theory of calculation here.

Here is the source code to this example:
int lightSensor = 2;
int lightADCReading;
double currentLightInLux;
double lightInputVoltage;
double lightResistance;

void setup() {

void loop() {
  lightADCReading = analogRead(lightSensor);
  // Calculating the voltage of the ADC for light
  lightInputVoltage = 5.0 * ((double)lightADCReading / 1024.0);
  // Calculating the resistance of the photoresistor in the voltage divider
  lightResistance = (10.0 * 5.0) / lightInputVoltage - 10.0;
  // Calculating the intensity of light in lux       
  currentLightInLux = 255.84 * pow(lightResistance, -10/9);

  Serial.print("Light ADC reading: ");
  Serial.print("Light in lux: ");

This is the output of the program:

Temperature Sensor on Arduino

The temperature sensor mini board from seeedstudio is also really easy to connect to the Arduino board. That's the beauty of those mini boards. However, it has one flaw though. The thermistor which is used on the board has not a very good datasheet. I wasn't able to come up with an exact formular to calculate the ADC readings to degrees celsius. I used a formular which I found on the web. Note that this formular is for linear thermistors and is meant for a certain circuit setup. So my readings are more or less approximations based on that formular. To shortly increase the value I breathed on the thermistor.

Here is the source:
int temperatureSensor = 1;
int temperatureADCReading;
float currentTemperatureInCelsius;

void setup() {

void loop() {
  temperatureADCReading = analogRead(temperatureSensor);
  currentTemperatureInCelsius = 25 + (temperatureADCReading - 512) / 11.3;

  Serial.print("Temperature ADC reading: ");
  Serial.print("Temperature in C: ");
This is the program printing out those readings:

Piezo Buzzer on Arduino

The piezo buzzer has a thin metal membrane. When voltage is applied to the component at a certain frequency, the membrane gets into a vibrating motion which produces soundwaves. With changes in frequency you can control the sound output.

Another great thing is, that you can use the piezo buzzer also as an input device. Audio waves can influence its membrane and can set it into a vibrating motion. When this happens, the output voltage of the component changes and you can detect sound.

There were already some great tutorials on the web, so I only had to change the source code according to pin layout or threshold values. Connecting the buzzer is pretty easy. In most cases you only might need a resistor in your circuit to limit the current, that's all. I simplified my experiments with an Arduino shield from seeedstudio.com which adapts the Arduino pins to connector ports and busses, which can be easily used when connecting to other boards. I also got some mini sensor circuit boards which makes prototyping quite easy. Here you can find the piezo buzzer used in this experiment.

Some simple experiments using the connector shield and the buzzer can be found here.

So first, let's hear the buzzer in action:

When experimenting with the frequencies and duration of the output, you even can produce some melodies. A tutorial of playing some notes can be found on the Arduino tutorial page.

Here is my attempt:

Now, to use the piezo buzzer for detecting sound or vibration you have to experiment with the threshold value in your code, so it won't be triggered by background noise. The Arduino tutorial page provides source code for this experiment as well.

I could have improved my threshold even more, but you can see that it works anyway.