tni / teensy-samples

BSD 2-Clause "Simplified" License
8 stars 5 forks source link

Reading out sensor-values #2

Open j-protex opened 6 years ago

j-protex commented 6 years ago

Hey there,

I was really excited when I found your code for sampling at a constant rate and even more excited when I got the "SdFatSDIO_low_latency_logger.ino"-example running for my purpose. I (simply) want to log the signal of a PIR-sensor (motion-sensor). So I actually just changed the ISR-part like this (storage is my struct for the data that has to be saved):

void captureData() { storage.logTime = micros(); for (uint8_t i = 0 ; i < 2 ; i++) { storage.data[i] = analogRead(i); } logEntry( {storage.logTime, storage.data[0], storage.data[1]}); }

Now, when I looked at the logged data, the sample rate was perfectly constant at 1 kHz. But although I made a test measurement inside an empty room (where there was no movement at all) there were these strange peaks in the signal: peaks_pir

These peaks seem to appear just when the Teensy is actually writing. While the code was running I simultaneously measured the signal with an oscilloscope. During the first 20 secs, where the Teensy is just waiting until it is mounted, the signal was constant and the peaks began to appear right after the start of the saving process.

The rate of peaks (from one peak to the next) is about 800 mHz, or every 1.2 seconds.

Any ideas, how to avoid the peaks?

Best regards and thank you for your time!

tni commented 6 years ago

If I had to guess, you are seeing power fluctuations. SD cards have a power consumption in the order of microwatts when they sleep, a write may draw several hundred mA (cards vary a lot). That's a rather massive load step for the voltage regulator.

The analog reference is by default using the 3.3V supply voltage, so any supply voltage fluctuation will show up in measurements.

Try using the internal voltage reference, if things look different. Check the AREF pin, if the 3.3V analog reference voltage is stable.

j-protex commented 6 years ago

Thank you for the fast response! I measured the 3.3V pin and couldn't see any large changes. But without changing the analog reference (= using the 3.3V supply) there were peaks in AREF correlating with the sensor signal peaks.

I don't really understand what you mean.. do you think having an external AREF for analogRead could improve the signal? For example using another voltage source?

tni commented 6 years ago

Since you are seeing AREF fluctuations that are correlated with the signal fluctuation, yes, an external reference would help a lot.

If you look at the Teensy schematic: https://www.pjrc.com/teensy/schematic.html VREFH (external reference input) is fed from 3.3V via a 470Ohm resistor. You either need to perform micro-surgery to feed VREFH from a stable voltage (and disconnect it from 3.3V) or you can connect a very low impedance reference to AREF (e.g. a shunt reference between AREF and ground).

Teensy has a 1.2V bandgap reference built in that you can try first. E.g. look at: https://forum.pjrc.com/threads/25532-ADC-library-update-now-with-support-for-Teensy-3-1

The 1.2V internal reference isn't great, but it should do better than the fluctuations you are getting.

A different reference won't solve the problem completely, since the Teensy analog VDDA will still fluctuate (tied to 3.3V via a ferrite). So if it doesn't work well enough, I would power the SD card with a separate power supply. E.g., there are SD card extension cables, where you could splice in power.

j-protex commented 6 years ago

Thanks again for your answer. I removed the 470 Ohm resistor and used an external 3.3 V regulator for AREF. Results seems to be perfect now. I couldn't see anything weird in the signal! I already ordered a shunt reference to try that too. This can then be connected directly to AREF without removing the resistor before?

j-protex commented 6 years ago

Hello again,

after using your logger for a lot of measurements I came across another problem: I am now using it for logging 3 motion-sensor signals. The measurements always take a few seconds, depending on whether there is a motion detected. After logging, the Teensy goes back into sleep mode until it is woken up by a motion trigger again. I want to use this system in the field, where it is supposed to measure for a few days. The first measurements are always fine, but then, after a certain time which I can not determine and I don't see when and why it happens, all the files have the maximum file size (log_file_size) and there is no content. I can't say that it happens after a certain amount of data is saved or that it depends on the number of files.. Any idea why this happens?

Best regards, Julian

tni commented 5 years ago

I just saw your comment. No, I have no idea why the file would be empty. Everything is working fine in my tests. I've had issues with the SdFat library in the past, where it was unable to read some valid file systems. The library comes with an example for formatting the SD card. You might want to use that to start with a clean file system.

If you can reproduce the issue, please post an example.