Arduino Basics Lesson 2-2: An Energy Conversion Unit

Arduino Basics Lesson 2-2: An Energy Conversion Unit

For the “Arduino For Kooks” course, I recommend you get the Arduino Starter Kit available here.

Building on our last project, we’re going to find a new way to control our piezo buzzer. Rather than using buttons, we’re going to use the variable resistance of a photoresistor to create different tones–a sort of light-activated Theremin. A photoresistor is an element that changes resistance based on the amount of light it detects. The more light, the less resistance. This device will convert those resistance values into various tones for our Theremin.

The Circuit:

Connect a jumper wire from +5V and GND to their respective busses on the breadboard. Connect a jumper from the +5V bus rail to one side of the photoresistor. This part is not polarized, so it doesn’t matter which side. Connect the opposite side of the photoresistor to GND via a 10K resistor and to A0 via another jumper.

Connect D8 to the positive leg of the piezo via a jumper and connect the ground leg of the piezo to GND.The Sketch:

int sensorValue;
// variable to calibrate low value
int sensorLow = 1023;
// variable to calibrate high value
int sensorHigh = 0;
// LED pin
const int ledPin = 13;

void setup() {
// Make the LED pin an output and turn it on
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// calibrate for the first five seconds after program runs
while (millis() < 5000) { // save the maximum sensor value sensorValue = analogRead(A0); if (sensorValue > sensorHigh) {
sensorHigh = sensorValue;
// save the minimum sensor value
if (sensorValue < sensorLow) {
sensorLow = sensorValue;
//turn the LED off, signaling the end of the calibration
digitalWrite(ledPin, LOW);

void loop() {
//read the input from A0 and store it in a variable
// map the sensor values to a wide range of pitches
int pitch=map(sensorValue, sensorLow, sensorHigh, 50, 4000);
// play the tone for 20 ms on pin 8
tone(8, pitch, 20);
// wait for 10ms

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.