Grove Relay module

Demonstrate the usage of a relay as a control mechanism.

Hello World

Import programSeeed_Grove_Relay_Example

Grove Relay module, simple example to turn the relay on / off.

Library

Import programSeeed_Grove_Relay_Example

Grove Relay module, simple example to turn the relay on / off.

Datasheet

http://www.seeedstudio.com/wiki/Grove_-_Relay

Notes

The Grove-Relay module is a digital normally-open switch. Through it, you can control circuit of high voltage with low voltage, say 5V on the controller. There is an indicator LED on the board, which will light up when the controlled terminals get closed.

Examples

Busy Wait Relay

In this example the relay is opened / closed via a digital signal. The wait() function is used to wait for a specified number of seconds.

busy_wait_relay.cpp

#include "mbed.h"

DigitalOut relay(D6);
int on = 1,off = 0;
int main()
{

    // Loop forever, relay on/ off for 1 second each
    // LED on relay will blink on / off 
    while(1) {
        relay = on;
        wait(1);
        relay = off;
        wait(1);
    }
}

Timer Countdown Relay

An alternative way of killing time is to use the Ticker class. The Ticker class implements a countdown timer. When the ticker has counted down to zero it will call a function you designate, reset the time counter, and start counting down. In this example we set up the relay_ticker with a counter for 1 second, so every 1 second the function ticker_handler will be called which will invert the value of the relay to turn it on / off.

ticker_relay.cpp

#include "mbed.h"

DigitalOut relay(D6);
Ticker relay_ticker;
int on = 1,off = 0;

void ticker_handler(void){
    relay = relay ^ 1; // flip the value in relay (on->off, off->on)
}

int main(){
    relay_ticker.attach(&ticker_handler, 1); // set ticker counter to 1 second
    
    while(1){
        // do something else here
    }       
}

You need to log in to post a discussion