False positives (only on certain power source)

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);
  }
}

Lots of words but one rather important missing bit of information...what EXACTLY is the mains power supply that seems to cause the problems? Link? Detailed specification?

Unfortunately it's no real help using terms you don't quite understand like "buck converter". Because one of those is a DC-DC converter and definitely NOT a mains power supply it just confuses matters.

Steve

Hi Steve,

Thanks for your interest and reply. Fair enough, that's what I really meant by not understanding enough to know what to describe but the feedback's good. I guess its what comes of never really talking to anyone about things and working stuff out alone by trial-and-error.

The power supply device is one of these (220VAC to 12VDC):
https://www.ebay.co.uk/itm/281773366865

I've also, however, tried it with one of these (220VAC to 5VDC) connected directly to the 5V pin of the Uno with identical outcomes:
https://www.ebay.co.uk/itm/401219875721

Regards

Chris

What is this "USB charger" you mention? Not something you plug into the mains supply?

Try printing out the LDR values in the Serial monitor, see what happens there (connect TX, RX and GND through a USB to TTL converter so you don't power the device from the computer).

It's strange that removing the pull-down resistor gives you any useful results. In case of the LDR you pull your pin up to Vcc, so you should get a reading of 1023 all the time, nothing else. In case of the button/switch, you leave your pin floating when it's off, and that normally gives rise to false positives. By the way, for the button you can wire it between GND and the pin, then enable the internal pull-up. Saves an external component. You could actually do the same for the LDR, as long as the mid-level resistance (based on your light levels - LDRs can go from some 100 Ohm to 10MOhm in resistance based on light level) is around 10-20k.

wvmarle,

The "USB charger" is something very similar to (possibly exactly the same as) this:
Samsung Galaxy UK Mains Charger

In respect of the use of the serial monitor to settle on a threshold for the LDR input, I did exactly as you describe in the very early stages of developing this with the Uno and a breadboard. Ambient light in the dark(ish) cupboard is always below 25 and a flash of the LED on the meter always produces something over 250 so I settled on 200 and this works perfectly in all the situations in my original post that I describe as "working perfectly".

In terms of "useful results" when removing the pull-downs - I don't get anything like useful results. I was trying to say that removing the pull-downs eliminates the very rapid flashing of the pin 13 LED (ie removes the very rapid apparent reads on pin A0) but it doesn't make the unit work OK. The only time it gives any useful results is when it is working perfectly. My point when I said that was that the problem seems to be related to the pin being connected to ground (ie pulled down) - when its not connected to ground there is no flashing of the LED - I hoped that fact might point someone toward the fault. Maybe it doesn't at all and I shouldn't have included it.

Minor update...

The issues go away completely when using this power supply:
AC-DC 5V 700mA 3.5W Power Supply Buck Converter Step Down Module UK

I'm increasingly of the opinion that this has to do with the quality of the power being supplied and I just happen to have a range of poor ones and two good ones - the one above and the Samsung Galaxy UK Mains Charger.

Does that stack up with anyone? If so, what is it about the "poor" ones that I should investigate in order to find a way to identify ones worth using and ones not worth using?

Thanks

Chris

Simple rule if its cheap, from eBay and connects to mains its probably not good/safe to use it.
I always go for genuine chargers from the big brands (avoid apple too many clones..) you can usually get em cheap at thrift stores or similar i pick up a few when i can and i never had any trouble with em so far.

Another tip on the ldr part of things i did something similar but i made it battery powered with a nrf module sending the data instead this: » Detecting a blinking LED » JeeLabs

Might be useful if you are thinking of making a version 2.0

Best of luck.

Time to invest in an oscilloscope so you can quickly track down the answers to your questions.

Paul