Pages: [1] 2   Go Down
Author Topic: 9vac to 4n35 to ardunio  (Read 4183 times)
0 Members and 1 Guest are viewing this topic.
Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got the 9vac transformer connected to (4) 1N4001 in a diode bridge
 + out is connected to the 4n35 via a 1k to pin 1 (anode)
 - out is connected to the 4n35 pin 2 (cathode)

I've connected the 4n35 pin 5 (collector) to arduino d4
and 4n35 pin 4 (emitter) to arduino gnd

arduino is powered via the usb cable

all seams to work as I expect if I disconnect one leg - but if I pull out the transformer I believe I'm getting emf/noise back that locks up the arduino.

so am I using the wrong diodes? wrong photo-transistor?

looking to count on second times that it's powered.

Code:
const int pin = 4;

void setup(){
  pinMode(pin, INPUT);
  digitalWrite(pin,1);
  Serial.begin(115200);
  Serial.println("Start");
 
}

unsigned long millisLastMin = 0;
unsigned long millisLast = 0;
const int pinOn = LOW;
unsigned long onSeconds = 0;
unsigned long onSecondsLast = 0;
int thisSecondOn = 0;
int pin60[60];
int pin60Pos = 0;

void loop()
{
  int r = digitalRead(pin);
  if (thisSecondOn == 0) {
    if (r == pinOn) {
      Serial.print("+");
      thisSecondOn = 1;
      onSeconds++;
    }
  }
  if (millis() - millisLast > 1000) {
    millisLast = millis();
    Serial.print(thisSecondOn);
    thisSecondOn = 0;

  }
  if (millis() - millisLastMin > 60000){
    millisLastMin = millis();
    Serial.println("x");
    pin60[pin60Pos++] = (onSeconds - onSecondsLast);
   
    int pin60Sum = 0;
    for (int x=0;x<60;x++)
    {
      pin60Sum +=pin60[x];
    }
    if (pin60Pos > 59) pin60Pos = 0;
    onSecondsLast = onSeconds;
   
    Serial.print("[Hh]");
    Serial.print(pin60Sum);
    Serial.print("[Ht]");
    Serial.println(onSeconds);
  }
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
out is connected to the 4n35
What is a 4n35?
Logged

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

optocoupler/phototransistor http://www.vishay.com/docs/83717/83717.pdf
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27386
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Clever way to detect DC power supply.

Try turning on the internal pullup resister on pin 4 so the output of the opto has something to pull low, and will be read as high when the opto is off.
Once you put some load on the output of that diode bridge you will start getting 120Hz ripple also - so put 100nF cap and larger (uF) cap across the output as well.


* Power_detecter.jpg (35.75 KB, 960x720 - viewed 147 times.)
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Montreal
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2602
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you trying to do a digital clock bases on main 60 Hz, than I'd suggest make a research first on a google, there are a millions projects and discussions that would help you to start.
http://www.google.ca/search?client=opera&rls=en&q=arduino+clock+using+main+60+Hz&sourceid=opera&ie=utf-8&oe=utf-8&channel=suggest
 Your idea to count with millisecond has a serious flow, if loop take > 10 msec (very likely with a lot of Serial.print) you loosing a ticks.
Better to implement AttachInterrupt , and by the way 4 diodes bridge outputs 120 Hz.
Logged

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the suggestions

I wasn't very clear - I'm not looking to measure the Hz cycle - I'm just looking to see if the power is on during one second as I want to record the number of seconds it's on during a 5 minute period. 

Most of my Serial.prints are to figure out if my code (I'm really good at causing buffer overruns) was the issue or if fly-back noise is causing the arduino to lockup - so I understand that serial output can cause you to miss events.

I'll try and add some cap's.

I thought that my "digitalWrite(pin,1)" pulls sets the internal pullup resister so I'll look to see what's needed to do that.

I first wired it up with just one diode reverse the led - and that was giving me what I wanted until I would plug and unplug the 9vac transformer and the arduino would then lock up - I'm guessing from fly-back/emf noise. I then when to the bridge to see if that corrected the issue but it didn't.

At some point I'm actually looking to use the circuit at a 18vac (hvac) - But I figured I would start with 9volts first cause that's the transformer that I had.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27386
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

digitalWrite(pin,1)

Maybe it would - I was looking for digitalWrite(pin, HIGH); and didn't see the statement at all.
Maybe add an external pullup then.

Also, try putting this section of code before setup, might have an impact. As is, its not it setup, its not in loop, its just kinda floating out there, which is expected before setup, I don't know about after.
Code:
unsigned long millisLastMin = 0;
unsigned long millisLast = 0;
const int pinOn = LOW;
unsigned long onSeconds = 0;
unsigned long onSecondsLast = 0;
int thisSecondOn = 0;
int pin60[60];
int pin60Pos = 0;
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my cap's are in the basement so they will have to wait until later - but I moved declarations above setup and used const HIGH.

Same result.

Here's my serial output - as I expect until it locks up- the zero's are when I pull one leg of the 9vac output from the circuit - 1's are if line has power in that second.

It's only when I pull the transformer from the wall (i.e. 110vac), reinsert it, pull it out, reinsert that it locks up - so only when I'm doing that does it lock up.

Start
00000000000000+1+1+1+1+1+100+1+100+1+1+100+1+1+100+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+x
[Hh]38[Ht]38
1+1+1+1+10

another run:

Start
+1+1+1+1+1+1+1+1+1+1000+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+x
[Hh]57[Ht]57
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+x
[Hh]117[Ht]117
1+1+1+1+1+1+1+1+1+1+1000000000000000000000000000+1+1+1+1+1+1+1+1+1+1+1+10000000000x
[Hh]139[Ht]139
000000000000000000000000000000000000000000000000000000000000x
[Hh]139[Ht]139
000000000000000+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+x
[Hh]185[Ht]185
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+x
[Hh]245[Ht]245
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+10000000000000000000+1+1000000000+1+100+10+100+10000x
[Hh]267[Ht]267
000
Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2602
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hope, it's not only me, who don't have a clue what your code doing.
Why not :
setup:
unsigned long millisLast = millis();

loop:
if ( digitalRead(pin) ){
 long time = millis() - millisLast;
    Serial.print("minutes: ");
    Serial.print(time/60000);
    Serial.print("seconds: ");
    Serial.println(%(time/1000);
}

//not tested
Logged

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok added some comments to my code -
I think my issue is hardware and not software - but I'm often wrong

Code:
const int pin = 4;
unsigned long millisLastMin = 0;
unsigned long millisLast = 0;
const int pinOn = LOW;
unsigned long onSeconds = 0;
unsigned long onSecondsLast = 0;
int thisSecondOn = 0;
int pin60[60];
int pin60Pos = 0;

void setup(){
  pinMode(pin, INPUT);
  digitalWrite(pin,HIGH);
  Serial.begin(115200);
  Serial.println("Start");
 
}


void loop()
{
  int r = digitalRead(pin);
//if hasn't been on in this second check if on now
  if (thisSecondOn == 0) {
    if (r == pinOn) {  //do we have power
      Serial.print("+"); //debug comment only
      thisSecondOn = 1; //got power this second dont need to check again until next second
      onSeconds++; //add to power seconds on
    }
  }
//new second so reset so that we check during this second time period
  if (millis() - millisLast > 1000) {
    millisLast = millis();
    Serial.print(thisSecondOn); //debug comment only
    thisSecondOn = 0;

  }

//after a minute save our data
//byte for pin60[] would be better then int as should never be more then 60 for seconds in a minute - changed to int to make sure //not some overflow issue

  if (millis() - millisLastMin > 60000){
    millisLastMin = millis();
    Serial.println("x"); //debug comment

    //pin60Pos is our minutes in the hour
    pin60[pin60Pos++] = (onSeconds - onSecondsLast); //save our seconds for this minute into an array
   
// sum our hour
    int pin60Sum = 0;
    for (int x=0;x<60;x++)
    {
      pin60Sum +=pin60[x];
    }
    if (pin60Pos > 59) pin60Pos = 0;
    onSecondsLast = onSeconds;
   
    //output seconds on in the hour [Hh]
    //output total seconds on [Ht]

    Serial.print("[Hh]");
    Serial.print(pin60Sum);
    Serial.print("[Ht]");
    Serial.println(onSeconds);
  }
}

Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27386
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Got a multimeter? See if pin 4 is really going Hi & Lo, and to what level.
The state of the input pin is not gonna crash your code.

What happens when pin4 is not connected? Can you simulate the opto with a simple switch & see if does the same thing?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so I plugged the transformer into one of those cheap strip surge protectors - I used the switch on the surge protector to switch the transformer on and off - no issues.

but if I unplug the transformer from the strip with the power on it locks up

on the pins 4/5 on the 4n35 (to the arduino) - power off get 19MΩ and power on get .4kΩ  readings with arduino  pins not connected

with arduino pins connected get - power off .37KΩ and 5volts and power on 1.24KΩ and .5volts readings
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27386
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it just me, or have all the files attached to posts disappeared?  Not even an X in a box to say invalid links or anything?

You are wired up like this?
I can see no reason for the arduino to be affected by the state of pin 4. You have only the arduino connected to ground & +5V, correct?
All the stuff on the left side of the opto is not connected to anything?

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your showing a 4N25 - and I'm using a 4N35 is there much of a difference?

correct the opto input side is not connected to the output (arduino) side in anyway.

I also plugged in a different 4N35 and also tried a 4N33

arduino side is
          +5v
base - 1kΩ - Digital Pin 4

like I said it works great except if I unplug the transformer multiple times - I get my different on/off states (shown below as 1 and 0) if I remove one of the 10vac lines or use a switch to power off the transformer

11111111111111000000001111111000111111111100011110001111
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27386
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

4N25, no, I was just using that as a generic optoisolator, same with the other parts.

What do you mean "base - 1kΩ - Digital Pin 4"
That is not correct; the collector goes to Pin 4.  The base is not connected.

Try putting a load resister in parallel with the filter caps, something to bleed them down & make the input LED turn off when there is no power.
Like a 1K.

I don't have an explanation for the power switching on/off causes issues with your code.
Maybe try something simpler to start - just turn on the D13 LED whenever you see pin4 high, and off when low. If that will not work stable, then you have got some other basic problem.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1] 2   Go Up
Jump to: