control Constant Current-Electromagnet

hello guys,

I need to control an electromagnet's intensity. Not an on/off state but varying. Thinkin to use constant current instead of pwm after some reading i've done. Any experience on a specific method's pros and cons?

What sort of transistor one uses with arduino to control constant current ?
I will eventually need to control loads of them.

Appreciate any answers
thanks

Not sure you would gain much. Driving a coil (motor or magnet) with a PWM, the induction of the coils acts like a big old low pass filter. So I'm not sure you would gain any smoother control using a variable (but constant) current driver. Note that many modern constant current driver themselves use PWM internally, followed by a low pass filter.

hey, thanks a lot for reply ,

currently testing pwm method.
im mosfeting my 12v 0.48A electromagnet with arduinos pwm like so http://bildr.org/2012/03/rfp30n06le-arduino/
I cannot actually see any difference in the magnetic field changing the analogWrite() value.
Im using ferrofluid to check.

How can i make pwm 's value make a bigger difference? darkness surrounds me around the matter.
viva forum

I would have to see your code to make any suggestion. However a analogWrite(pin,0) gives full off and analogWrite(pi,255) give full on so I can't see why you don't see a change in field strength if your using full range control.

Lefty

the code is real simple. just controlling the analogWrite value from outside. (works with led)

the problem seems to be on the circuit. (see above) my transistor http://www.irf.com/product-info/datasheets/data/irlz24npbf.pdf

im measuring small variances in amperes but insignificant to make a change.
sorry for newbiness . really apreciate your replys

billybouki:
the code is real simple. just controlling the analogWrite value from outside. (works with led)

the problem seems to be on the circuit. (see above) my transistor http://www.irf.com/product-info/datasheets/data/irlz24npbf.pdf

im measuring small variances in amperes but insignificant to make a change.
sorry for newbiness . really apreciate your replys

Well the circuit design is correct, the MOSFET is a proper logic level device. So that leaves your code to look at, can you post it?

allrigth seems the mosfet was half broken.

its the 3rd one i have destroyed since this morning. now it works fine.
trying to find why they keep breaking

this is the ultrasimple code to send values from vvvv to arduino. It shouldn't cause any issues for all i know

const int bufferSize = 20; // how big is the buffer

char buffer[bufferSize]; // Serial buffer
char commandBuffer[10]; // array to store a command
char pinBuffer[3]; // array to store a pin number
char valueBuffer[10]; // array to store a value
int ByteCount; // how many bytes arrived
boolean ledON; // state of the LED
int pinNumber; // pinNumber
int value; // brightness value

void setup()
{
//start the serial communication at the speed of 9600 baud
Serial.begin(9600);
}

void loop()
{
SerialParser();

//if something arrived
if (ByteCount > 0)
{
if (ledON)
{
analogWrite(pinNumber, value);
}
else
{
digitalWrite(pinNumber, LOW);
}
}
}

void SerialParser()
{
ByteCount = -1;

// if something has arrived over serial port
if (Serial.available() > 0)
{
//read the first character
char ch = Serial.read();

//if it's 's', then it's the start of the message
if (ch == 's')
{
//read all bytes of the message until the newline character ('\n')
ByteCount = Serial.readBytesUntil('\n',buffer,bufferSize);

//if the number of arrived bytes > 0
if (ByteCount > 0)
{
// copy the string until the first ','
strcpy(commandBuffer, strtok(buffer, ","));

// copy the same string until the next ','
strcpy(pinBuffer, strtok(NULL, ","));
strcpy(valueBuffer, strtok(NULL, ","));
if (strcmp(commandBuffer, "LED_ON") == 0)
{
ledON = true;
}
else
{
ledON = false;
}

// convert the string into an 'int' value
pinNumber = atoi (pinBuffer);

// convert the sting into a 'float' value and bring it to (0..255) range;
value = atof(valueBuffer) * 255;
}

// clear contents of buffer
memset(buffer, 0, sizeof(buffer));
Serial.flush();
}
}
}

Such complex coding, way beyond my ability to understand. Might I suggest you just write a very simple testing sketch where you read a analog input pin that has a pot wired to it. Then divide the results by four and output the value via a analogWrite() command to your mosfet. That would allow you to power your magnet with values form 0 to 255 so you can measure the current value going to the magnet as you rotate the pot.

In the future if posting code please enter it into a code window using the # edit option..

Lefty

aa sorry, was looking for the code button.

it is nothing really, just serially setting the value that goes in the analogWrite().
the serialparser() which seems messy is actually converting a string from v4 to a value.
i didnt write it tbh.

seems to work now, hopefully it wont break again.

If you have distroyed 3 FETs so far you are doing something wrong. Do you have a reverse biase diode across the coil? Is the FET getting hot?

i do have a diode, could it be the electromagnet drawing too much current?
I am using a custom made one.(not the one i said before)
the current should be my voltage times resistance of the wire ,right?

maybe during charging or discharging the coil i am getting massive spikes?

If you are using PWM you need a 100R in seriese with the gate, but that only protects the arduino not the FET.

Yes you do get spikes, that is what the diode is for.

Unless the FET is getng hot it is unlikely to be too much current.