Week 9: Night Musical Box!


Get information from at least one analog sensor and at least one digital sensor (switch), and use this information to control at least two LEDs, one in a digital fashion and the other in an analog fashion, in some creative way.


I started with creating a box out of the cardboard, with holes on the top for the lights. Using the light sensor , I detected when the lights were out and only when it was dark would the potentiometer (knob) get power from its port, and transfer it current to different leds and the buzzer. I used 330 ohm resistor for each leds, and 10k resistor for the light sensor and the buzzer.  Here is a photo of the setup:

The output of the knob is divided between three intervals. On each interval, one light lights up , starting with the yellow led and ending at the blue one.  The buzzer too at each interval beeps at different frequencies, giving a musical note.

Here’s a video:


//declaring the ports
const int pot_power = 2;
const int ledyellow = 3;
const int ledred = 5;
const int ledblue = 9;
const int buzzer = 11;
const int light = 13;
const int pot = A0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // begin detecting the sensors
  //declaring role of each port
  pinMode(pot_power, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledred, OUTPUT);
  pinMode(ledblue, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(light, INPUT);

void loop() {
  // put your main code here, to run repeatedly:

  int lightValue = digitalRead(light); //reading value from the light port

  if (lightValue = 1) { //condition
    digitalWrite(pot_power, HIGH);
  } else {
    digitalWrite(pot_power, LOW);
    noTone(buzzer);// switch off buzzer
  delay (100);

  int potValue = analogRead(pot); // reading value of the dial
  int mappedValue = map(potValue, 0, 1023, 0, 255); //mapping it to a value compatible for LEDs
  //  Serial.println(lightValue);
  //  Serial.println(mappedValue);
  if ( mappedValue < 85) {
    analogWrite(ledyellow, HIGH);
    analogWrite(ledred, LOW);
    analogWrite(ledblue, LOW);
    tone(buzzer, 1000);
  if (mappedValue >= 85 && mappedValue < 170) {
    analogWrite(ledyellow, LOW);
    analogWrite(ledred, HIGH);
    analogWrite(ledblue, LOW);
    tone (buzzer, 1500);
  if (mappedValue >= 170) {
    analogWrite(ledyellow, LOW);
    analogWrite(ledred, LOW);
    analogWrite(ledblue, HIGH);
    tone(buzzer, 2000);


Leave a Reply