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 3604 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
  • fmilburn
    0 fmilburn over 5 years ago

    Hello John,

     

    I will try and get you started....  Using threads is a somewhat advanced topic.  Here is another way to do it.  Relay 3 and 4 are simple as you probably already know.  Just turn them on and leave them that way with the GPIOZero library or whatever library you are using.

     

    Relay 1 and relay 2 are not that much more complicated.  Use the time.monotonic method which will keep steady time in seconds . Don't use sleep.  Start with something like this....

     

    import time

    start = time.monotonic()

    onRelay1 = 3

    offRelay1 = 5

    stopRelay2 = start + 5*60

     

    In your loop, continuously check to see if 5 minutes has passed using something like nowTime = time.montonic() compared to stopRelay2.  For relay 1, check if onRelay1 seconds have passed if it is on, and offRelay1 if it is off.

    Frank

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

    Hello John,

     

    I will try and get you started....  Using threads is a somewhat advanced topic.  Here is another way to do it.  Relay 3 and 4 are simple as you probably already know.  Just turn them on and leave them that way with the GPIOZero library or whatever library you are using.

     

    Relay 1 and relay 2 are not that much more complicated.  Use the time.monotonic method which will keep steady time in seconds . Don't use sleep.  Start with something like this....

     

    import time

    start = time.monotonic()

    onRelay1 = 3

    offRelay1 = 5

    stopRelay2 = start + 5*60

     

    In your loop, continuously check to see if 5 minutes has passed using something like nowTime = time.montonic() compared to stopRelay2.  For relay 1, check if onRelay1 seconds have passed if it is on, and offRelay1 if it is off.

    Frank

    • Cancel
    • Vote Up +6 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