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 2901 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
  • Former Member
    0 Former Member over 11 years ago in reply to kidiccurus

    I just tested it out but it was delayed by about a minute and the relay stayed on for almost two minutes

    • 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

    how long did you set the relay to go on for what number did you actually enter. As for the delay, your time may be a minute or so behind from when you where uploading the time. It probably took about a minute for the rtc to be programed with the time from when you entered it in the time setting program. Try setting the time a minute in the future when using the upload program to help compensate.

    • 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 set the belltiime as "2" and then changed the "(belltime*1000)" to just "(2000)" but it still does the same thing

    • 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

    my bad. I forgot to add the (delay59000) after the last relay command.

    • 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

    Try running the blink sketch with the relay connected to pin 13. If that works then I don't know what the problem is. If it fails, then I think the problem is your relay. Test and report back.

    • 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 got your sketch to work okay. the only problem im having is setting and keeping the time on the RTC module. If i set the time with a sketch that outputs to a serial monitor, the time resets back to what it was originally set to everytime the serial monitor is opened. The only way i can get the time to set is by not outputting to a serial monitor, but then i cant see whats going on and im blinded.

    • 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

    But it is working, right? Do you have a battery installed in the RTC. If not, install one. Other than that I don't know what the problem is.

    • 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

    there is a network plug right beside where this will be located. Would it be better to have it get the time from the internet? but then again if the internet goes down the bell wouldnt work.

    • 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

    Probably not, this would be way to complex. I am still unsure, is it working? If so, why do you need a serial monitor?

    • 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

    yes, your code works. but i just tried to check/change the time on the rtc and it quit working. The only reason i need the serial monitor is to check and see what the time on the rtc is.

    • 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

    yes, your code works. but i just tried to check/change the time on the rtc and it quit working. The only reason i need the serial monitor is to check and see what the time on the rtc is.

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

    This code should do the job. You can set a time in the main block where you would normally set the bell to go off and it will turn on the boards built in led at this time. Not as good as serial but an acceptable workaround. I need to sign off now so I won't be able to provide further help until tomorrow.

    //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"
    int relay = 13;
    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);
      };
     //This is the end of the module if you are going to copy and paste
      
    }

    • 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 dont believe there is anything different about this code??

    • 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

    I removed the part to switch off the pin after 2 seconds and changed it to activating pin 13 and the internal LED  for easy checking. Otherwise, yes it is the same.

    • 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

    This is the code I have used to correctly set and keep the time on the RTC module. Everything else I have used makes the RTC's time reset if the serial monitor is opened. However the time still resets on loss of arduino power, as if the coin cell battery is not installed. If you look at the code attached, I also have it monitoring the battery voltage, so I know that the battery is correctly installed and charged. I just don't know why it is not working correctly. Maybe I just don't know what im doing, but I have ordered another RTC and it will be delivered tonight. I'm hoping my first one is just faulty. The only thing different I have noticed about THIS code is the library included at the beginning. "RTC_DS1307" as opposed to the others, "rtc_millis".

     

     

     

     

    //Arduino with Tiny RTC I2C http://zygzax.com/?lang=en

    #include <Wire.h>

    #include "RTClib.h"

    RTC_DS1307 RTC;

     

    void setup () {

      //Initialize the serial port, wire library and RTC module

        Serial.begin(9600);

        Wire.begin();

        RTC.begin();

        //If we remove the comment from the following line, we will set up the module time and date with the computer one

        //RTC.adjust(DateTime(__DATE__, __TIME__));

    }

     

    void loop () {

      int sensorValue = analogRead(A0);

      float voltage = sensorValue * (5.0 / 1023.0);

      Serial.println(voltage);

        DateTime now = RTC.now();

        //We print the day

        Serial.print(now.day(), DEC);

        Serial.print('/');

        //We print the month

        Serial.print(now.month(), DEC);

        Serial.print('/');

        //We print the year

        Serial.print(now.year(), DEC);

        Serial.print(' ');

        //We print the hour

        Serial.print(now.hour(), DEC);

        Serial.print(':');

        //We print the minutes

        Serial.print(now.minute(), DEC);

        Serial.print(':');

        //We print the seconds

        Serial.print(now.second(), DEC);

        Serial.println();

        //We check the time and sent through the serial port every 3s

        delay(1000);

    }

    • 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

    I recommend that you just set the time with a bit of trial and error.

    • 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 figured it out! The library i've been using to set the time on my RTC doesn't work very well. I switched to using another DS1307 library and the Time library from the arduino website. Everything works! I tested the code to ring the bell by setting it to go off three times, once every three minutes. I uploaded it, watched it go off once, unplugged it for a minute, plugged it back in, and it still had the correct time! however I left it unplugged overnight (about 8 hrs) and plugged it the next morning to find that it was ticking around 15 seconds behind the correct time. I have no idea why it would do this or how to fix it, so Im just assuming that the coin cell battery needs time to fully charge (but it doesnt take that much power to keep memory of time, right?).

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

    Mason,

              the error you see in the time shown on your PC versus the 1307RTC is probably due to the inherent accuracy of the 1307.  I had a similar issue and I solved it by monitoring the RTC time against my watch and noted that the error is progressive and predictable on the RTC.  I.E. the RTC constantly loses 10 seconds a day.  I therefore simply added a correction factor into my program to correct the RTC time.  In the example I would add 10 seconds every 24 hours to compensate for the time 'lost'.  That was good enough for that particular application.

     

    However, I really wanted to get a more accurate RTC to use in my projects and I found the DS3231 modules.  These are considerably more accurate than the DS1307 and are I2C also.  I bought some modules from www.banggood.com for £1.42 and they take about 3 weeks to ship to the UK.  These modules also have a AT24C32 Eeprom on board as well as a back up battery.  There is also an Arduino library for them.  Having used the DS3231 I would not go back to 1307.

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

    Glad it is working. Sorry I had to leave but it was around 3 in the morning when I finally signed off. It is nice to see my code acctualy help someone.

    • 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