element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Writing threads into code for 4 channel relay on pi
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Raspberry Pi requires membership for participation - click to join
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 26 replies
  • Answers 9 answers
  • Subscribers 664 subscribers
  • Views 3584 views
  • Users 0 members are here
  • rasberry_pi_3_b_plus
Related

Writing threads into code for 4 channel relay on pi

john2674
john2674 over 5 years ago

Hi i am a super noob at all things PI, i have done a few simple projects. That said coding is really new to me. Ive read python for dummies and quite a few articles on the web so far, but my latest project im stuck.

Im trying to write a code to controll a 4 channel relay board with my pi. But i want my relays to turn off and on at different intervals independant of each other. So far i think i need to use threading commands, but im not sure how to write them? As an example of what im trying to accomplish.

Relay 1 on 3 seconds off 5 seconds repeating forever

Relay 2 on 5 minutes then off permanently

Relay 3 on permanently

Relay 4 on permanently

 

If anybody can point me in a direction where i can learn these code commands i will be super grateful!

Ive tried searching all over and cant find what i need. Im sure i just need to get better at searching though lol.

  • Sign in to reply
  • Cancel
Parents
  • phoenixcomm
    0 phoenixcomm over 5 years ago

    john2674 John you are going to have to set a timer.

    you are not clear about the order of precedence. Do you know who is one first?

     

    #include <stdio.h>

    #include <time.h>

    #define FOREVER if(;;);

     

    # define ON 1

    #define OFF 0

    int relay1 = OFF;

    int relay 2 OFF;

    int relay 3 = OFF;

    int relay 4 = OFF

    time_t seconds;

    relay1 = ON;

    now1 =time(&seconds)l;

    FOREVER{

         now // Stores time seconds

              if ( now1 =time(&seconds) ==  now + 3) { relay1=ON;}

         else if ( now2 =time(&seconds) ==  now + 5) {

              relay1=OFF;

              now1 = 0;

              now2 = 0;

              relay1 = 0n;

              now1 =time(&seconds)l; }

              if ( now3 =time(&seconds) >  ontime2 + 300) { relay1=OFF;}

           

              relay3 = ON; //set and forget

              relay4 = ON; //set and forget

    }

      

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

    Ahh sorry for the slow replies. i have been researching all things related to time.monotonic all day. As i said i am extremely new to programming. as such i dont even know the right questions to ask yet. this project is my first attempt to make a code that performs a task of my own choosing. I have followed along on a few simple tutorials in the past, but i have much to learn!

    as far as order of precedence.. I would say relay1 and relay 2 on first then relay 3 on 3 seconds then off 5 seconds. i would like relay 4 to come on close to the same time that relay 3 starts cycling and stay on for 5 minutes. then turn off.

    The project this will be going into is an experimental outdoor biomass burner. relays 1,2 will turn on the combustion and circulating fans. relay 3 will control an auger to feed the burn chamber. and relay 4 will control the ignitor. in the future i plan to expand with high medium and low feed rates and safety proof of fire and high limit switches. but this first program is just for working out the timings and testing.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • fmilburn
    0 fmilburn over 5 years ago in reply to john2674

    Cristina is writing in pseudo C code to show you structure and flow.  My example to get you started is Python.  time.monotonic() returns the time in seconds in an ever increasing way with no jumps backwards or forwards to account for leap years, daylight savings time, etc.  If you do a search for something like "multiple blinking LED at different intervals" you will find  C/C++ code that does a general case of what you are looking for.  For example:  https://forum.arduino.cc/index.php?topic=214664.0

     

    Be sure and keep your relay names consistent - you have changed behavior between the first and second post.  E.G. in first post relay 1 cycles on and off forever while in second post it is relay 3.  So I am not sure your order but try something like this (Python pseudocode).

    import time 
    start = time.monotonic() 
    onRelay1 = 3
    offRelay1 = 5
    changeRelay1 = start + onRelay1
    relay1On = True
    stopRelay2 = start + 5*60
    
    relay1(on)   # cycle forever
    relay2(on)   # stay on 5 minutes
    relay3(on)    # turn it on and leave it on
    relay4(on)    # turn it on and leave it on
    
    while(True):
         nowTime = time.monotonic()
         if (nowTime >= stopRelay2):    # turns it off after 5 minutes
              relay2(off)
         if (nowTime >= changeRelay1):    # cycle
              if (relay1On == True):
                   relay1(off)
                   relay1On = False
                   changeRelay1 = nowTime + offRelay1
              else:
                   relay1(on)
                   relay1On = True
                   changeRelay1 = nowTime + onRelay1
         # do other stuff as needed

    This is untested and it is almost a certainty that I have introduced syntax and other errors somewhere but this is the general idea in a straight forward but not very elegant manner. Try to get something running and if you are still having trouble post the Python script and describe the issue.

     

    Edit:  saw a couple of errors in the pseudo code and tried to fix some of it

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • john2674
    0 john2674 over 5 years ago in reply to fmilburn

    Firstly thank you both for all your patience with me!

    Yes i changed the order when christina mentioned that i need to specify order of precedence. I'll admit i was not 100 percent sure what that meant so i thought about how i wanted my relays to behave exactly and changed the order to match that more closely???

    As far as her code example I thought it looked a little strange at first image but when i started typing some of it into Thonny the built in helper told me it was c.

    Im almost to a point where i can look at examples of simple code and understand what is being accomplished in it. I have a long way to go to get to the point where i can just sit down and write it un-aided. But i've never been the type to give up on anything. Your code example looks really close to what i need by the way! I believe i just need to import my GPIO library and assign the pins first, then try it. I will definitely report back with the results!

    • Cancel
    • Vote Up +5 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • john2674
    0 john2674 over 5 years ago in reply to john2674

    well Here is where i am at so far the Thonny editor did not like the word on or off in parenthesis so i changed it to relay1.off() not sure if thats my mistake or not, but i no longer get any errors when i test run the script, but the relays just come on and thats all no other action is happening. i will post below what i have so far.

     

     

    from gpiozero import LED

    import time

    relay1 = LED(21)

    relay2 = LED(20)

    relay3 = LED(26)

    relay4 = LED(19)

     

     

    start = time.monotonic()  

    onRelay1 = 3 

    offRelay1 = 5 

    changeRelay1 = start + onRelay1 

    relay1On = True 

    stopRelay2 = start + 5*60

    relay1.on()  # cycle forever

    relay2.on()  # stay on 5 minutes

    relay3.on()  # turn it on and leave it on

    relay4.on()  # turn it on and leave it on

     

     

     

    while(True): 

         nowTime = time.monotonic() 

         if (nowTime >= stopRelay2):    # turns it off after 5 minutes 

              relay2.on() 

         if (nowTime >= changeRelay1):    # cycle 

              if (relay1On == True): 

                   relay1.on() 

                   relay1On = False 

                   changeRelay1 = nowTime + offRelay1 

              else: 

                   relay1.on() 

                   relay1On = True 

                   changeRelay1 = nowTime + onRelay1 

         # do other stuff as needed 

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

    fmilburn  Jhon pseudo not quite it is in ANSI C but I know that =time(&seconds) returns seconds. Im preaty sure it might work, but then after a long weekend in Real Estate Class my mind was clouded.  Im not a fan of Python as it looses the ';' at the end of the semantic line, but instead relies on you to indent your code. Lets put line numbers before and call it BASIC LOL

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • john2674
    0 john2674 over 5 years ago in reply to phoenixcomm

    I miss basic! took days to write the simplest of programs, but even my simple mind understood it! lol

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

    john2674 John when using C it is much easier to deal with it in a "REAL" IDE like Eclipse.  You can deal with an Arduino or any other CPU with it as well. And BTW you get real error messages not the vagary with the Arduino IDE. Here are some of my posts:

    Software Dev: Arduino sketch & eclipse on Linux -1.0

    Software Dev: Arduino sketch & eclipse on Linux -2.0

    Software Dev: Arduino sketch & eclipse on Linux - 3.0

    Back to College: Setting up Eclipse Demystified

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • phoenixcomm
    0 phoenixcomm over 5 years ago in reply to john2674

    john2674 John when using C it is much easier to deal with it in a "REAL" IDE like Eclipse.  You can deal with an Arduino or any other CPU with it as well. And BTW you get real error messages not the vagary with the Arduino IDE. Here are some of my posts:

    Software Dev: Arduino sketch & eclipse on Linux -1.0

    Software Dev: Arduino sketch & eclipse on Linux -2.0

    Software Dev: Arduino sketch & eclipse on Linux - 3.0

    Back to College: Setting up Eclipse Demystified

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
No Data
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