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 2923 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

    Yes I can do that image Thank you again!

    • 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

    Your welcome. Please make sure you test it. It is simple code but I don't want to make any large mistakes. Also, could you please mark my answer as correct so people know that you don't need any more help. Where is this going to be installed? It would be nice to know who I have helped plus it could could be good for a resume in the future.

    • 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 thought I got the time set, but it isn't wanting to work. It isnt setting the time from my computer.

    • 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

    // 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();
    
    rtc.adjust(DateTime(2014,MONTH,DAY,HOUR,MINUTE,0));
      }
    
    }
    
    void loop () {
    
    }

    Right, try this. Line 13, replace the month day hour and minute with the correct numerical values.

    • 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

    Im getting these errors when I try to upload


    Untitled_Project.ino:13:1: error: use of undeclared identifier 'rtc'

    rtc.adjust(DateTime(2014,8,23,21,18,0));

    ^
    Untitled_Project.ino:16:1: error: extraneous closing brace ('}')

    }

    ^

    • 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

    Change rtc to RTC (block capitals)
    and delete the curly brace directly underneath it. It is quite late where I am at the moment so I am making a few silly mistakes.

    • 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 the time set, but when I upload the bellring sketch I get this


    BellRing.ino:19:7: error: use of undeclared identifier 'now'

      if (now.hour() == 21 && now.minute() == 26){

      ^
    BellRing.ino:19:27: error: use of undeclared identifier 'now'

      if (now.hour() == 21 && now.minute() == 26){

      ^
    2 errors generated.

    • 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

    Add this line directly above the first bellring module. (line 17): DateTime now = RTC.now();

    • 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

    Okay. That worked. Also I forgot to mention that my relay module works different than most others. It switches OFF when power is supplied to the control pin. To fix that I would just have to switch the 'HIGH' and 'LOW' in your code, correct?

    • 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

    Yep, that is correct. I also recommend that you add the line "digitalWrite(relay,HIGH);"  between line 14 and 15. Without this the bell will ring non stop until the first alarm as the pin defaults as low. An active low relay seems quite dangerous to me. Here, if the control circuitry fails the bell will ring constantly, and in an oven this could start a fire. I recommend you add a suitable switch to override the relay to spare everyone's ears if it does go wrong.

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

    Yep, that is correct. I also recommend that you add the line "digitalWrite(relay,HIGH);"  between line 14 and 15. Without this the bell will ring non stop until the first alarm as the pin defaults as low. An active low relay seems quite dangerous to me. Here, if the control circuitry fails the bell will ring constantly, and in an oven this could start a fire. I recommend you add a suitable switch to override the relay to spare everyone's ears if it does go wrong.

    • 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 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
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