Piezo with an Arduino & photoresistor « adafruit industries blog

Piezo with an Arduino & photoresistor

Short video (m4v) of a piezo, photoresistor and Arduino….

Pt 1951
Here’s the breadboard view to wire this up yourself…

And.. here’s the code we used from TodBot!


int photosensorPin = 0;
int piezoPin = 9;

int val = 0;

void setup() {
  pinMode(piezoPin, OUTPUT);
}

void loop() {
  digitalWrite(piezoPin, LOW);
  val = analogRead(photosensorPin);
  val = val/2;

  for( int i=0; i<500; i++ ) {  // play it for 50 cycles
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(val);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(val);
  }
}


5 Comments »

  1. Cool little project. I just bought a photoresistor but didn’t know what to do with it. I’ll try this soon!

    Also: how did you do the breadboard/Arduino diagram? From scratch or is there a template that can be used with other software?

    Comment by MikeD — May 20, 2009 @ 2:37 pm

  2. check out fritzing.org – that’s how we make the diagrams.

    Comment by adafruit — May 20, 2009 @ 2:57 pm

  3. It looks like the source code was accidentally mutilated.

    Comment by Len — May 20, 2009 @ 4:26 pm

  4. fixed – we were adding some new formatting.

    Comment by adafruit — May 20, 2009 @ 5:28 pm

  5. The “for” Loop needs to be updated with “piezoPin” instead of “speakerPin” in the digitalWrite elements.

    Comment by Jon — May 21, 2009 @ 11:23 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Prove you are human by reading this resistor:
0Ω+/- 5%

0
0
1
2
3
4
5
6
7
8
9

0
0
1
2
3
4
5
6
7
8
9

0
0
1
2
3
4
5
6
7
8
9

5
5
10
20

Match the sliders on the left to each color band on the resistor.

Click Here for a new resistor image.

New to electronics? Click here to learn how to read resistor values.

www.flickr.com
adafruit's items Go to adafruit's photostream
www.flickr.com
items in Adafruits More in Adafruits pool