Sunday, 21 October 2012

Pressure sensor

Hello everyone,

I came across to a nice tutorial about how to control a servomotor's position using the value returned from an analog sensor and I've decided to try it but adapting it using the SSC 32 controller as I will use it in my tetrapod.
I'm using a variable resistance pressure sensor as a analog input. The sensor operates by decreasing resistance the harder it is pressed. When applying different pressure levels to the sensor the servo will move accordingly.
The sensor is connected in series with a fixed resistor across a 5v supply (from Arduino) forming a voltage divider. The pressure dependent voltage is then read by the microcontroller and then the value is mapped to move the servo in a range between 0 and 180 degrees.
Let's see the connexions sketch. Note I couldn't find in Fritzing an SSC 32 controller so I've used a PWM shield to show connections:

I've used a 820 ohms reistor instead of 10k because I couldn't find one in my toolbox and the resistor is as I said before to act as a voltage divider. So I play with the one I had and it worked as you can see in the little video.
I've used the Serial Monitor from Arduino IDE to visualise the readings from analog input to be able to map the values.


int potpin = 0;                                                // analog pin used to connect the potentiometer
int val;                                                           // variable to read the value from the analog pin

void setup()
  Serial.begin(115200);                                 //initiate serial communication with SSC 32

void loop()
  val = analogRead(potpin);                         // reads the value of the pressure sensor (value between 323 and 330)
  val = map(val, 323, 330, 100, 500);          // scale it to use it with the servo (value between 0 and 180)
  Serial.print ("#");
  Serial.print (0);
  Serial.print (" P");
  Serial.print (val);                                       // sets the servo position according to the scaled value
  Serial.print (" T");
  Serial.println (800);                
  delay(15);                                                 // waits for small amount of time before making another reading

Maybe I can use this type of sensor to make my tetrapod react in different ways. We will see. 

Wednesday, 17 October 2012

Arduino + SSC 32 + Servo

Hello everyone,

In this post I want to show how to use the SSC32 controller with Arduino for moving few servos and compare it with a direct connexion of servos to Arduino without using a servo controller.

I will start with the later...

We need:
  • Arduino
  • Servos
  • Breadboard
  • Wires
  • Power cable for Arduino
I've made the connections layout below using fritzing

Arduino receives power and data from USB. Arduino is powerful enough to give power to 3 servos so we can connect red and black wire from 5v and ground in Arduino to breadboard. Servos will take power from the breadboard lines set by Arduino. We also need to connect yellow wire to a PWM input pin. I select pins 2, 3 and 4. If we want to connect more servos probably we will need to add an external battery to power them.

#include <Servo.h>
Servo servoMain1;                                // Define our 1st Servo
Servo servoMain2;                                // Define our 2nd Servo
Servo servoMain3;                                // Define our 3rd Servo
void setup()
   servoMain1.attach(2);                        // servo1 on digital pin 2
   servoMain2.attach(3);                        // servo2 on digital pin 3
   servoMain3.attach(4);                        // servo3 on digital pin 4
void loop()
   servoMain1.write(0);                          // Turn Servo1 to 0 degrees
   servoMain2.write(90);                        // Turn Servo2 to center position (90 degrees)
   servoMain3.write(180);                      // Turn Servo3 180 degrees
   delay(1000);                                        // Wait 1 second
   servoMain1.write(180);                      // Turn Servo1 to 180 degrees
   servoMain2.write(135);                      // Turn Servo2 135 degrees
   servoMain3.write(90);                        // Turn Servo3 to center position (90 degrees)
   delay(1000);                                       // Wait 1 second
Using SSC32
  • Arduino
  • Controller SSC32
  • Servos
  • Wires
  • External battery
  • Power cable for Arduino and SSC32
We need to connect the SSC32 controller to Arduino like below:

See also how to connect the 3 wires of each servo and where to connect an external power supply (5v) for the servos. The picture below shows the controller being powered by USB cable to the computer.

Also it is important to set the baud rate the same as in the code ("serial.begin" parameter)

void setup() {
Serial.begin(115200);                              //Start serial communication at 115200 baud rate
void loop() {
  Serial.println("#0 P750 #4 P750 #11 P750 T500");         //Moves 3 servos at same time
  delay(1000);                                                                     //Wait 1 second
  Serial.println("#0 P2200 #4 P1500 #11 P2200 T500");

Using the SSC 32 controller the connexions are simpler and neater. Also is really powerful to be able to move many servos at same time using the multi-servo command (the one used in the code above). Notice in the video that the servo with yellow target starts and finish movement at the same time as the others even though rotates smaller angle, so movement is slower than the other 2.