TomGeorge:
Hi,
What is the application, where your signal can be AC and or DC?
What is the source?
Why do you need to know if its AC or DC?
Thanks.. Tom..
hi, never mind what application, i guess this will further just impose more questions than solution and longer thread. lets pretend we have such application with ac and dc with the signal transition as i posted in my first thread post.
cattledog solution is correct, i made mistake in posting above that the led blinked when reading ac...i don't know what happened then but now it is working.
Will the signal be pure DC then pure AC (with offset for arduino analog input)
If pure AC will it be always oscillating around 2.5Vdc
Will the frequency vary or is it always the same?
Will the DC always be one value or unpredictable?
Will the DC vary while it is DC? (Thats is drift or change over many seconds)
Will the AC amplitude always be one value or unpredictable?
Is this a school/college/university project?
You need to define what you call AC and DC, and how quickly you want the difference to be signaled.
AC can be a power sine-wave of a complicated waveform of many frequencies and harmonics passing through a zero point defining a change in current flow.
DC can be a forever continuous constant voltage or a changing voltage whose current is always in one direction used to control a variable device.
Can you draw a diagram of a typical signal input that we need to analyse?
Just a picture of a hand drawn graph will suffice.
Please answer these questions in point form.
Please tell us the application, you want our help yet you will not supply the basic information.
If it is a secret, employ someone to do your research and get them to sign a disclosure document.
If you use a capacitor type circuit , it will pass AC only, so you will know if its AC, if not AC then it must be DC.
Depending on your signal levels, it may be better to work with analogRead() instead of digitalRead() to determiine the levels. Hers's the sequential digital reading example modified to analog.
const byte myPin = A0;
const byte LED0 = 12;
byte inputState0 = 0;
int threshold = 800; //high value of analogRead
byte analogValue;
const unsigned int interval = 1000;
unsigned long lastTime = 0;
void setup()
{
pinMode(LED0, OUTPUT);
pinMode(myPin,INPUT);
}
void loop()
{
if (millis() - lastTime >= interval)
{
lastTime += interval;
if(analogRead(myPin) >= threshold)
{
analogValue = HIGH;
}
else
{
analogValue = LOW;
}
//use bitshift and bit wise "or" to combine the readings
//this is equivalent to inputState = inputState*2 + digitalRead(myPin)
//it builds a multi bit bit number from the sequential digital reads
//if 8 sequential readings are 1 it is >3v DC
inputState0 = inputState0 <<1 | analogValue;
if (inputState0 == 0xFF) { //all ones
digitalWrite(LED0, HIGH);
}
else {
digitalWrite(LED0, LOW);
}
}
}
cattledog:
I'm please you had success with this method.
Depending on your signal levels, it may be better to work with analogRead() instead of digitalRead() to determiine the levels. Hers's the sequential digital reading example modified to analog.
const byte myPin = A0;
const byte LED0 = 12;
byte inputState0 = 0;
int threshold = 800; //high value of analogRead
byte analogValue;
const unsigned int interval = 1000;
unsigned long lastTime = 0;
//use bitshift and bit wise "or" to combine the readings
//this is equivalent to inputState = inputState*2 + digitalRead(myPin)
//it builds a multi bit bit number from the sequential digital reads
//if 8 sequential readings are 1 it is >3v DC
the last code(not this one), i tested in hardware and I get blinking instead of just on or off. i don't understand, in simulation it works fine. I tested with different intervals.
Connect the signal to the input pin through a capacitor with a bleeder resistor from pin to GND, if signal is "AC" the cap will charge and read > 0, if DC the bleeder will discharge the cap and read close to 0.
BTW, what is the freq?
756E6C:
Connect the signal to the input pin through a capacitor with a bleeder resistor from pin to GND, if signal is "AC" the cap will charge and read > 0, if DC the bleeder will discharge the cap and read close to 0.
BTW, what is the freq?
i appreciate your trick but it has same problem like the timer one, once there is ac signal, there is reading of it and changes the digitalWrite value creating LED blinking effect. what i wanted to keep the LED ON until there is ac signal and LED off when there is dc signal.
If you get eight analog readings over threshold or eight digital reads of HIGH on an analog signal when it is greater than 3v you will see the led light up. Certainly, you can raise the threshold, but a low frequency AC signal to 5v might be hard to handle.
I'm not sure if this method can be made completely generic for any ac frequency and amplitude. What is the AC signal? You should be able to tune the reading interval to pick up a low value.
On thing to try if you can't tune the 8 reading interval for the frequency and amplitude of the AC signal is to require more sequential readings to be 1 in order to indicate DC.
Type inputState0 as an integer and compare to 0xFFFF for 16 sequential readings to DC or type it as a long for 32 readings and test against 0xFFFFFFFF
The more sequential reading required, the less quick you will be to detect when AC switches to DC. DC to AC will be detected with one reading not at 1.
You need to define what you call AC and DC, and how quickly you want the difference to be signaled.
AC can be a power sine-wave of a complicated waveform of many frequencies and harmonics passing through a zero point defining a change in current flow.
DC can be a forever continuous constant voltage or a changing voltage whose current is always in one direction used to control a variable device.
Can you draw a diagram of a typical signal input that we need to analyse, indicating where you want the LED to show AC?
Just a picture of a hand drawn graph will suffice.
What if you went looking for DC and reversed the output logic?
cattledog:
If you get eight analog readings over threshold or eight digital reads of HIGH on an analog signal when it is greater than 3v you will see the led light up. Certainly, you can raise the threshold, but a low frequency AC signal to 5v might be hard to handle.
I'm not sure if this method can be made completely generic for any ac frequency and amplitude. What is the AC signal? You should be able to tune the reading interval to pick up a low value.
On thing to try if you can't tune the 8 reading interval for the frequency and amplitude of the AC signal is to require more sequential readings to be 1 in order to indicate DC.
Type inputState0 as an integer and compare to 0xFFFF for 16 sequential readings to DC or type it as a long for 32 readings and test against 0xFFFFFFFF
The more sequential reading required, the less quick you will be to detect when AC switches to DC. DC to AC will be detected with one reading not at 1.
it isn't that the program does not detect ac or dc, it detects and distinguishes ac and dc signal. the problem is once it detects, the LED does not remain high or low, instead for ac signal the led blinks which i want to avoid.
the ac signal is low frequency signal but other times the signal is also audio signal
TomGeorge:
What is the application, where your signal can be AC and or DC?
TomGeorge:
You need to define what you call AC and DC....
OP's definition of AC is just DC of varying positive voltage, and that DC is a constant positive voltage. S/he's not actually looking for a reversal of current / polarity.
nyphot:
OP's definition of AC is just DC of varying positive voltage, and that DC is a constant positive voltage. S/he's not actually looking for a reversal of current / polarity.
thats partly right, ac signal(pure tone) is riding on DC.
ardravi:
thats partly right, ac signal(pure tone) is riding on DC.
You say "partly right", which means it's partly wrong. What's wrong with what I said? Everything you said so far is that the voltage never goes below 0.
So humour me and answer this: does it or does it not, ever reverse polarity, ie is the -ve side of the AC larger than the DC to take it below zero.
it detects and distinguishes ac and dc signal. the problem is once it detects, the LED does not remain high or low, instead for ac signal the led blinks which i want to avoid.
I believe that this is because when there is an ac signal you are getting enough high readings in a row for the program to identify it as dc for a brief period of time. If the ac signal is going above and below the threshold value for a high, you have to time the interval and the number of readings to make sure you pick up some lows.
That is why I suggested trying to extend then number of required readings to say its dc.
nyphot:
You say "partly right", which means it's partly wrong. What's wrong with what I said? Everything you said so far is that the voltage never goes below 0.
So humour me and answer this: does it or does it not, ever reverse polarity, ie is the -ve side of the AC larger than the DC to take it below zero.
by saying repeatedly "OP's definition of AC" it sounds like you are trying to humoring me and not the other way around, which is wrong.
i never claimed as definition, rather, saying "OP's AC signal is DC of varying positive voltage...." is correct way of saying it as you did in the previous post and that is correct.
I mentioned in my first post or so that there is no negative excursion of my signal, the signals behavior were clearly mentioned.