Pages: [1]   Go Down
Author Topic: How to make my arduino uno act as a switch?  (Read 237 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


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);

    delay(15);
  }


}
Logged

UK
Offline Offline
Tesla Member
***
Karma: 127
Posts: 7247
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

does
digitalWrite(3, LOW);    mean off? 
Logged

Online Online
God Member
*****
Karma: 57
Posts: 941
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
digitalWrite(3, LOW);

Yes, the LOW means the pin will be set to 0 volts output.
Logged

Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Pages: [1]   Go Up
Jump to: