Electromagnet with Arduino

I’m a newbie in electronics with a background in programming.

So, I want to create a transformer, to do it, first of all, I want to try to do a electromagnet. I know how to do one, and I have done one. The problem is that to do it I have to connect the 5v pin to the ground through a coil, that surrounds a metal. When I do that, after a while, the arduino stops working. I have seen forums that says that you should not connect the 5v to ground. Then I tryied putting a resistor, it didn’t stop, but the metal didn’t heat up neither act like a electromagnet.

Why does that happen? I don’t know that much of electricity, if you could help in the fact of how electricity works in the explanation it would be grateful.

After the electromagnet I want to put another coil in the metal to receive the magnetic induction (I believe is called like that). And I’ll use a program that I’ve done to create like an Alternative Current with two pins of the Arduino. It’s this one:

const byte OUT1 = 9;
const byte OUT2 = 10;

const float FREQUENCY = 0.1;
const float CLAMP = 2 * PI / FREQUENCY;

double deltatime = 0;
unsigned int actualmicros = 0;
unsigned int lastmicros = 0;

void setup(){
  pinMode(OUT1, OUTPUT);
  pinMode(OUT2, OUTPUT);

float f = 0;
int output = 0;
void loop(){
  // deltatime
  actualmicros = micros();
  deltatime = (double)(actualmicros - lastmicros) / 1000000;
  lastmicros = actualmicros;
  f += deltatime; // forward in time
  if ((output = sin(f * FREQUENCY) * 255) > 0){ // get output and calculate with it
    digitalWrite(OUT1, LOW);
    analogWrite(OUT2, output);
  } else if (output < 0){
    digitalWrite(OUT2, LOW);
    analogWrite(OUT1, -output);
  } else {
    digitalWrite(OUT1, LOW);
    digitalWrite(OUT2, LOW);

  // for not having a giantenourmous number
  if (f > CLAMP){
    double d = f / CLAMP;
    int i = (int)d;
    f = CLAMP * (d - i);

And I would like to know if that will work. I think (correct me if I’m wrong) that a coil with a magnetic field moving in it creates a electric field on the coil, then if I make the pins switch with a sine wave (I don’t know why is the sine wave, it’ll work just with a switch?) it will make the coil receive electricity continuously because of the moving magnetic field (change of direction). If that is correct, could you please tell me how the pins are going to work in the secondary coil, like, there will be two ends for the wire, I believe one positive or one negative? I don’t know this part. Help!

Thank you in advance.

Have you heard that shorting an arduino is bad?(it is)
Do you know that just a coil of wire is basically a short?(it is)

You are pulling way to much current through the arduino. It is frying. You need a mosfet to switch on and off the coil.

Search for a mosfet motor driver circuit. Replace the motor for your coil and you will be set.

This is just to make the magnet however.

To make the ac power needed for the transformer, you will need an h bridge, a more powerful power supply, and well, a transformer. My point is, this is a fairly complex project. I would learn more basics with arduino. Definitely come back to this project.

The Arduino processor produces logic level signals only in the range 0-5VDC. It is not a driver or power amplifier. You need interposing components to do things like drive high current devices such as coils.

Here are some example output circuits for interfacing the ATMega328 to the real world.