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 RTC combined with servo device
  • 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 8 replies
  • Subscribers 392 subscribers
  • Views 2228 views
  • Users 0 members are here
  • Design
Related

RTC combined with servo device

Former Member
Former Member over 10 years ago

Hello,

I have a project and part of it involve an RTC clock and a servo. see attachment below.

I already built the circuit but I am completely lost with the sketch.

The objective of the project is :

After every 3 hours the servo automatically rotate 60 degrees.

Hope you could help me in the sketch.

Thanks in advance!

 

image

Attachments:
image
  • Sign in to reply
  • Cancel

Top Replies

  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago +1
    Can you show us what you have so far Have your already tried getting the servo to work using the standard servo library, same question for the RTC What is the model of the servo, I would normally recommend…
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago in reply to Former Member +1
    You can make it change at what ever interval you like and as far as you like within its limits just write the program based on what info I provided and then share your results, and we can help further…
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    Can you show us what you have so far

     

    Have your already tried getting the servo to work using the standard servo library, same question for the RTC

     

    What is the model of the servo, I would normally recommend NOT connecting the motors to the regulated output of the Arduino Power Supply as it invariably causes issues. Power it separately if you can

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to Robert Peter Oakes

    Yes the servo works heres the code below:

     

    #include <Servo.h>

    Servo myservo;  // create servo object to control a servo

    int potpin = 0;  // analog pin used to connect the potentiometer

    int val;    // variable to read the value from the analog pin

    void setup()

    {

      myservo.attach(9);  // attaches the servo on pin 9 to the servo object

    }

    void loop()

    {

      val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)

      val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)

      myservo.write(val);                  // sets the servo position according to the scaled value

      delay(15);                           // waits for the servo to get there

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to Robert Peter Oakes

    the servo is the MG996R and the rtc 1307:

     

     

    #include <Wire.h>

    #include "RTClib.h"

     

     

    RTC_DS1307 rtc;

     

     

    void setup () {

      Serial.begin(57600);

    #ifdef AVR

      Wire.begin();

    #else

      Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due

    #endif

      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(F(__DATE__), F(__TIME__)));

        // This line sets the RTC with an explicit date & time, for example to set

        // January 21, 2014 at 3am you would call:

        // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

      }

    }

     

     

    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 midnight 1/1/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
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    So first things first (btw, you don't say what to do if and when the servo reaches 180deg or 360, do you stop, reset to zero, something else ?

     

    Ignore the RTC initially and simply setup an LONG variable to hold the current system time (mills() )

    every 60 seconds (60,000 mills) move the servo

     

    #define interval 60000

    #define changeAngle 60

    #define maxAngle 180

    #define minAngle 0

     

    long mytimer = 0;

    int myAngle = 0;

     

    setup()

    {

    ...

    mytimer = mills();

    }

    loop()

    {

    ...

    if (mills() - myvariable > interval)

         {

         myAngle += changeAngle

         if (myAngle >= maxAngle)  myAngle = minAngle; // protect servo and sanity image

         myservo.write(myAngle);

         mytimer = mills();                // setup timer for next loop

         }

    // now do other stuff

    }

     

    This is not tested , I simply put it from head to page as I thought of it so you may need to do some work on it but it shows you the basics of how to get what you want, now its up to you

    once you have this running, it will be a simple matter of changing the mills() to use the RTC and changing the values of the interval

     

    ... in setup and loop simply is where your other code may go, it is of course not valid in the real code

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to Robert Peter Oakes

    in fact yes, since a servo can rotate only 180 degrees after 180 degress i wanted it to return to zero.


    ok yes so do you think every 1 minutes it can change 60 degrees?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to Former Member

    You can make it change at what ever interval you like and as far as you like within its limits

     

    just write the program based on what info I provided and then share your results, and we can help further

     

    Peter

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jasminemparham
    0 jasminemparham over 8 years ago

    Did this code ever work?... because I am doing something similar and I just found out today that I should add an RTC because I do want my servo to function on set times of the day except instead of it would be 20 degrees then down -20 degrees. Originally I was going to convert all set hours into milli secs but that might mess up my order everyday for the hours i want it set on.Would you suggest getting the RTC?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jasminemparham
    0 jasminemparham over 8 years ago

    Robert Peter Oakes I am doing something similar and I just found out today that I should add an RTC because I do want my servo to function on set times of the day except instead of it would be 20 degrees then down -20 degrees at a set time of day. Originally I was going to convert all set hours into milli secs but that might mess up my order everyday for the hours i want it set on.Would you suggest getting the RTC too?

    • 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