Fun with Arduino 18 Railway Crossing Servo Motor to Operate the Gate

Servo_SG90We can use a servo motor to operate the gate. Servo’s can be had in different sizes, strengths and qualities, like fitted with plastic or with metal gears. For our purpose a cheap SG90 type servo will do fine. They can be had for under €2,-, but even at that price we get quite some amazing technology with multiple components all packed inside the tiny housing:

  • servo_internala DC motor
  • a control circuit, partly digital, partly analog
  • a potentiometer for angle measurement feedback
  • a gearbox

Servo_Control

The servo uses a closed loop feedback control system. The shaft angle is measured and a motor current is generated when the measured angle differs from the target angle. When it has reached the target angle, it wants to stay there. Any force that tries to change the angle will be counteracted by sending a current to the motor. This is why you can sometimes hear them rumble a bit.

Servo_WiringBy the way, tiny as they are, they can be quite hungry for current. One or maybe two SG90 servos can safely be powered from the Arduino on board 5V, but if you have more, then connect them to a separate 5V power supply. Take care that the GND of that supply is connected to the GND of the Arduino.

The control signal to be sent to the servo looks much like a pwm signal. We do not have to generate this signal ourselves, it is taken care of by servo functions that come available once we’ve included the servo library. A maximum of 12 servo’s can be controlled by an Arduino UNO and we have to give every servo a name. Let’s name ours gate_servo. Now we can use an instruction like gate_servo.write(35) to rotate the servo to a 35 degrees angle.

A standard SG90 servo has a range of 0-180 degrees. If we don’t take measures, the servo moves at maximum speed. We want it to rotate at a controlled speed however and we can do that with a similar technique as we did with LED fade: we increment or decrement the angle step by step with an interval time.

Read on below the video …

Enough theory … let’s connect a servo and play with it. The code is almost similar to the LED fade code we used in video 16, with a few additions for the servo:

  • The servo library is loaded: #include
  • A servo object is created: Servo gate_servo;
  • The servo is connected to a pin: gate_servo.attach(SERVO_PIN);
  • An angle is sent to the servo with: gate_servo.write(angle);

The servo moves from angle A to angle B when we toggle the switch. Angle B can be changed with the potentiometer. The speed is determined by the SERVO_SPEED interval time in ms. In the code the word ‘setpoint’ is used in stead of ‘target’ because this is more custom in control systems.

#define SWITCH_PIN    2
#define SERVO_PIN     3
#define GATE_SPEED   50 // [ms] lower number is higher speed
#define GATE_OPEN    45 // angle
#define GATE_CLOSED 135 // angle
#define POTM_PIN     A0

#include 
Servo gate_servo; // creates a Servo with the name 'gate_servo'

byte angle    = GATE_OPEN;
byte setpoint = GATE_OPEN;
unsigned long time_for_servo;

void setup() {
  pinMode(SWITCH_PIN,INPUT_PULLUP);
  gate_servo.attach(SERVO_PIN);
  Serial.begin(9600);
}

void loop() {

  if(digitalRead(SWITCH_PIN) == LOW)
    setpoint = map(analogRead(POTM_PIN), 0, 1024, 2 , 179);
// alternative: setpoint = GATE_CLOSED; 
  else
    setpoint = GATE_OPEN;

  if (millis() > time_for_servo) {
    time_for_servo = millis() + GATE_SPEED;
    if (angle < setpoint) angle++;     if (angle > setpoint) angle--;
    gate_servo.write(angle);
    Serial.print(setpoint);
    Serial.print("  ");
    Serial.println(angle);
  }
}

Let’s test this … yes … the servo moves when we toggle the switch and we can change the angle with the potentiomenter.

With our railway crossing we will use fixed angles for the gate servo, approximately 90 degrees apart. We can use this sketch to find the correct angles and later put them in the code.

Optical_SensorOn to the next challenge … we need to detect a train. We’ll use optical sensors for that … like this one. (Click to enlarge.)

— 0 —

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s