Arduino: control a LED and one servo motor using a joystick
In this article we’ll control a servo motor using a joystick and the microcontroller Arduino.
We’ll also set the value of a LED-2-colors in according with the stick axis.
It’s just an introduction for the next article: Arduino and Flex: connect a joystick to your application
The final result (I’m sorry for the video quality):
REQUIRED HARDWARE
To accomplish this task you need the following hardware:
- Microcontroller Arduino Board Duemilanove
- A Bread Board
- LED 2Colors (or 2 different LED)
- Servo motor (i’m using a simple hitec HS-55)
- Joystick (i’m using a Mini Stick connected to a joystick board)
THE PROTOTYPE
First of all we need to assemble the circuit as you can see in the following images:
photo 1 | photo 2 | photo 3 | photo 4 | photo 5
So:
1) The Joystick is connected to analog pins 4 and 5.
2) The Servo to digital pin 3
3) LED to pins 6 and 11 (instead of 6 you could have a better experience using pin 9 or 10)
SKETCH FILE
/* Joystick + Servo + Led 2 Colors Features: - Move the servo using a Joystick - Change LED1 value in according to the horizontal joystick position - Change LED2 value in according to the vertical joystick position NOTE1: I have used a LED with two Colors but you could use 2 different LED NOTE2: Joystick Button is not used in this sketch The circuit: * LED 2 COLORS attached to digital PWM pin 3 and 11. * Servo attached to digital PWM pin 6 * Joystick attached to analog pins 4 and 5 * created 28 october 2009 * by Fabio Biondi <http://www.fabiobiondi.com/blog> */ #include <Servo.h> const int servo = 3; // the number of the servo const int ledPin1 = 6; // the number of the LED pin 1 const int ledPin2 = 11; // the number of the LED pin 2 const int joyH = 4; // the number of the Joystick Horizontal Axis const int joyV = 5; // the number of the Joystick vertical Axis int servoVal; // variable to read the value from the analog pin Servo myservo; // create servo object to control a servo void setup() { // initialize the LED pin as an output: pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); // Servo myservo.attach(servo); // attaches the servo on pin 9 to the servo object // Inizialize Serial Serial.begin(9600); } void loop(){ // Check Joystick values using the serial monitor outputJoystick(); // Read the horizontal joystick value (value between 0 and 1023) servoVal = analogRead(joyH); servoVal = map(servoVal, 0, 1023, 0, 20); // scale it to use it with the servo (value between 0 and 20) analogWrite(ledPin1, servoVal); // Set the LED1 Value // Read the horizontal joystick value (value between 0 and 1023) servoVal = analogRead(joyV); servoVal = map(servoVal, 0, 1023, 0, 20); // scale it to use it with the servo (value between 0 and 20) analogWrite(ledPin2, servoVal); // Set the LED2 Value // Move Servo servoVal = analogRead(joyV); // reads the value of the potentiometer (value between 0 and 1023) servoVal = map(servoVal, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) myservo.write(servoVal); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } /** * Display joystick values */ void outputJoystick(){ Serial.print(analogRead(joyH)); Serial.print ("---"); Serial.print(analogRead(joyV)); Serial.println ("----------------"); }
In the next article will see how to control a Flex application using the same circuit











[...] Fabio Biondi Blog » Blog Archive » Arduino: control a LED and one … [...]
[...] Connect a joystick to your arduino board (you can get more info in the article Arduino: control a LED and one servo motor using a joystick) [...]
sei un fottuto genio!
[...] Fabio Biondi Blog » Blog Archive » Arduino: control a LED and one … [...]
what if i want two servos one for up,down and one for left,right?
how do i connect?
do i connect second servo were the LED was connected >
servos red, and black connect to + and - and the yellow to what pin?
TNX
you could to “attach” another servo moving it just the other one.
i.e.: myservo.attach(servo2);
Or using a micro serial controller, like the Pololu. Check it :
http://www.fabiobiondi.com/blog/2009/12/arduino-and-pololu-micro-serial-8-servo-controller/
Leave your response!
Archives
Categories
Blogs
3D ActionScript Adobe AIR AIR for Android android Arduino Binding burrito hero 4.5 TabbedMobileApplication Chat Chrome Cocomo code optimization components custom/generic components custom components DataList developer central Emitter FLARToolkit Flash Flash Builder Flex HTTPService illustrator Infrared Interactions itemRenderer Joystick Loader mobile Papervision PHP Rss Servo singleton Skin Skinning Transitions Tween URLLoader URLRequest Vertical Scrollbar VScrollbar youtube
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.
Recent Posts
Develop Mobile apps with Catalyst?
Code optimization
TextInput Component
Interactions and Transitions
Most Commented
Recent Comments
how to call methods between swf files