top of page

8. Using Magnetism to switch a Green LED on and off

 

In this tutorial you will learn how to control an actuator using a magnetic switch. You will also learn how a magnetic switch responds to a magnet, and how a LED can be used as a measuring device for magnetism. In this example, we use our Jester character to magically switch a green LED on (secretly holding a magnet behind his back).

Required components:

SODAQ Moja

0.5W Solar Panel

1aH battery pack

Grove Green LED

Grove Magnetic Switch

A (fridge) Magnet

2x Grove w/o Buckle Cable (any length)

 

First, plug the Switch into the D2/D3 pin in the always on row.

Then, plug the Green LED into the D4/D5 pin in the always on row.

Next, plug the 0.5W solar panel and the 1A LiPo battery into the respective sockets (see http://www.sodaq.net/#!getting-started/c21ma for a board diagram)

 

Turn on the SODAQ board, compile and upload the following sketch from the arduino IDE onto the SODAQ Board, and then unplug the USB cable from the computer when it is working:

 

#define MAGNECTIC_SWITCH 2

#define LED 4

 

void setup()

{

 pinsInit();

}

 

void loop()

{

 if(isNearMagnet())  //if the magnetic switch is near the magnet?

 {

  turnOnLED();

 }

 else

 {

  turnOffLED();

 }

}

 

void pinsInit()

{

 pinMode(MAGNECTIC_SWITCH, INPUT);

 pinMode(LED,OUTPUT);

}

 

/*If the magnetic switch is near the magnet, it will return ture, */

/*otherwise it will return false */

 

boolean isNearMagnet()

{

 int sensorValue = digitalRead(MAGNECTIC_SWITCH);

 if(sensorValue == HIGH)   //if the sensor value is HIGH?

 {

  return true;   //yes,return true

 }

 else

 {

  return false;   //no,return false

 }

}

 

void turnOnLED()

{

 digitalWrite(LED,HIGH);

}

 

void turnOffLED()

{

 digitalWrite(LED,LOW);

}

© 2013 SODAQ. All rights reserved.

  • Facebook Classic
  • Twitter Classic
bottom of page