Datasheet

6
digitalWrite(Electromagnet, HIGH); // turn the Electromagnet on (HIGH
is the voltage level)
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage
level)
delay(1000); // wait for a second
digitalWrite(Electromagnet, LOW);// turn the Electromagnet off by
making the voltage LOW
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
4.2 With Raspberry Pi
1. You should have got a raspberry pi and a grovepi or grovepi+.
2. You should have completed configuring the development environment, otherwise follow here.
3. Connection. Plug the sensor to grovepi socket D4 by using a grove cable.
4. Navigate to the demos' directory:
cd yourpath/GrovePi/Software/Python/
To see the code
nano grove_electromagnet.py # "Ctrl+x" to exit #
import time
import grovepi
# The electromagnet can hold a 1KG weight
# Connect the Grove Electromagnet to digital port D4
# SIG,NC,VCC,GND
electromagnet = 4
grovepi.pinMode(electromagnet,"OUTPUT")
time.sleep(1)
while True:
try:
# Switch on electromagnet
grovepi.digitalWrite(electromagnet,1)
print "on"
time.sleep(2)
# Switch off electromagnet