DFPlayer and LED

Hi there,
I use an Arduino UNO with the DFPLayer module. So far, that's going quite well. Now I have the problem that I would like to control an LED depending on the volume. So, if loud noises and tones are played, I would like the LED to be brighter and, if it is quieter, the LED to go darker. A kind of graphic volume display.
My approach so far has been to connect the output of DFPlayer pin 6 (I also tried pin 8 ) with A0 to the Arduino UNO and read out the analog values with analogRead (). I have no idea why, but I only get completely nonsensical values. The values change regardless of what is played through the speaker. Even if I don't make any sounds or noises at all, I get quite high values. Where am I doing something wrong? How could I get it right? I'm really at a loss!

Thanks in advance

Tobias

You have the project in front of you. We only know what you have told us, this is rather incomplete.

You can’t just connect an audio output to an Arduino analogue input pin. It needs either an envelope follower or a biasing circuit ( two resistors ) and AC coupling ( a series’s capacitor ).

Maybe you have a little circuit diagram for me? I'm a little lost at the moment and grateful for any help!

Tobias

Google the words

Envelope follower
Or

envelope detector

Then google the words
Arduino audio input.

Or click on this link
https://forum.arduino.cc/index.php?topic=508537.0

It's not exactly what you want but my [u]World's Simplest Lighting Effect[/u] should get you started and it will flicker/flash the LED with the sound/music. And, there is a schematic for the bias circuit.

… My "real effects" use a [u]Peak Detector/Envelope Follower[/u] but the bias circuit is easier (while making the software a little more complicated).

Maybe I'm stupid but it will not work.

I do not messure anything. What do I do wrong???

Tobias

The LEDs are not connected to an PWM capable pin, those are the ones with ~ next to their number. As they are connected in anti parallel only one of the will work anyway.
Please post the code you are trying to use here.

Note that an analogue read will give you the numbers from 0 to 1023 with silence coming out at about 512. The more deviation from this number both positive and negative give the volume. So you are best subtracting 512 from your reading and taking the abs ( absolutely value ) of the result.

Hi,
yes, my mistake. I drew it yesterday out of my head and I mixed it completly up. I'm Sorry.
Now I attached the correct circuit and also my sketch.
In fact it doesn't matter if there is one or two LED, because they both should light always with the same brighness.

My measurements on A0 (output with Serial.print ()) always give completely arbitrary values. Even if I don't put any music on the speaker, arbitrary values continue to come out.

Tobias

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11);
DFRobotDFPlayerMini myDFPlayer;
int sensorPin = A0;
int sensorValue = 0;
int ledPin1 = 3;
int ledPin2 = 4;

void setup()
{
  mySoftwareSerial.begin(9600);
  if (!myDFPlayer.begin(mySoftwareSerial))
    while(true){
      delay(0);
    }
  myDFPlayer.volume(10);
  myDFPlayer.loopFolder(1);
}

void loop()
{
  sensorValue = analogRead(sensorPin);
  analogWrite(ledPin1, sensorValue);
  analogWrite(ledPin2, sensorValue);
}

Even if I don't put any music on the speaker, arbitrary values continue to come out.

That means that the circuit is not wired as shown, or the contacts on the board are not making contact with the components.

If you have a DVM then measure the voltage at A0, you should see it at about 2.5V. If you haven’t got a DVM then get one, should be less than $10 at a thrift shop.

I measured it and I always get 2.46xV. The x fluctuates between 0.004V and 0.006V, so I think I always have almost 2.5V. As I said, no significant changes can be measured.
To be on the safe side, I set up the circuit again exactly according to the circuit diagram that I had attached. I get exactly the same values there. And I took other components. So a second DFPlayer, a second Arduino and of course everything else new. And yes, I measured the resistances beforehand to be 100% sure.
But where's my mistake? What am I doing wrong??

Tobias

Well you said:-

My measurements on A0 (output with Serial.print ()) always give completely arbitrary values.

So that means your 2.5V point on the board is not connected correctly to the A0 on the Arduino, or the power and ground are not connected to the board.

Do you have a solderless bread board with a gap between the red and black lines. That means there is no electrical continuity between those gaps.

Posting a photograph of your setup would help.

I once attached the desired photo. I don't know if that can help. The three cables hanging loose are just my measuring points. I measure the desired 4.9V via the voltage divider and 2.4xV across each resistor. So as actually desired!

Tobias

I measured it and I always get 2.46xV. The x fluctuates between 0.004V and 0.006V, so I think I always have almost 2.5V. As I said, no significant changes can be measured.

You meter won't show the audio. It will tend to read the approximate average (about 2.5V) even with the audio coming in.

AnallogRead() will read the instantaneous values and the readings will "look random" because you are measuring a constantly-changing waveform.

But as Mike says, you'll get a bigger range of "random" numbers with louder sound. Your readings should deviate at least +/-100 counts from the ~512 bias but of course that depends on "loudness".

My measurements on A0 (output with Serial.print ()) always give completely arbitrary values.

The code you're showing doesn't display those numbers.

Within what range? Those numbers don't change at all with sound playing? You're hearing sound, right? Is the volume cranked-up?

  analogWrite(ledPin1, sensorValue);A couple of problems here. AnalogRead is 10-bits and it goes from 0-1023.
analogWrite() is 8-bits and it goes from 0-255. If you write a bigger number it will "roll over", you'll loose the 2 most significant bits and it will be totally fouled-up.

And with the 2.5V bias analogRead() will read about 512 with silence.

You can subtract-out the bias, then you can throw-away the negative reading or take the absolute value, or take a moving average of the positive values, or whatever.

Then, you can divide by 4 or [u]map()[/u] the 0-1023 readings to the 0-255 range.

You may not get the full 0-1023 range so you may want to further-manipulate the numbers but analogWrite() should be constrained to the 0-255 range (positive numbers only).

The bit that concerns me is the quote

Even if I don't put any music on the speaker, arbitrary values continue to come out.

That software in inadequate if you have a biased audio input.
Two things

  1. The brightness will be changing far too fast for you to see it as a brightness change.
  2. You will need to grab the peak voltage not every reading.