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 Arduino programming HELP NEEDED!!!
  • 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 Not Answered
  • Replies 44 replies
  • Subscribers 393 subscribers
  • Views 2878 views
  • Users 0 members are here
  • programming
  • rtc
  • code
  • arduino
  • relay
Related

Arduino programming HELP NEEDED!!!

Former Member
Former Member over 11 years ago

Hello! I am currently working on a project that requires a relay to be switched on at a certain time using a TinyRTC module. I understand that this should be fairly simple... but I can't get the code to play out correctly. Could someone PLEASE help me?

  • Sign in to reply
  • Cancel
Parents
  • kidiccurus
    0 kidiccurus over 11 years ago

    Can you please post your currant code and circuit diagram. Also, if it is literally just switching something on at a certain time, you may be better off with one of these:

    http://www.amazon.co.uk/Electronic-digital-mains-Socket-Display/dp/B002P7RF9A/ref=sr_1_1?ie=UTF8&qid=1408837264&sr=8-1&keywords=plug+timer

    I use and old mechanical version to automatically switch off my soldering iron every 20 minutes so I cant leave it on overnight.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to kidiccurus

    I don't really have any code yet. Everything I have started on is a bust. I am building a school bell timer to ring before and in-between classes. I have an arduino uno, a sainsmart 2-channel relay module, and a tinyrtc ic2. Currently the bell is operated by a button wich closes the connection between two wires. All I need to do is tell the arduino to switch the relay and close the connection for 'x' amount of time (about 2 seconds) at certain times throughout the day.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Former Member
    0 Former Member over 11 years ago in reply to kidiccurus

    I don't really have any code yet. Everything I have started on is a bust. I am building a school bell timer to ring before and in-between classes. I have an arduino uno, a sainsmart 2-channel relay module, and a tinyrtc ic2. Currently the bell is operated by a button wich closes the connection between two wires. All I need to do is tell the arduino to switch the relay and close the connection for 'x' amount of time (about 2 seconds) at certain times throughout the day.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    If someone could help me write up this code, that would be really great. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kidiccurus
    0 kidiccurus over 11 years ago in reply to Former Member

    This library should let me interface with the rtc. Please install it while I write some quick code.

    https://github.com/adafruit/RTClib

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to kidiccurus

    I have it installed.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kidiccurus
    0 kidiccurus over 11 years ago in reply to Former Member

    Great. this code will let you set the time. Just upload it with the rtc attached. It uses your computer clock so make sure that is set right.

    // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
    
    #include <Wire.h>
    #include "RTClib.h"
    
    RTC_DS1307 RTC;
    
    void setup () {
        Serial.begin(57600);
        Wire.begin();
        RTC.begin();
    
      if (! RTC.isrunning()) {
        Serial.println("RTC is NOT running!");
        // following line sets the RTC to the date & time this sketch was compiled
        RTC.adjust(DateTime(__DATE__, __TIME__));
      }
    
    }
    
    void loop () {
        DateTime now = RTC.now();
    
        Serial.print(now.year(), DEC);
        Serial.print('/');
        Serial.print(now.month(), DEC);
        Serial.print('/');
        Serial.print(now.day(), DEC);
        Serial.print(' ');
        Serial.print(now.hour(), DEC);
        Serial.print(':');
        Serial.print(now.minute(), DEC);
        Serial.print(':');
        Serial.print(now.second(), DEC);
        Serial.println();
    
        Serial.print(" since 1970 = ");
        Serial.print(now.unixtime());
        Serial.print("s = ");
        Serial.print(now.unixtime() / 86400L);
        Serial.println("d");
    
        // calculate a date which is 7 days and 30 seconds into the future
        DateTime future (now.unixtime() + 7 * 86400L + 30);
    
        Serial.print(" now + 7d + 30s: ");
        Serial.print(future.year(), DEC);
        Serial.print('/');
        Serial.print(future.month(), DEC);
        Serial.print('/');
        Serial.print(future.day(), DEC);
        Serial.print(' ');
        Serial.print(future.hour(), DEC);
        Serial.print(':');
        Serial.print(future.minute(), DEC);
        Serial.print(':');
        Serial.print(future.second(), DEC);
        Serial.println();
    
        Serial.println();
        delay(3000);
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kidiccurus
    0 kidiccurus over 11 years ago in reply to kidiccurus

    And this will be your actual code. Well almost. Read the comments (grey stuff) they explain how to correctly set it up. I needed about a minute after I saw your post to finish it but I uploaded the other code first so you didn't need to wait.

    //Bellclock v1.0 Created by kidiccurus I alow you to use this program on the condition that no more than 1 (one) Great British Pound (GB£) of revenue will be generated as a result of its use or sale. Just put that in there to stop you selling this to anyone
    //Conect your relay to digital pin 4
    //requires the libary you just installed
     
    #include <Wire.h>
    #include "RTClib.h"
    //Please est the lenght of time (in seconds) you would like the bell to ring for here in place of the number 2. Please do not set a decimal.
    int belltime = 2;
    int relay = 4;
    RTC_DS1307 RTC;
    void setup() {
      Wire.begin();
       RTC.begin();
       pinMode(relay,OUTPUT);
    }
    void loop() {
      
     //This module rings the bell. In the top row, the number 12 can be changed for the hour in 24 hour format and the number 0 can be changed for the minute (0-59).
     //Please copy and paste this module as many times as required 
      if (now.hour() == 12 && now.minute() == 0){
        digitalWrite(relay,HIGH);
        delay(belltime*1000);
        digitalWrite(relay,LOW);
         };
     //This is the end of the module if you are going to copy and paste
      
     //Place here
    //Or here
    //Or here 
      
    //Or here  
      
    //Or here
    
    //Et cetera et cetera
      
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kidiccurus
    0 kidiccurus over 11 years ago in reply to kidiccurus

    Just spotted a minor glitch. After the line "digitalWrite(relay,LOW); " In theBell ringing module,please add the line "delay(59000);" Whithout this the code will ring the bell for a full minute.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to kidiccurus

    On this first bit of code (for setting the clock), I have verified that my computer time is correct, but when I upload to the arduino, the time is not the same and is incorrect.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    Nevermind! I got the time set correctly.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kidiccurus
    0 kidiccurus over 11 years ago in reply to Former Member

    Great. Now make sure you update the code according to instructions. It would be embarrassing for my code to annoy the large amount of people who will be within range of the bell when it goes off. Any other requests for the code please don't hesitate to post here.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to kidiccurus

    Thank you for the code! I have one more request. I don't want to mess up the formatting in the code, so could you post it again with 10 bell timing modules? (10 different times to ring the bell)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify 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