help! arduino project with electromagnets

hello,
i have a problem getting my arduino hooked up with some electromagnets.

the blue wires coming from the electromagnets go to power and the yellow wire is ground.
i managed to hook up the first three magnets, but they don’t work properly, they get switched on and off randomly instead of following the code (which you can find down below).
i think the problem is probably something about the connection to the ground wire, i’ve tried these black things which let the power flow only in one direction (in the picture on the left, sorry, i don’t know the name in english), but the magnets didn’t work with these between the magnet and the ground wire.
so then i soldered normal wires from the copper wire to the ground line and that works better, but not correctly. and also only the thirst three magnets work, number 4 and 5 don’t work at all when it’s hooked up that way.
i would be really greatful for any help or ideas…
have a nice day,
jo

int line0 ={ 0,0,0,0};
int line1 ={ 1,0,0,0};
int line2 ={0,1,0,0};
int line3 ={1,1,0,0};
int line4 ={0,0,1,0};
int line5 ={1,0,1,0};
int line6 ={0,1,1,0};
int line7 ={1,1,1,0};
int line8 ={0,0,0,1};
int line9 ={1,0,0,1};
int line10 ={0,1,0,1};
int line11 ={1,1,0,1};
int line12 ={0,0,1,1};
int line13 ={1,0,1,1};
int line14 ={0,1,1,1};
int line15 ={1,1,1,1};
int dataChipPower= {2,3,4,5} ;
int dataChipGND= {6,7,8,9};
int strobe =10;
int val=0;

void powerLine(int nbr){
digitalWrite(strobe,HIGH);
delay(10);
val=nbr[0];
digitalWrite(dataChipPower[0],val);
val=nbr[1];
digitalWrite(dataChipPower[1],val);
val=nbr[2];
digitalWrite(dataChipPower[2],val);
val=nbr[3];
digitalWrite(dataChipPower[3],val);
digitalWrite(strobe,LOW);
}

void gndLine(int nbr){
digitalWrite(strobe,HIGH);
delay(10);
val=nbr[0];
digitalWrite(dataChipGND[0],val);
val=nbr[1];
digitalWrite(dataChipGND[1],val);
val=nbr[2];
digitalWrite(dataChipGND[2],val);
val=nbr[3];
digitalWrite(dataChipGND[3],val);
digitalWrite(strobe,LOW);
}
void setup(){
for(int i=0;i<3;i++){
pinMode(dataChipPower * , OUTPUT);*
_ pinMode(dataChipGND , OUTPUT);_
* }*
}
void loop(){
powerLine(line1);
delay(500);
powerLine(line2);
delay(500);
powerLine(line3);
delay(500);
}

Here a quick observation:

 for(int i=0;i<3;i++){
   pinMode(dataChipPower[i]  , OUTPUT); 
   pinMode(dataChipGND[i] , OUTPUT); 
 }

This loop only sets three of each pin class to “OUTPUT” mode, because “i” only attains values 0, 1, and 2. Try

 for(int i=0;i<4;i++){
   pinMode(dataChipPower[i]  , OUTPUT); 
   pinMode(dataChipGND[i] , OUTPUT); 
 }

Cheers,

Mikal

Not that you asked, but here’s a simplified condensation of your code that may make bug hunting easier:

int dataChipPower = {2,3,4,5};
int dataChipGND = {6,7,8,9};
#define strobe 10

void powerLine(int nbr){
digitalWrite(strobe, HIGH);
delay(10);
for (int i=0; i<4; ++i)
digitalWrite(dataChipPower*, nbr & (1<<i));*

  • digitalWrite(strobe, LOW);*
    }
    void gndLine(int nbr){
  • digitalWrite(strobe, HIGH);*
  • delay(10);*
  • for (int i=0; i<4; ++i)*
    _ digitalWrite(dataChipGND*, nbr & (1<<i));_
    _
    digitalWrite(strobe, LOW);_
    _
    }_
    void setup(){
    _
    for(int i=0; i<4; i++){_
    _ pinMode(dataChipPower, OUTPUT);
    pinMode(dataChipGND, OUTPUT);
    }
    }
    void loop(){
    powerLine(1);
    delay(500);
    powerLine(2);
    delay(500);
    powerLine(3);
    delay(500);
    }[/quote]*

    Let us know how this project turns out! It looks interesting!
    Mikal_

Any idea how much current those electromagnets use when on? The arduino I/O pins may simply not be able to drive them directly.

If I misunderstood and there is already something between the arduino and the magnets, my apologies…

i've tried these black things which let the power flow only in one direction, but the magnets didn't work with these between the magnet and the ground wire.

These are called diodes and there will be a stripe on one end. Connect them across the magnets (that is in parallel) with the stripe towards the +ve. There job is to suppress the back EMF (reverse voltage) generated when the magnet is turned off, this could be upsetting your system.

now it’s (kind of) working, but i have the problem that only up to 5 of the magnets work, it seems to be something like too few power.
i changed the code to this to get 4 running, they all run, but after about 15 seconds the fourth one gets weaker and weaker and then after a while does not work at all, or just sometimes but really weak, while the other 3 work ok…
any ideas?

int line0 ={ 0,0,0,0};
int line1 ={ 1,0,0,0};
int line2 ={0,1,0,0};
int line3 ={1,1,0,0};
int line4 ={0,0,1,0};
int line5 ={1,0,1,0};
int line6 ={0,1,1,0};
int line7 ={1,1,1,0};
int line8 ={0,0,0,1};
int line9 ={1,0,0,1};
int line10 ={0,1,0,1};
int line11 ={1,1,0,1};
int line12 ={0,0,1,1};
int line13 ={1,0,1,1};
int line14 ={0,1,1,1};
int line15 ={1,1,1,1};
int dataChipPower= {2,3,4,5} ;
int dataChipGND= {6,7,8,9};
int strobe =20;
int val=0;

void powerLine(int nbr){
digitalWrite(strobe,HIGH);
delay(10);
val=nbr[0];
digitalWrite(dataChipPower[0],val);
val=nbr[1];
digitalWrite(dataChipPower[1],val);
val=nbr[2];
digitalWrite(dataChipPower[2],val);
val=nbr[3];
digitalWrite(dataChipPower[3],val);
val=nbr[4];
digitalWrite(dataChipPower[4],val);
digitalWrite(strobe,LOW);
}

void gndLine(int nbr){
digitalWrite(strobe,HIGH);
delay(10);
val=nbr[0];
digitalWrite(dataChipGND[0],val);
val=nbr[1];
digitalWrite(dataChipGND[1],val);
val=nbr[2];
digitalWrite(dataChipGND[2],val);
val=nbr[3];
digitalWrite(dataChipGND[3],val);
val=nbr[4];
digitalWrite(dataChipGND[4],val);
digitalWrite(strobe,LOW);
}
void setup(){
for(int i=0;i<4;i++){
pinMode(dataChipPower * , OUTPUT);*
_ pinMode(dataChipGND , OUTPUT);_
* }*
}
void loop(){
powerLine(line1);
delay(500);
powerLine(line2);
delay(500);
powerLine(line3);
delay(500);
powerLine(line4);
delay(500);
}

any ideas?

Yes it's the hardware, no need to keep posting the software. You are probably running out of current on the power drive.

ok, thanks. what could i do about that?

this seems to be similar: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1223157698

-arne

First step would be to figure out how much current each coil is drawing when in operation under 5 volts, and then come back here and ask for help picking something to drive twice that load (just to be safe).

I suspect you could build something quite serviceable with one or two transistors per coil, depending on the needed current.

Got an amp-meter and a strong source of 5V power? ::)