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.

Code:

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. 

No comments:

Post a Comment