OPEN-SMART Electromagnet

by wilsnico in Circuits > Arduino

47 Views, 1 Favorites, 0 Comments

OPEN-SMART Electromagnet

20241001_133017.jpg
20241001_133017.jpg
20241001_133121.jpg
20241001_133511.jpg
20241001_133531.jpg
20241001_133436.jpg
20241001_133301.jpg
20241001_132432.jpg
20241001_133453.jpg

Open-smart electromagnet operated with push button.

Supplies

20241001_133511.jpg
20241001_133436.jpg

Arduino UNO

Jumper wires (generic)

KY-004

Open-Smart Electromagnet

20241001_133453.jpg

Arduino Uno Code :


const int buttonPin = 4;

const int magnetPin = 5;

int buttonState = 0;

int lastButtonState = 0;

bool magnetState = false;


void setup() {

pinMode(buttonPin, INPUT_PULLUP);

pinMode(magnetPin, OUTPUT);

digitalWrite(magnetPin, LOW);

}


void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState == LOW && lastButtonState == HIGH) {

delay(50);

magnetState = !magnetState;

if (magnetState) {

digitalWrite(magnetPin, HIGH);

}

else {

digitalWrite(magnetPin, LOW);

}}

lastButtonState = buttonState;

}