So I've been playing with one of those nbmpt101b boards to read mains voltage (US 120v 60hz) but I don't have a scope so I figured I'd just get an idea of the output with my Mega 2560 using the serial plotter.
I'm using AREF set to 3.3v with the nbmpt101b at 3.3v as well.
With a delay of 50 after the analogRead I get an expected sine wave. But if the delay is anything more or less than 50 it just goes crazy. You can see in the screen capture below, at delay 40 and 60 or even 51,52,53... Why do I only see a clean sine wave at exactly delay of 50?
I am not seeing the same results you are getting. I can tell you this. The serial plotter vertical Y axis will change based on amplitude (auto-range) and the horizontal X axis has 500 points and each tick of the axis is equal to an executed Serial.println() command.
Here is what I had:
int analogPin = analogRead(A0);
int val=0;
void setup() {
analogRead (A0);
Serial.begin (115200);
}
void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
delay(16.66);
}
Figure the analogRead can run about 9,615 samples/sec but the reality is less. I used 60 Hz applied and since the time for a single cycle of 60 Hz is 16.66 m/Sec I set the delay for that. Anyway using a delay of 50, 25 and even 10 m/Sec there was not much change in my plots.
Edit, I was able to duplicate what you are seeing. I made my code like yours.
Serial.println(val); // debug value
Serial.print(" ");
Keep in mind the Serial.println is what starts the 500 points on the X axis. Your code follows that with Serial.print(" "); and that is what is doing it somehow.
Adding a delay that long will introduce aliasing.
There should be at least two samples per sine wave (<= 10ms delay for 50 Hz).
Better to let the A/D sample at max speed (no delays) for a certain time.
Leo..
Thanks folks, taking out the the serial.Print(" ") and playing with some other delay values worked better for me.
At the moment I'm just messing around a bit and mostly wanted to check this cheapo zmpt101b board even worked. I was able to use the Arduino plot to adjust the pot on this the board to make sure the tops of the waves were not being clipped.
Working on a whole home energy meter (like the other 1000 projects out there on the net). Built one years ago with an Arduino Uno but this new one will be Raspberry Pi based with an external ADC.
Tough trying to get it together without having a scope...Honestly I don't even remember how I built the original Arduino based one. I have to dig deep in storage to find it, I have the code somewhere in my mess of files
Wawa:
delay(16.66); // :o
A delay only takes integers.
Adding a delay that long will introduce aliasing.
There should be at least two samples per sine wave (<= 10ms delay for 50 Hz).
Better to let the A/D sample at max speed (no delays) for a certain time.
Leo..
Ya know I wondered about that. I figured it would bounce that delay but it took it. Never expected it to load. So why? I haven't a clue and 16.66 sure isn't and integer. So why did the code sample even load? Anyone?
I used an ole function generator to get the 60 Hz and offset the output by 2.5 vdc.