Hack Attack Tutorial: Controlling a Servo using a Flex Sensor

 In Tutorials

The flex sensor is a versatile sensor that is most notably used for gaming controllers such as the Nintendo PowerGlove. Its resistance increases the more it’s bent, allowing us to detect the amount bending in a single direction. Voltage divider circuits are typically used to detect this change in resistance. In this tutorial, we’ll teach you how to use a single flex sensor to control the position of a servo.

You can also find the (much quicker) video version of this tutorial at our Youtube channel and Facebook page. Subscribe to get the latest news and tutorials as they come out!


STEP 1. For this tutorial you will need a flex sensor (we used the short one here but the long one works too), an Arduino Uno (We’re offering a 14% discount for all orders of the Arduino Uno Rev3 until the end of February! 😀), a servo, a breadboard, a 10k resistor (brown, black, orange) and a lot of wires.

STEP 2. Follow the wiring diagram below.

STEP 3. Connect your Arduino to your laptop using an A-B cable. Open your Arduino IDE and upload the code below. The 10k resistor and the flex sensor form the voltage divider of our circuit. We used PIN0 (input pin) to detect the bending of the sensor and PIN10 (output pin) to control the servo. We also have to map and constrain our output to the servo’s limits of 0-180 degrees.

#include <Servo.h>

const int pinflex = 0;

Servo myServo;

void setup() {



void loop() {

int flexpos;

int servopos;

flexpos = analogRead(pinflex);

servopos = map(flexpos, 600, 900, 0, 180);

servopos = constrain(servopos, 0, 180);




STEP 4. Test out your circuit by varying the amount of bending in the sensor. If all goes well, your servo should follow along.

Recommended Posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.