Identify AC/DC power supply using ATmega328p

Hi,

I have a project to control LED dimming.

Now i met a problem. My project requests, my PCB can work at AC or DC power supply, that is, if AC 0-230V supplying, it can work; if DC 24V, it can work too. I have only one input with using MCU, ATmega328P. And the output can implement LED dimming control depending on the input voltage value. (I select PWM-dimming control)

How to program the code in Arduino can implement it?

Thanks

My project requests,

Is this homework? If so I think you have misunderstood your project brief.

if AC 0-230V supplying,

This is silly, you get 230V AC (in the UK) or nothing.

if AC ~~0-~~230V supplying, it can work; if DC 24V, it can work too.

That is impossible on one input. You can have a 24V input that can be supplied from a bench supply or a wall wart.

And the output can implement LED dimming control depending on the input voltage value.

Where is this voltage coming from? It can not be the supply that powers your arduino. Normally we use a pot and the analogue input pin to measure the voltage and use that to adjust the PWM value.

Hi, buddy

It's a real Project! We made a Controller-A only working at AC power supply (160-230 V) to drive a HB-LED string (80 pc). The Controller-A was consist of BD & PFC & HV9910 & circuit elements (no arduino MCU) and could provide a constant DC voltage 260V to the string, that is, we can get a constant lighting?

We want to do a extension design (Controller-A & ATmega MCU to Controller-B) and make the Controller-B work at AC or DC power supply to one Input with a variable lighting LED (i.e., (power supply) 0 - max --> (LED) dark - full of lighting).

I think, ATmega can be used as a MCU to sample value from input voltages (power supply voltage), then by this the MCU can identify, there is Frequency or no Freq und how big is the input voltage. According to these infos MCU can switch the suited circuit for power supply type via signals. It's a fool idea or ....?

My problem is, the Boss order me using arduino, and i'm a beginner. I don't know how to program for identify-function / anyone can know the useful codes in arduino?

This is not an easy project. Having a single power input that could be either AC or DC, and cover a large voltage range, is tricky at best.

First off, we need more clarification of your project.

The input power you are interested in monitoring. Is this power going to be used to power the LED strip, or will that power be constant? How about the MCU? Will that be powered by it, or separate?

If you want a single power supply input to run the whole system, then you need to decide upon the actual voltage the system (including LED strip) will run at - the lowest voltage your input will accept will be ABOVE this value.

Then I would think about passing the input through a bridge rectifier, so you remove the concern of "is it AC or DC", and just work with DC between your lower voltage (24V) and your upper limit (around 300VDC from a 230V AC supply).

Then you would need something that can convert that huge range of DC into a single stable DC voltage at your target voltage. There must be some form of switching regulator that can operate of such a wide range of voltages and give say 24VDC out of it - I don't know of one off hand, but they must exist.

That would give you a stable 24V power supply off anything between about 25V and 230V AC or DC.

As for monitoring WHAT that incoming voltage is - well, that could be done with a carefully crafted voltage divider after the bridge rectifier going into a properly protected analogue input.

Hi majenko,

As i said this project wanna realize a constant brightness of LED change to a variable brightness with dimming control. MCU means ATmega328P we already selected. Our discussion considers only how to realize the Identify-function at the first step. Unfortunately, i cannot insert images, so i give you an example to explain.

Example: 1) original Product: If we provide AC power supply 230V, the lamp reaches 100% brightness. If we provide AC 185V, 100% too. If we provide DC power supply, it doesn't work.

2) new Product after improvement: If AC 230V, lamp keeps 100%. If AC 185V, lamp can keep 80% brightness. If AC 115V, keep 50% brightness ... Or if DC 24V provided, only 10% pieces of LED string work. Therefore i retain the circuit of the Original and design an additional circuit-DC wired 10% LED-piece of the Lamp. I build the two circuits together in a PCB and set an ATmega328P & some switching elements (MOSFET/Transistor) to control.

At the first step of the workflow our PCB has to identify the power supply type (AC or DC), that is, the ATmega328P is used to sample data from input. According to data the ATmega328P can turn on/off the switching elements to let only one suited circuit work.

Using Arduino can implement identifying frequency of Power Supply (no f --> DC; with f --> AC), yes or no? If say yes, i wanna know how to write the code (It's my question).

majenko: Then you would need something that can convert that huge range of DC into a single stable DC voltage at your target voltage. There must be some form of switching regulator that can operate of such a wide range of voltages and give say 24VDC out of it - I don't know of one off hand, but they must exist.

Most switching power supplies work with 120-240 volt input directly (think PC power supply). Many PC power supplies work this way (have no "115/230" switch).

Of course, the 24 volt input cannot go into the same spot as the AC line... probably it would be more like this:

AC 120/240 >-----[SWITCHER]------\ |------> 24 VDC to load DC 24 VDC >-------[DIODE]--------/

Using Arduino can implement identifying frequency of Power Supply (no f --> DC; with f --> AC), yes or no? If say yes, i wanna know how to write the code (It's my question).

Once you solved the AnySupplyPSU hardware problem, to run your Arduino, the rest is easy. Once you solved the hardware problem to build a (opto isolated (?)) signal showing your input signal as ( 0 / 50 / 60 Hz) pulses in an Arduino suitable level, the rest is very easy: Count those pulses during 1000 millis, if this count is 0 or 1 ( but your Arduino is running nevertheless ; ) , it must be DC, else it gives you the supply frequency.

Depending on your preference, you might want to use an interrupt to count the pulses or do it all inside loop(). Arduino is fast enough for both approaches. Give it a try and if necessary ask for help with your code.

Unfortunately, i cannot insert images,

Yes you can. Use the triangle that says additional options under the reply box.

1) original Product: If we provide AC power supply 230V, the lamp reaches 100% brightness. If we provide AC 185V, 100% too. If we provide DC power supply, it doesn't work

All very well but what voltage are you driving the LEDs at. Is this applied power powering the Arduino? Why use an Arduino at all other than the boss says so, I have read Dilbert, is the Arduino doing anything else apart from reading a pot and producing PWM? What sort of current do you need?

A universal AC / DC power supply is a very difficult thing to design and should only be undertaken by a skilled electronics engineer. If you are having to ask how, then it is probably beyond you.

Grumpy_Mike:

Unfortunately, i cannot insert images,

Yes you can. Use the triangle that says additional options under the reply box.

  1. original Product: If we provide AC power supply 230V, the lamp reaches 100% brightness. If we provide AC 185V, 100% too. If we provide DC power supply, it doesn’t work

All very well but what voltage are you driving the LEDs at. Is this applied power powering the Arduino? Why use an Arduino at all other than the boss says so, I have read Dilbert, is the Arduino doing anything else apart from reading a pot and producing PWM?
What sort of current do you need?

A universal AC / DC power supply is a very difficult thing to design and should only be undertaken by a skilled electronics engineer. If you are having to ask how, then it is probably beyond you.

“If you have to ask, you aren’t capable of understanding.”
_ – Me._

if AC 0-230V supplying, it can work;

I like to see a device working at AC 0v.

Hallo Michael_x,

vielen Dank für deine Antwort :)

Now i know two functions for that, one FreqCounter library, another pulseIn(). I have an idea about using pulseIn() to implement PSU-frequency-identify. If u read it, pls tell me it does work or doesn't.

The project met three kinds of the PSU' freq, 0 (DC), 50 , 60 Hz (AC). The code is following as:

int identify_pin = 7; unsigned long duration;

void setup() { pinMode(identify_pin, INPUT); }

void loop() { duration = pulseIn(identify_pin, HIGH); delay(20); if(duration == 0) { //Now is DC power supply. } else //AC PSU }

The project met three kinds of the PSU...

Yeah. But would the mcu get to execute that code, when powered by ac?

You are attempting a project without understanding the project.

The typical route of using ac/dc power supplies is to rectify the power input, regardless of whether it is ac or dc.

But none of that works at AC 0v.

In this forum, before I asked a question, I must explain you the entire complex project ?

I want to know only, how to program in arduino with using ATmega328P can identify AC power supply or DC power supply input.

If arduino can do, pls tell me the code. If cannot, select other chip.

Any helpful Info here?

Any helpful Info here?

Rather than trying to figure out ways to do it, focus on if it is possible to do it.

Bingo, it has always been my thought. But some one ( michael_x) says it can be done, so rather than arguing about being possible to do or what is project with?without understanding? let's talk about how to do implementation. pls check my code!

But some one ( michael_x) says it can be done,

It depends on what "it" is. Powering anything at AC 0v is certainly not possible.

Powering something at AC 230v is certainly possible.

Without knowing what "it" is, it is not possible to tell you what is and isn't possible.

pls check my code!

Until you have a plan, your code has no meaning.

It is not difficult for a mcu to detect what is powering itself. It is not possible for a mcu to be powered by AC 0v.

Now i know two functions for that, one FreqCounter library, another pulseIn(). I have an idea about using pulseIn() to implement PSU-frequency-identify. If u read it, pls tell me it does work or doesn’t.

Arduino is great for thousands of libraries with example code, and sometimes an example works to your satisfaction and solves exactly your problem. This is amazing, and times when you built everything from scratch are long gone, but normally you should at least understand what is going on inside a library or function like pulseIn.

In your case the problem with pulseIn is that it hangs until a pulse in the right direction happens.
You’d have to set a timeout, at least.
I never started understanding the FreqCounter library, as the problem is rather trivial in your case, once you managed to setup the hardware environment properly and run your ATmega with 0V, hehe :wink:

Just setup a loop without delays, which checks when 1000 millis have passed and count state changes of your identify_pin.
You don’t need anything but some subtractions, additions, digitalRead(), millis() and if statements
and a few intermediate static variables for the number of millis when you started, the number of state changes, and the state during the previous loop run, to detect a change.

Edit: You need the millis() function too, of course, and perhaps a few more bits, but the software part is trivial, believe me.

Dear Michael_x,

you are right. Just I have modified the code as following:

const int identify_pin = 7;
const int yellowLED_pin = 12;
const int redLED_pin = 11;
unsigned long duration = 200;

void setup()
{
pinMode(identify_pin, INPUT);
pinMode(yellowLED_pin, OUTPUT);
pinMode(redLED_pin, OUTPUT);
}

void loop()
{
digitalWrite(redLED_pin, LOW);
digitalWrite(yellowLED_pin, LOW);
delay(2000);

duration = pulseIn(identify_pin, HIGH, 20000);

if(duration <= 1)
{
digitalWrite(redLED_pin, HIGH);
digitalWrite(yellowLED_pin, LOW);
delay(2000);
}
else
{
digitalWrite(redLED_pin, LOW);
digitalWrite(yellowLED_pin, HIGH);
delay(2000);
}
}

If i give the arduino DC power supply 5V, the red LED can work. If i give it a PWM(50% @ 50Hz), the yellow one can work. Unfortunately, i have no AC PSU, so only use PWM to test. The test result can proof the code can solve my problem?

Because our PCB (ATmega328P) needs firstly to identify the PSU type(AC or DC) and then with the data it can do the next corresponding things, i define 20ms (max time value, a period of PSU with 50Hz >= period of PSU 60Hz) for timeout, that is, in one period can find, there is a pulse or no pulse.

Do you have any Suggestion to improve codes? Do I need to consider some what parameters or situation?

In PWM-Test, the code "unsigned long duration = 200;" is changed "unsigned long duration;".

dhenry, pls, can we be friends? I understand really the project. "AC 0V working" is bullshit, i know. sorry for wrong writing. If 0V, it's sure no work. We wanna set a mini limit of working AC PSU, but it's the late thing.

Anyway, thanks for your Info

dhenry, pls, can we be friends?

You will be the first then.