1 minute read
Summary
234 Hacking Electronics
the sound’s waveform. So to find the “loudness” we need to first subtract 511 from the raw value—511 being equivalent to the 2.5V offset in the raw analog reading that spans from 0 to 1023.
The “abs” function makes any negative number positive and then divides the whole result by 10 to give us a number between 0 and 51 and assigns it to the variable “topLED”. We are not actually using LEDs, but you could think of each “*” as being an LED illuminated on a bar graph display.
The “for” loop then prints a number of “*”s equal to the value held in “topLED”. Finally, a new line is printed and we delay for 1/10th of a second.
void loop() { int value = analogRead(soundPin); int topLED = 1 + abs(value - 511) / 10;
for (int i = 0; i < topLED; i++) { Serial.print("*"); } Serial.println(); delay(100); }
Summary
In addition to the how-tos just covered, there are lots of audio modules you can make use of. Low-cost stereo power amplifiers are available from eBay and suppliers like SparkFun and Adafruit.
You can also buy ultra-low-cost amplified speakers intended for computers and reuse them in your projects.