An array of music-reactive LEDs that uses Python and serial communications inputs
Devices and components
Arduino Uno Rev3
LED (generic)
74HC595 Shift Register
220 ohm resistance
Jumper wires (generic)
Software and tools
Arduino IDE
Python 3
Project description
What's different from other meters?
How does it work?
The results
Arduino Lights
Lights the LED according to the RMS
1int latchPin = 11;
2int clockPin = 9;
3int dataPin = 12;
4byte LEDs = 128 ;
5int rms;
6
7void lightUp() {
8 digitalWrite(latchPin, LOW);
9 shiftOut(dataPin, clockPin, LSBFIRST, LEDs);
10 digitalWrite(latchPin, HIGH);
11}
12
13void setup() {
14 // put your setup code here, to run once:
15 Serial.begin(115200);
16 pinMode(latchPin, OUTPUT);
17 pinMode(dataPin, OUTPUT);
18 pinMode(clockPin, OUTPUT);
19}
20
21
22void loop() {
23
24rms=(Serial.readStringUntil('/r')).toInt();
25
26if(rms<50){
27 LEDs = 0;
28}
29else if(rms >50 && rms<100){
30 LEDs = 128;
31}
32else if(rms >100 && rms<150){
33 LEDs = 192;
34}
35else if(rms >150 && rms<200){
36 LEDs = 224;
37}
38else if(rms >200 && rms<250){
39 LEDs = 240;
40}
41else if(rms >250 && rms<300){
42 LEDs = 248;
43}
44else if(rms >300 && rms<350){
45 LEDs = 252;
46}
47else if(rms >350 && rms<400){
48 LEDs = 254;
49}
50else if(rms >400){
51 LEDs = 255;
52}
53
54else{
55
56}
57
58lightUp();
59}
Audio analysis
python
Calculates, encodes and sends the rms value to Arduino
1import pyaudio
2import audioop
3import serial
4
5
6arduinoData = serial.Serial('com3',115200)
7
8CHUNK = 1024
9FORMAT = pyaudio.paInt16
10CHANNELS = 2
11RATE = 44100
12
13p = pyaudio.PyAudio()
14stream = p.open(format = FORMAT,
15 channels = CHANNELS,
16 rate = RATE,
17 input = True,
18 input_device_index = 2,
19 frames_per_buffer = CHUNK)
20
21
22while True:
23 data = stream.read(CHUNK)
24 rms = audioop.rms(data, 2)
25 rmsVal = str(rms) + '/r'
26 arduinoData.write(rmsVal.encode())
27 print(rms)
Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.