I will be using a joystick to move a servo left and right, and I will be using the joystick push button as an on /off switch for a string of LEDs. The LEDs will be powered by a 12v source (8 AA batteries.)
My question is, how do I attach the 12v power source within the circuit without frying my board? My joystick is being powered by the 5v on the arduino, but since the LEDs connect to the same circuit, I have no choice but to ground my 12v to the board. I have heard multiple opinions, and some say to use a transistor. I am fairly new to Arduino, but have some experience with electronics. I am wondering how a transistor would help in this situation.
You switch a 12V strip of LEDs just like a relay, except you don't need a freewheel
diode as LEDs are not inductive. There are examples for interfacing to many things
here: Arduino Playground - InterfacingWithHardware
I attached a visual representation and a schematic below. The LED in the schematic serves as a prototype and test at this point. My power source for an LED strip in replacement of the single LED is my main obstacle.
/**
PanTiltControl
*/
#include <Servo.h>
Servo horzServo; // Create a servo object for the pan (horizontal) servo
Servo vertServo; // Create a servo object for the tilt (vertical) servo
const int ledPin = 13; // LED output pin
const int buttonPin = 2; //Input for joystick push button
int buttonState = 0;
int horzPin = 0; // Analog input for the joystick horizontal axis
int vertPin = 1; // Analog input for the joystick vertical axis
int vertVal; // Value read from the vertical axis
int horzVal; // Value read from the horizontal axis
/**
Setup
*/
void setup()
{
pinMode (ledPin, OUTPUT); // initialize pin as output
pinMode (buttonPin, INPUT); // initialize pin as input
horzServo.attach(9); // Use pin 9 PWM output for horizontal servo
vertServo.attach(10); // Use pin 10 PWM output for vertical servo
}
boolean debounce (boolean last)
{
boolean current = digitalRead (buttonPin);
if (last != current)
{
delay(5);
current = digitalRead(buttonPin);
}
return current;
}
/**
Main program loop
*/
void loop()
{
currentButton = debounce (lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
ledOn= !ledOn;
}
lastButton = currentButton;
digitalWrite (ledPin, ledOn);
horzVal = analogRead(horzPin); // Read joystick horizontal position
horzVal = map(horzVal, 0, 1023, 179, 0); // Scale reading to suit servo
horzServo.write(horzVal); // Move servo to required position
vertVal = analogRead(vertPin); // Read joystick vertical position
vertVal = map(vertVal, 0, 1023, 179, 0); // Scale reading to suit servo
vertServo.write(vertVal); // Move servo to required position
delay(15); // Give the servos time to settle
This is low-side switching using an NPN transistor. An n-channel MOSFET will
also switch low-side in a similar manner. If the load isn't a relay/motor, then the
diode across the load isn't needed. The grounds are common, but the supply to
the load (LED strip in your case) can be pretty much what you like if its positive
and the load and transistor can handle the voltage.
You need to consider how much current the load takes - do you know this? The
transistor will need to be rated accordingly, for large currents a MOSFET will be
better.
I'm sorry to say that you would do better to sketch the circuit on a piece of
paper and photograph it than use Fritzing for a schematic, its not really the
tool for the job ATM.
Can you please DRAW a circuit diagram please.
Sorry but fritzy is not a circuit diagram.
Try using EXPRESS PCB program, its free.
Even drawing by hand and photographing it would be okay.
Tom.....
(foetal position for 15minutes. And I thought I'd overcome that reaction too.)