Servo Controller Questions

Hello. I am very new to Arduino. I have been working with all of the sample programs. However, i am trying to develop a servo controller for an automation process i am working on, and was looking for help designing it. Currently i have an Arduino Mega 2560 that i have been using.

I have 7 servos that i want to use to adjust dampers in my home. basically they will just need to be 0 degrees or 180 degrees.

I would like to attach switches to the arduino for each servo. The switch will be used to make the servo rotate to 180. when the switch is closed, it will rotate to 180. When opened, it will rotate back to 0.

Finally, in order to save on power, i was thinking of connecting a relay to break the servo power until the servo needs to move.

Basically, the servo will remain at 0 with no power. When the switch is closed, the relay will close to power up the servo, then the servo will be told to rotate to 180, the relay will open again to power down the servo. When the switch is opened again, the relay will close to power up the servo, the servo will move to 0, then the relay will open again to power down the servo.

I was hoping for some help to write the main footprint of this program. Once i have some kind of outline, I will be able to make adjustments and fine tune things as i need them.

I appreciate any help i can get on this!!! Thank you all!

What code have you tried?

For momentary switch wiring, debouncing and coding there's a great discussion/tutorial by Nick Gammon.

For servo wiring and coding look here.

I am honestly just getting started. Ive had the Mega 2560 for about 3 years and never did anything with it. This will be my first arduino project. I am trying to use examples and combine them to make this work.

I would first do a test to check if your "dampers" will remain in place when there is no power to the servos. If you remove the power to a servo it will no longer be able to retain its position so if there is any load on it at all then it will move. That may be fine but personally I have no idea what's involved in "adjusting dampers".

The Button example in the IDE should be enough to get you reading switches. The Knob example gives you the basics of moving a servo. Then a search on "Arduino servo button" will help combining those ideas by finding plenty of projects that have done it previously.

You can worry about relays when you've checked that taking the power from the servos will work.

Steve

You might use the forum search function and search for "servo button" to see previous discussions. Servos don't use a lot of power when they are in their desired position and not under load. You might consider using the servo attach/detach functions instead of a relay.

Thanks guys..i did just what you said and did a search for different code for each. I dont know why i didnt think of that! I guess i was trying to take the easy way out. ! was able to write a quick code to move the servo using an input pin for testing. The dampers do stay in place with no power so i may look into using a relay to disable the servo power for now. Im not sure yet though.

So my original plan was to purchase a 4-Channel Wifi relay board from Sonoff that i can control with my phone. I would connect the relay outputs to the inputs on the arduino to control the dampers. The dampers are going to be 3 sets of 2 and 1 by itself, so 4 channels would be perfect. To me, being new to Arduino coding, this would be the easiest way to accomplish controlling the dampers remotely and automating with google home.

However, i notice now that there are Arduinos with built in wifi modules. Does this allow the arduino to be controlled remotely, or should i stick with a separate wifi relay model as my original plan?

However, i notice now that there are Arduinos with built in wifi modules

Adafruit, Sparkfun and other vendors sell various products that either add WiFi capability to an existing µC PCB or have it integrated on an µC PCB. The µC PCB can then control a number of relay add-ons via Adafruit IO or other internet data brokerage services or your own web server. There are other lower-cost options. Take a piece of paper and plan what you really want to happen where and how, with discrete or integrated parts.

"However, i notice now that there are Arduinos with built in wifi modules. Does this allow the arduino to be controlled remotely, or should i stick with a separate wifi relay model as my original plan?"

There are boards with the ESP8266 or ESP32 wifi chips that have I/O pins that can operate relays and servos very similarly to the arduino boards. You might check out the WeMOS development boards and similar.

So i started messing around with code. I started modifying some stuff i found. I believe the servo movement is from Zoomkat. I am still trying to get the relay to work, but it just comes on and stays on. Any ideas?

#include <Servo.h>
int button1 = 22; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup() {
// put your setup code here, to run once:
pinMode(50,OUTPUT);
pinMode(button1, INPUT);
servo1.attach(9);
digitalWrite(22, HIGH); //enable pullups to make pin high
}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
digitalWrite(50,HIGH);
delay(500);
servo1.write(160);
delay(500);
digitalWrite(50,LOW);
}
else {
digitalWrite(50,HIGH);
delay(500);
servo1.write(10);
delay(500);
digitalWrite(50,LOW);
}
}

Without a circuit diagram or comments in the code you might need to give us a hint or two. What's on pin 50? Is that "the relay"? Is it active HIGH or LOW?

The way your code is written whether the button is pressed or not pin 50 will go high for a second then go low and immediately loop round and go high again over and over again. So it will seem to be more or less permanently high. What is it supposed to be doing?

BTW you don't need that digitalWrite() to enable pullup. Use pinMode(button1, INPUT_PULLUP) and drop the digitalWrite().

Steve

Hey Steve. I see what you are saying. I’m trying to control a servo. It will stay at 0 until the input is made. Then should move to 180 and then wait until the input is released. Because this only happens over long periods of time, I wanted to use a relay to break the power to the servo. Before the servo command is given to move to 180, the relay will turn on and power the servo. Then after enough time for the servo to complete the movement, the relay will turn off and power down the servo.

I’m almost thinking I wouldn’t be able to use an If Else since the Else will always be running when the input is not made.

Hopefully this clears up a little bit. I am very new and just trying to piece codes together to see if I can get it to do what I want. I appreciate your input!!!

Please format posted code with code tags and draw a pencil & paper diagram so others can understand what the issue is.

ryanm0085:
Hopefully this clears up a little bit. I am very new and just trying to piece codes together to see if I can get it to do what I want. I appreciate your input!!!

How do you expect the servo to go to 0 and 180 when you write 10 and 160 to it? And you haven't answered the question about pin 50/relay. And what does "the input is made". You have a button not an input. Or maybe you're not telling us what the project is actually doing.

And do you realise that the servo needs power to keep its position? If you cut the power to it any load on the servo will move it out of position. If there is little load it will use very little power so cutting the power with a relay is usually pointless.

It seems like you want to do something when the button becomes pressed and do something else when the button is released. If that's it then take a look at the StateChangeDetection example in the IDE.

Steve

Is your switch momentary like a pushbutton or maintained like a toggle switch? Post a link to your relay board.

slipstick:
How do you expect the servo to go to 0 and 180 when you write 10 and 160 to it? And you haven't answered the question about pin 50/relay. And what does "the input is made". You have a button not an input. Or maybe you're not telling us what the project is actually doing.

I had changed the 0 and 180 for testing. Didnt realize i forgot to change it back before copying the code. I thought i had explained the relay in my last post. I want to use it to cut the power to the servo to eliminate any servo noise. It would need to be turned on to power the servo before it moves, then turned off after the servo is finished moving. The servo is being used to move a damper which is balanced. There is no opposing force. It will stay without keeping the servo powered.

As far as the input, I am using a Sonoff Wifi controller that has a relay that will be attached to Pin 22. When i activate it with my phone, i want the arduino to turn on the servo power relay, then move the servo to 180, then turn off the servo power relay.

When the input is off, i would like the servo power relay to turn on, move the servo to 0, then turn the servo power relay off.

I am assuming that the way i have it written, the If and Else will constantly run. I am thinking I need code that will monitor an input, than run once. Than if the input disappears, it will run a different code once.

I will try to draw it out and post a picture of it.

As I said you need to look at the StateChangeDetection example so when the input becomes high something happens (once) then when the input next becomes low again something else happens.

And if there's a next time please tell us what you're actually doing and don't mess about talking about buttons when that's not what you're actually doing!

Steve

Okay i will take a look at it.

Yeahhh. I will definitely be more specific next time. Im going to work on this and see if i can figure it out. Thank you for all of your help! I really do appreciate it!