Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

Project 5.02 Light and Sound

 The code is simple but it produces an interesting effect.  Upload the sketch and then wave a hand over the Arno or hold it up to a light source.  It’s fun to ask someone to find the phototransistor on the board.  Most people I’ve asked figure it out pretty quickly by listening to the tone that’s generated as they hold their hand over different parts of the board.

Concepts: analogRead, frequencies, map function

Circuits: 
  • Circuit 4
  • Circuit 5


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 5.02 Light and Sound
    int photoTran = A1;
    int reading = 0;
    int frequency = 0;
    int piezo = 12;
    void setup(){
    pinMode(photoTran,INPUT);
    pinMode(piezo,OUTPUT);
    }
    void loop(){
    reading = analogRead(photoTran);
    frequency = map(reading,0,1023,1000,10000);
    piezoTone(frequency,10);
    }
    void piezoTone(long freq, long duration){
    long aSecond = 1000000;
    long period = aSecond/freq;
    duration = duration*1000;
    duration = duration/period;
    for(long k = 0; k < duration; k++){
    digitalWrite(piezo,HIGH);
    delayMicroseconds(period/2);
    digitalWrite(piezo,LOW);
    delayMicroseconds(period/2);
    }
    }
    ///////////////////////////////////////////////////

    The phototransistor on the Arno board has two pins.  One is connected to the 5V power supply and the other to a resistor that is connected to ground.  The phototransistor and resistor act as a voltage divider.  The Arno pin photoTran reads the voltage drop between the phototransistor and resistor:

      reading = analogRead(photoTran);

    We set the frequency of the piezo with the map function.  We map the range of the reading, o to 1023, to a range of 1000 to 10,000 hertz:

      frequency = map(reading,0,1023,1000,10000);

    Finally, we call the piezoTone function that we developed in Projects 4.  The frequency used in the call to piezoTone is set by the phototransistor:

      piezoTone(frequency,10);

    The tone lasts for 10 milliseconds.  The frequency can then be updated as the program moves back to the top of the loop() block and takes a new measurement of photoTran.

    Back to Projects 5

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.