Go Down

### Topic: How to make my arduino uno act as a switch? (Read 1 time)previous topic - next topic

#### obito94

##### May 05, 2014, 09:10 am
For a project I'm working on, I need to turn an electromagnet on and off relatively fast.  I need my arduino uno to turn the power on and off at a rate ranging around 10 times per second or 100 times per second at the fastest.   I figured since the adruino uno is capable of doing things in milliseconds, that I could  safely get my arduino to cut and re-supply power to my electromagnet at 100 times per second.

I was going to solder a circuit together(as I have a dislike for breadboards) specifically to connect the signal wire to the positive and negative wires of the electromagnet via a transistor, as demonstrated here
http://bildr.org/2011/03/high-power-control-with-arduino-and-tip120/

http://stackoverflow.com/questions/14833999/arduino-switch-control

What I don't understand is how to tell my arduino uno what it's connecting to, and what pin to use.
for example in void setup, I can't say something simple like  "servo_0.attach(0); " which identifies what it's connecting to (in that case, a servo).  Also, when I was looking at someone else's question in a forum (2nd link above)  where they were trying to get their arduino to turn a circuit off within a time period, he mentions the use pf "digitalWrite(pinNum, HIGH);" , implying that he used this in combination with the code provided from the first site I listed.

//////////////////////////////////////////////////////////////////
//Simple code to output a PWM sine wave signal on pin 9
//////////////////////////////////////////////////////////////////

void setup(){
}

void loop(){

for(int i = 0; i<360; i++){
//convert 0-360 angle to radian (needed for sin function)

//calculate sin of angle as number between 0 and 255
int sinOut = constrain((sin(rad) * 128) + 128, 0, 255);

delay(15);
}

}

#### Robin2

#1
##### May 05, 2014, 09:34 am
I'm not clear if you just want the electormagnet ON or OFF or whether you want to use a PWM signal to give variable magnetization.

If you want to use PWM (i.e. analogWrite()) you need to choose one of the PWM pins. Otherwise you can use any pin (apart from 0 and 1 which are the serial connection to your PC).

If I want to put a HIGH or low on (say) pin 3 it would be as simple as

pinMode(3, OUTPUT); // within setup()

digitalWrite(3, HIGH); // within the code that makes the electromagnet go on/off

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### obito94

#2
##### May 05, 2014, 10:02 am
thank you!I'm a bit noobish with a bit of this stuff, I'll take what you said into consideration

#### obito94

#3
##### May 05, 2014, 11:54 pm
does
digitalWrite(3, LOW);    mean off?

#### Hackscribble

#4
##### May 06, 2014, 12:01 am
Code: [Select]
`digitalWrite(3, LOW);`

Yes, the LOW means the pin will be set to 0 volts output.
arduino@hackscribble.com | www.hackscribble.com

#5
thanks!

#### retrolefty

#6
##### May 06, 2014, 02:27 am
Yes, it's best to not think of digital output pin values as being on or off, they are HIGH (+5 vdc) or LOW (0 vdc or ground). In many cases (common Asian relay modules come to mind) one's external circuit might use LOW to turn on a LED and HIGH to turn off the LED. It all depends on the external wiring used.

Go Up