Using VS1053 ADC for voltage measurement etc.

Writing software for systems that use VLSI Solution's devices as slave codecs to a host microcontroller.
Post Reply
User avatar
pasi
VLSI Staff
Posts: 2120
Joined: Thu 2010-07-15 16:04

Using VS1053 ADC for voltage measurement etc.

Post by pasi »

Sometimes you want to measure analog voltages, for example the battery voltage or ambient illumination or perhaps background noise level and adjust display or playback parameters accordingly. But your microcontroller may not have analog to digital converters. In this case you can take advantage of the two ADC's of vs1053.

Even if you use recording from mic, you can use the line input channel for your own purposes.

You don't need to upload any software to vs1053. You can turn on the ADC and read values through the SCI interface. Note that the ADC (and audio path in general) in VS1053 is very different than in VS1003.

A quick setup procedure:
write 0xc042 to SCI_WRAMADDR, then write 0x0005 to WRAM. This configures the ADC for 48kHz (lowest possible). The power-on default in SCI_MODE is to keep mic amplifier off. If you write to SCI_MODE, check that you set or clear the LINE bit accordingly (to disable or enable the mic amplifier, respectively).

When the ADC is running, you can read the values with: write 0xc043 to SCI_WRAMADDR, then read WRAM once to get left channel (mic / line1) data, then read WRAM second time to get right channel (line2) data.

If you only want to read the right channel data, write 0xc044 to SCI_WRAMADDR, then read SCI_WRAM.

Even if you use mono recording feature, you can use the other channel for generic converter. When the encoding mode is active the ADC is already started, so you can just read the values.

Note: due to a hardware bug you can disable / enable the ADC only once after each hardware reset or the ADC channels may be swapped. A software reset should not disable the ADC though, so writing a new enable should not do anything.
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook
DenMCU
User
Posts: 5
Joined: Wed 2020-03-25 10:13

Re: Using VS1053 ADC for voltage measurement etc.

Post by DenMCU »

Hi everyone,

Thanks for your description about ADC use.
I need detect signal from MIC input on VS1053b Board (https://imall.com/product/VS1053-VS105 ... 1870085/uk).
I did code for arduino and and I try read signal from LINE2 (pin 48).
Mic input connected series to capacitor and resistor 4.7k and and then to VS1053b LINE2 (pin 48).

I use arduino VS1053 library https://github.com/adafruit/Adafruit_VS1053_Library

When I dont have signal in LINE 2 often I read wrong values. I mean 65498 and 65535.
Serial port log:

Code: Select all

$ 65498;
$ 9;
$ 65535;
$ 3;
$ 12;
$ 12;
$ 14;
$ 19;
$ 9;
$ 65533;
$ 65535;
$ 2;
$ 20;
$ 22;
$ 9;
$ 10;
$ 5;
$ 5;
$ 9;
$ 11;
$ 0;
$ 12;
$ 13;
$ 1;
$ 14;
$ 12;
$ 65529;
$ 6;
$ 6;
$ 3;
$ 15;
$ 15;
$ 17;
$ 27;
$ 13;
$ 2;
$ 65535;
$ 7;
$ 65527;


Code: Select all

#include <Adafruit_VS1053.h>

// These are the pins used
#define VS1053_RESET    8     // VS1053 reset pin (not used!)
#define VS1053_CS       6     // VS1053 chip select pin (output)
#define VS1053_DCS      7     // VS1053 Data/command select pin (output)
#define CARDCS          9     // Card chip select pin
// DREQ should be an Int pin *if possible* (not possible on 32u4)
#define VS1053_DREQ     2     // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
  Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);


void setup() {
  Serial.begin(115200);
  // Wait for serial port to be opened, remove this line for 'standalone' operation
  while (!Serial) {
    delay(1);
  }

  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("VS1053 Init Error"));
    while (1);
  }

  mic_vs1053_adc_init();
}

void loop() {
  // put your main code here, to run repeatedly:
  mic_vs1053_adc_read();
}

void mic_vs1053_adc_init(void)
{
  musicPlayer.sciWrite(VS1053_REG_WRAMADDR, 0xC042);
  musicPlayer.sciWrite(VS1053_REG_WRAM, 0x0005);
}

int mic_vs1053_adc_read(void)
{
  Serial.print("$ ");
  uint16_t val = 0;
  musicPlayer.sciWrite(VS1053_REG_WRAMADDR, 0xC044);
  val = musicPlayer.sciRead(VS1053_REG_WRAM);
  Serial.print(~val);
  Serial.println(";"); 
}

Could you help me understand what I do wrong?
User avatar
pasi
VLSI Staff
Posts: 2120
Joined: Thu 2010-07-15 16:04

Re: Using VS1053 ADC for voltage measurement etc.

Post by pasi »

The values are signed, so 65498 is actually -26, and 65535 is -1. Maybe that's your issue, maybe there are other things.

You also seem to be printing the one's complement of the value and not the inverted value.

Another thing you may encounter: if you stop/disable/turn off and restart the ADC without hardware reset, the ADC channels may get swapped.
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook
Post Reply