Go Down

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


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


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.

The code from the first site I listed was this 

//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code to output a PWM sine wave signal on pin 9

#define fadePin 9

void setup(){
  pinMode(fadePin, OUTPUT); 

void loop(){

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

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

    analogWrite(fadePin, sinOut);




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

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


thank you!I'm a bit noobish with a bit of this stuff, I'll take what you said into consideration


digitalWrite(3, LOW);    mean off? 


Code: [Select]
digitalWrite(3, LOW);

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


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