element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Help. I need the reverse of zero crossing
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 7 replies
  • Answers 4 answers
  • Subscribers 391 subscribers
  • Views 1110 views
  • Users 0 members are here
  • inrush
  • programing
  • arduino
Related

Help. I need the reverse of zero crossing

phoenixcomm
phoenixcomm over 6 years ago

Ok, I need some hunk of code to detect the peek not the zero crossing. ( this is the solution).

Problem: Toroid based power supply have inrush current problems. As when you turn on the power switch,

it is a random chance where on the sine wave it will be.

You can not start this a zero crossing as that is the Imax and will blow your fuse. image

This solution must be only a primary side problem. I was thinking of a relay but a small triac is much faster.

Thanks

Cris

  • Sign in to reply
  • Cancel

Top Replies

  • michaelkellett
    michaelkellett over 6 years ago +3 suggested
    Why do you need code - the simplest trick is to use an NTC current limiter. These people have a lovely web site: https://www.ametherm.com/ If you have to do it in code it might be easier to detect zero…
  • abrain
    abrain over 6 years ago in reply to phoenixcomm +3 verified
    Yes, sorry Cris, I don’t think I explained it very well. If you’re measuring the voltage (which I think you must be to find the zero crossing) then work out how much that voltage changed from the last…
  • phoenixcomm
    phoenixcomm over 6 years ago in reply to michaelkellett +1 suggested
    I have seen current limiters like Ametherms before. But the problem is that I have three toroidal power transformers, and for the first 20ms or so they draw I max so the idea is to turn them on one at…
  • michaelkellett
    0 michaelkellett over 6 years ago

    Why do you need code - the simplest trick is to use an NTC current limiter. These people have a lovely web site:

     

    https://www.ametherm.com/

     

    If you have to do it in code it might be easier to detect zero crossings and then add a suitable time (phase) delay to locate the next peak.

     

    MK

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • phoenixcomm
    0 phoenixcomm over 6 years ago in reply to michaelkellett

    I have seen current limiters like Ametherms before. But the problem is that I have three toroidal power transformers, and for the first 20ms or so they draw Imax so the idea is to turn them on one at a time about one second apart. I like your idea about using a delay from zero crossing.

    Since we know that (T=1/60) 1/60Hz = 0.0166666667 then by using T/2 = 0.00833333335 now I will have to check this on my scope and most likely fudge the result as I really want this to fire just at the start of the bottom of the peak curve ie just before the top. and on the way down.

     

    thanks for the help

    Great answer!

    Cris H.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • abrain
    0 abrain over 6 years ago

    Hi Cris, if you’re sampling fast enough, and measuring the input voltage rather than doing some simple thresholding, then why not actually base it on what you’re interested in? Measure the voltage, quickly work out the change from last time you sampled, and as that rate of change comes down to a nice low value, switch on?

     

    That way you avoid dodgy time delays being affected by changes in frequency, etc, but you may have the issue of a working out the change on a noisy signal, depending on how you’re sampling circuit is filtered.

     

    A

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • phoenixcomm
    0 phoenixcomm over 6 years ago in reply to abrain

    You miss understand, I need just at the peek to fire off some Crydom SS Relays HD6090 (90amp   overkill image)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • abrain
    0 abrain over 6 years ago in reply to phoenixcomm

    Yes, sorry Cris, I don’t think I explained it very well.

     

    If you’re measuring the voltage (which I think you must be to find the zero crossing) then work out how much that voltage changed from the last sample you took, that change is smallest when you’re at the peak of the voltage.

     

    You’ll need to take the absolute value of the difference between the two readings, to account for negative and positive going peaks, but it will let you fire the relay at exactly the right time,

     

    I’ll see if I can sketch something out tomorrow to explain it better.

     

    A

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • phoenixcomm
    0 phoenixcomm over 6 years ago in reply to abrain

    Abrain, I have been working with C for well decades (1980) but I really hate the Arduino's IDE, personally, it sucks. I use eclipse. I haven't done too much with the analog side of the house except for some synchro stuff. For the most part, I use them for control. They really have no guts. like arm cortex stuff (32bit).  And the TI Launchpads are 10-20 bucks and load of IO. and well-done libraries.

    not like Arduino. Like we have a print-line function but not printf( " string" arg1,.... ). Using there IDE makes me brain dead. They are trying to stop good programming techniques by using setup() and loop() instead of main() which could call your init stuff, and then fall into a do{ }while( TRUE == some

    variable );

    At Trenton State, I used to teach and help with programming skills and they where pushing Pascal at the time. I used to say "The C Programming Language is akin to playing Russian Roulette.  Where the Wheel is gun which is your program. The little ball is the bullets. And of course, you are holding the gun to your head. and between the OS and your errors is what pulls the trigger"  You have to remember that this was K&R and no IDE on a pcXT.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • abrain
    0 abrain over 6 years ago

    Hi Cris, don't these things always turn out more interesting than you expect when you have a look at them?!

     

    Here's what I've come up with....

     

    My idea was basically to sample the voltage quite quickly (I think you're looking at 60Hz mains), so we get a good number of readings, and work out the difference between the latest reading and the previous one, effectively differentiating the signal. For a sine wave inout, that would give us something like the samples in the graph below (assuming you've got your input scaled nicely in the middle of the Arduino's 5V range).

     

    image

    From the orange dots showing the delta, you can then see that you're effectively after the zero crossing of the delta signal to get at the peak.

     

    When I came to try it, I got quite a lot of noisy triggering when there was no signal there, so I've added an extra check to make sure the ADC reading is above 800 before triggering, which is about a 3.9V input signal.

     

    The (moderately) interesting bit is that to get some good sampling, I set up an interrupt routine using Timer1, so we've got 2kHz sampling out of an Arduino! It's based on some other code I found on a forum some time ago, hopefully it'll be of use!

     

    // Interrupt-driven ADC sampling from Jack Christensen 13 Jun 2013
    // http://forum.arduino.cc/index.php?topic=171226.msg1276869#msg1276869
    
    
    #define BAUD_RATE 9600
    #define ADC_THRESHOLD 800      // if the actual voltage isn't above this, don't trigger
    
    
    int timerCount = 0;            // use to flash the LED at a sensible 2Hz rate
    volatile int oldAdcVal;        // keep the last reading so we can calculate a delta
    volatile int lastDelta;        // keep the old delta, so we can look for a zero crossing in that
    boolean justPeaked = false;    // flag to our main loop when we've just reached a peak
    
    
    void setup(void)
    {
        delay(1000);
        Serial.begin(BAUD_RATE);
        Serial.println("Setup...");
    
    
        //set up the timer
        TCCR1B = 0;                //stop the timer
        TCCR1A = 0;
        TIFR1 = 0xFF;              //ensure all interrupt flags are cleared
        OCR1A = 999;               //timer runs at 16MHz / 1000 / 8 (prescaler) = 2000Hz (500us between samples)
        OCR1B = 999;
        cli();
        TCNT1 = 0;                 //clear the timer
        TIMSK1 = _BV(OCIE1B);      //enable timer interrupts
        sei();
        TCCR1B = _BV(WGM12) | _BV(CS11);    //start the timer, ctc mode, prescaler 8
    
    
        //set up the adc
        ADMUX = _BV(REFS0);                                //use AVcc as reference
        ADCSRA  = _BV(ADEN)  | _BV(ADATE) | _BV(ADIE);     //enable ADC, auto trigger, interrupt when conversion complete
        ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);    //ADC prescaler: divide by 128
        ADCSRB = _BV(ADTS2) | _BV(ADTS0);                  //trigger ADC on Timer/Counter1 Compare Match B
    
    
        pinMode(LED_BUILTIN, OUTPUT);
        Serial.println("Ready to go...");
    }
    
    
    void loop(void)
    {
      if ( justPeaked ) {
        Serial.println(micros());
        Serial.flush();
        justPeaked = false;
      }
    }
    
    
    ISR(ADC_vect)
    {
      int newAdcVal = ADC;
      int newDelta = newAdcVal - oldAdcVal;
      oldAdcVal = newAdcVal;  // save the new reading to use in the next delta sum
    
    
      // we make sure our actual reading is above a threshold to prevent
      // a lot of triggering from small signal noise
      if ( newAdcVal > ADC_THRESHOLD ) {
        // if we've had a zero crossing in our delta value,
        // we've just reached the peak of the waveform
        if ( ( lastDelta > 0 ) && ( newDelta < 0 ) ) {
          justPeaked = true;
        }
      }
      
      lastDelta = newDelta; // save the new delta value for next time
    }
    
    
    ISR(TIMER1_COMPB_vect)
    {
      // this is simply to toggle the LED, the triggering of the
      // ADC happens automatically
      timerCount++;
      if ( timerCount == 500 ) {
        digitalWrite(LED_BUILTIN, HIGH);
      }
      if ( timerCount == 1000 ) {
        digitalWrite(LED_BUILTIN, LOW);
        timerCount = 0;
      }
    }

     

    I hooked this up to my Arduino Micro, with the signal generator connected between pins GND and A5, with a 3Vpk-pk sine wave signal and 2.5Vdc offset, at 60Hz.

     

    So there you go - hopefully you can replace that serial output part in the loop() function with what you need to drive your relays, and you're done!

     

    A

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube