Odd analog readings (part 2)

@bko, Provided a fix for the extra zero in the converted data by:
1)Changing the DMA mode from “circular” to "normal"
2)Enabling ADC2 external trigger conversion for each analogRead
3)Reset the buffer and DMA counter before starting the ADC-DMA command

Commt : https://github.com/spark/core-firmware/commit/b7ce24a4fb2dfe4f90e597e3a0f568f9ae098cfe

2 Likes

Hi @satishgn

This worked great on my test case and I like that you merged the loops to cut down the overhead.

Nice job and thanks!

2 Likes

Thanks @bkp for verifying the results.

For those interested in STM32’s Advanced ADC Modes, here is an application note from ST:

When do you think this will get pushed out to allow us to build using the WebIDE and not experience the cross-talking issues between analog channels? I know I can use the USB approach, but I’m curious what the timeline is for seeing it in the over-the-air version.

@mbeasley Usually it will be announced in the forum as well.

@satishgn I haven’t read the whole thing yet, but this jumped out at me:

The dual slow interleaved ADC mode is intended for the conversion of one channel. ADC1
and ADC2 convert the selected channel alternately with a period of 14 ADC clock cycles.
The channel is thus converted every 14 clock cycles. Each ADC converts the channel every
28 ADC clock cycles. The conversion can be started by external trigger or by software and
the conversion results of ADC1 and ADC2 are stored into ADC1’s data register (32-bit
format).
The maximum allowed sampling time is 14 ADC clock cycles to avoid any overlap with the
next conversion.
This means that the only allowed sampling times are 1.5, 7.5 and 13.5
cycles.

Yet we still have it set to 41.5.

EDIT: Sounds like the effective sampling rate using two ADCs at 13.5 is still about half as good impedance wise as one ADC set to 41.5. However the sampling rate is higher. I guess for a small percentage of users this would be beneficial. Most would want a rock solid reading.

1 Like

@BDub, good find. This was a big surprise to me as how the sampling time was bumped from the original 1.5 to the current 41.5. Think it happened during the community provided merge. I will update and test the code again to see how the change affects the whole process.
Thanks

Yes it was from a direct result of this thread :slight_smile: See first post.

I’m guessing you’ll want to stay to the high end 13.5. Having it set to 41.5 probably yields 13.5 max anyway so I don’t know if there will be much change in how it responds. Comparing to the old way of 41.5 single channel/conversion might be interesting though.

@BDub correct after trying out with 1.5, 7.5 & 13.5, readings are pretty much similar to 41.5 sampling time. But I haven’t tested it comprehensively as you did in your first post. Would you like to repeat the test and see how the use of Dual ADC compares with the single ADC in terms of input impedence and publish the result.
Thanks,
Satish.

@BDub, Default sampling time is now set to 7.5 and added a new setADCSampleTime() which can be called in setup() to override the default ADC Sampling time.

Ahh cool! I was totally going to suggest that since it seems like some people might need faster sampling and can take care of making sure the input impedance is low enough to get good readings. I’ll try to get some testing in tonight.

I like the new setADCSampleTime() function–looks great!

I tried the new master tonight with the 7cycles5 default rate and that worked great for my simple test.

3 Likes

Well it was very nice to return home and find that the ADC issues are solved - thanks guys. The new code gives excellent results with my 50Hz measurements so I can get back to the Energy Monitor development.

I’m getting around a 35uS conversion time with 1_5 set in setADCSampleTime() and 40uS with the default 7_5 so there is not a lot of difference. If I want faster sampling for my next project I’ll go back to the pre-DMA version of the code which was much faster but make sure I take care with input impedance.

3 Likes

Can someone help me out with the circuit of the temperature sensor interfaced with spark core? My program is correct but some problem in circuit.? Reply ASAP

Thanks in Advance

Hi @anirudh,

Can you post a picture or diagram of your circuit? What do you need help with?

Thanks,
David

Hello @Dave

I’m doing basing on this circuit, I’ve connected everything, I need the code of it, REPLY ASAP

Thanks in Advance

@anirudh Hi there! The code can be found here…
http://docs.spark.io/examples/#measuring-the-temperature

// -----------------
// Read temperature
// -----------------

// Create a variable that will store the temperature value
int temperature = 0;

void setup()
{
  // Register a Spark variable here
  Spark.variable("temperature", &temperature, INT);

  // Connect the temperature sensor to A7 and configure it
  // to be an input
  pinMode(A7, INPUT);
}

void loop()
{
  // Keep reading the temperature so when we make an API
  // call to read its value, we have the latest one
  temperature = analogRead(A7);
}

How to read the data:

voltage = (sensor reading x 3.3)/4095
Temperature (in Celsius) = (voltage - 0.5) X 100

The API request will look something like this:
GET /v1/devices/{DEVICE_ID}/temperature

# EXAMPLE REQUEST IN TERMINAL
# Core ID is 0123456789abcdef
# Your access token is 123412341234
curl -G https://api.spark.io/v1/devices/0123456789abcdef/temperature -d access_token=123412341234

Since this is a GET request you can also just use a URL like this in your browser:
https://api.spark.io/v1/devices/0123456789abcdef/temperature&access_token=123412341234

Perhaps you have this already? What issues are you having?

2 Likes

Hi @anirudh,

Looks like @Bdub got back to you quickly, but just jumping in too to say Hi. :slight_smile:

edit: for reference, I’m in the CST time zone and although I often am online, I won’t necessarily be online at midnight for an asap response. :slight_smile:

Thanks,
David

2 Likes

Hey @Dave :smile:

Since I got your reply as soon as i posted the problem, In my next thing I said reply ASAP.

Otherwise I wouldn’t have said it :stuck_out_tongue:

Hail Spark team :smile:

1 Like

Ahh, haha, sorry! :slight_smile: