Hi,
I'm a self-taught and pretty inexperienced hobbyist so I have very varied levels of understanding in different topics.
I've created a project that works perfectly until I come to deploy it with a different power supply. I'll keep it simple here by trying to only provide relevant points (please, if I've decided something isn't relevant and haven't included it, correct my "oversimplification" wherever you want!!!).
The project is based on an Arduino Uno and senses a pulse from an electricity meter. Its a light-based type and so uses an LDR connected to pin A0. The sketch flashes the pin 13 LED whenever a pulse is detected on the LDR. That all works perfectly when powered from a USB charger (the type as you'd use to charge a phone/tablet/etc).
In an attempt to learn I like to turn projects in to something built as much from components as possible and so have re-created this as a barebones ATMEGA328P-based PCB. Again, all works perfectly until....
My next "back to raw components" step is to replace the USB charger with a power supply which plugs directly into the mains. I have, from previous projects, some experience of using a cheap eBay-bought AC-DC buck convertor which has always suited my needs. Unfortunately on this occasion switching from the USB to buck supplies causes the software to rapidly sense a "read" on pin A0.
I assume, but am well out of my depth and don't even appear to be able to find anything on-line (presumably because I don't even have the vocabulary to successfully find related articles), that this is something to do with the power not being smooth enough to keep the pulled-down A0 pin at a sufficiently lower level than my analogue read threshold at all times and that the pin is being read at a voltage periodically (very quickly) that appears to be a true read but is, in fact, a false positive.
I'd be grateful for some pointers in where to look to learn more and create a more suitable mains power supply rather than taking the easy option of buying off the shelf (aka phone charger).
On the other hand, it may be nothing to do with the above, in which case I'd be equally interested in enough info to help me learn where I've created the problem.
MORE INFO...
The issue occurs regardless of whether the sketch reads an analogue pin (with an LDR) or a digital pin (with a switch) AS LONG AS THE PIN IS PULLED DOWN (currently via a 4k7 resistor). If I remove the pull-down the issue goes away (but also of course ceases to read accurately when there is a genuine pulse).
The issue occurs whether I use my barebones creation or a genuine Arduino Uno
When I connect USB power it is to the USB socket on the Arduino (or to the 5V and GND pins of the L7805CV voltage regulator on the barebones PCB). When I connect the buck power it is to the Vin and Gnd pins on the Arduino or same on the PCB.
When I use a bench power supply all options work perfectly on both Arduino and barebones either at 5V to the 5V pin, or at anything between 7 and 12V to the Vin pin.
Circuit diagram attached
Sketch as follows (note its a single sketch with the digitalRead version commented out at present. Also, its simplified to take out all the bits that I'm confident are unrelated - ie it doesn't actually flash an LED alone but writes over a serially-attached ESP8266 WiFi module to an MQTT broker which updates my Node-RED dashboard. I've taken those bits out for simplicity)...
#define pulsePin A0 // analogue pin for LDR circuit
//#define pulsePin 6 // digital pin for simple switch circuit
#define ledPin 13
#define pulseThreshold 200 // level at which LDR performs most accurately on my specific meter
boolean meterPulse = false;
boolean currentlyPulsing = false; // indicates whether a pulse that may currently be seen is new or started in the last loop
void setup() {
// setup pins
pinMode(pulsePin, INPUT);
pinMode(ledPin, OUTPUT);
// initialize hardware serial for debugging on serial monitor
Serial.begin(9600);
}
void loop() {
// read the input, set a flag if its high, set a flag if its low
if (analogRead(pulsePin) > pulseThreshold) { // comment out to demonstrate fault with digital version on switch
// if (digitalRead(pulsePin)) { // commented out at present so as to use the sketch as the analogue LDR version
meterPulse = true;
} else {
meterPulse = false;
}
// using the flag from above decide whether there's a pulse in progress...
if (meterPulse && !currentlyPulsing) { // ie if a pulse is detected (ie meter LED is lit) ie meterPulse is true, but it wasn't lit on last loop ie currentlyPulsing is false
currentlyPulsing = true; // ie record that we now recognise that there's a pulse in progress
Serial.println("Pulse START");
digitalWrite(ledPin, HIGH);
}
if (!meterPulse && currentlyPulsing) { // ie if we recognised that there's a pulse in progress on at least the last loop, but now its ended - ie meterPulse is false, ie meter LED has gone out
sprintf(str_payload,"%lu",millis());
currentlyPulsing = false;
Serial.print("Pulse END : ");
Serial.println(str_payload);
digitalWrite(ledPin, LOW);
}
}