Monday, April 25, 2011

Controlling a Picoswitch with Arduino

I had a viewer of my Youtube channel request the code accompanying this video. I made this video a long time back because I wanted to trigger a battery-powered airsoft gun from my robot. The Picoswitch is a solid state relay that works with pulse width modulation, same as a standard hobby servo. What this means is, you can supply power to the switch, and a load, such as an electric motor and you can toggle off and on any device up to 1 Amp from a standard R/C radio or microcontroller. If you need more current flow, try Dimension Engineering's Battleswitch. I know, the code says servo, but treat it exactly the same and it works.
Here ya go!

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int pulse; // variable to show pulse width
void setup()
{
Serial.begin(9600); // begins serial communication with the computer
myservo.attach(12); // attaches the servo on pin 12 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
pulse = map(val, 0, 179, 750, 2250); // get a pulse width for current pot position
myservo.write(val); // sets the servo position according to the scaled value
Serial.print("degrees ");
Serial.print(val);
Serial.print(" pulse" );
Serial.println(pulse);
delay(15); // waits for the servo to get there
}

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. dude you wanna tell me why your video is talking about a picoswitch and your code talk's nothing about this switch? what does this video relates to servo and a switch ? seems like 2 unrelated stories? did you fucked up ?

    ReplyDelete
  3. Because you can use the exact same code example as controlling a servo. I didn't bother changing the terminology, but it's the same thing functionally.

    ReplyDelete
    Replies
    1. ahhh ic , thanks for the explanation, i was looking for a example for my robot but i dont think this is going todo the whole trick i need do, can you make this to take commands from serial ? "move to pos " & at this speed?

      Delete
  4. You would change the code from reading a analog value from a pot., to reading in a number from the serial monitor using 'serial.write;' command. Speed is dependent on the servo's design, but you could do a workaround by setting a position and incrementally moving it to that position in a loop while inserting a delay of your choosing to slowly get there

    ReplyDelete
  5. example pls? im not really getting the picture ? will mean alot dude thanks!

    ReplyDelete