Software Overview  |  Sitemap  |  Downloads  |  Developers  |  Forums
Small_clear_logo_llc

Recipe that Positions a Servo Based on an Analog Input Pin Value

Rotor and Servo

This recipe uses a voltage level on an Arduino analog input pin to position a servo. Positioning is accomplished by using a servo output pin.

An easy way to create a variable input voltage is to use a variable resistor connected between 0 and 5V with its wiper pin connected to the Arduino analog input. If you are using a Grove system, use one of their "Rotary Angle Sensor" devices.

Ingredients

For this recipe, you will need:

  • an Arduino (running Firmata)
  • a Grove Rotary Angle Sensor or equivalent
  • a servo

Script

You can find the Script in the Scripts "examples" area. The Script is called "rotor_and_servo.script". The Script is as follows:

# Use a variable input voltage to control the position of a servo.
# Requires an Arduino, a 0-5V voltage source (a Grove Rotary Angle Sensor
# works well here), and a servo.
# The voltage source is on A0.  The servo is on servo output pin 3.

# Get the file name of the Arduino device.
# On *nix systems, the file will be in the "/dev" directory with
# a name like "/dev/cu.usbmodem411" or "/dev/ttyUSB0".
# On Windows systems, the name will be something like "COM1", "COM2", ....
script_parameters("arduino_port")


# define the location of the voltage source - has to an an analog input
optional_script_parameters(voltage_pin:"A0")

# define the location of the servo - has to be a servo output
optional_script_parameters(servo_pin:3)


# Arduino device
run_script("Scripts/Device/Arduino/Arduino", port_location:arduino_port, id:"arduino")


# Create an analog input to measure the value of the angle sensor.
# Make it's output units be a percentage (0-100).
run_script("Scripts/Device/Arduino/PinTypes/AnalogInputs", names:"angle_sensor", pins:voltage_pin, value_type:"percent", arduino_id:"arduino")


# Create a mapper to convert the 0-100 values of the angle sensor
# to 0-180 for the servo.
run_script("Scripts/Device/Virtual/Mapper", from:"0..100", to:"0..180", id:"servo_mapper")


# Create a servo output to control a servo.
run_script("Scripts/Device/Arduino/PinTypes/ServoOutputs", names:"servo", pins:servo_pin, arduino_id:"arduino")


# wire things together
wire("arduino:angle_sensor", "servo_mapper:in")
wire("servo_mapper:out", "arduino:servo")

To run this Script, you need 3 parameters: the arduino_port, voltage_pin, and servo_pin parameters. The arduino_port parameter is the name of the USB port where the Arduino is plugged in. On *nix hosts, the USB port name is in the "/dev" directory. To see the devices in order of their creation, type "ls -lrt /dev" at a shell prompt and look near the bottom of the list for your Arduino's USB port.

The voltage_pin parameter defines the Arduino pin where the variable resistor is located. It has a default value of "A0" (analog input 0). The servo_pin parameter defines the Arduino pin where the servo is located. It has a default value of 3.

As an example, for an Arduino at USB port "/dev/ttyUSB0" with a variable resistor at pin A0 and an servo at pin 3, the Script parameters are:

arduino_port: "/dev/ttyUSB0"
voltage_pin: "A0"
servo_pin: 3

Running the Script

Run the Script and change the voltage on the Arduino analog input pin by changing the value of the variable resistor. The servo will move as you change the voltage value.

Catalina Computing, LLC.

Copyright © Catalina Computing, LLC. (2013-2018)




Page last updated: Fri Jan 17 16:33:56 2014 (UTC)