Alex Glow
Created May 2, 2019 © GPL3+

Hacksternoon: Mikroe Relay + Bluetooth

Turn something on/off programmatically, using a relay and (maybe) Bluetooth!

IntermediateProtip1 hour104
Hacksternoon: Mikroe Relay + Bluetooth

Things used in this project

Story

Read more

Schematics

Circuit diagram!

Code

Mikroe Two-Relay Test

Arduino
Make a lil drumbeat with two things plugged into the relays!
// source from https://www.hackster.io/ingo-lohs/mikroelektronika-relay-click-board-542fbe

const int RelayOne = 5;
const int RelayTwo = 9;             
const int ledPin = LED_BUILTIN;

void setup() {
 pinMode(RelayOne,OUTPUT);      
 pinMode(RelayTwo,OUTPUT);      
 pinMode(ledPin,OUTPUT);
 digitalWrite(RelayOne,LOW); 
 digitalWrite(RelayTwo,LOW); 
 digitalWrite(ledPin,LOW); 
}
void loop() {
 digitalWrite(RelayOne,HIGH); 
 digitalWrite(ledPin,HIGH); 
 delay(1000);
 digitalWrite(RelayOne,LOW); 
 digitalWrite(ledPin,LOW); 
 delay(1000);
 digitalWrite(RelayOne,HIGH); 
 digitalWrite(RelayTwo,HIGH);  
 digitalWrite(ledPin,HIGH); 
 delay(1000);
 digitalWrite(RelayOne,LOW); 
 digitalWrite(RelayTwo,LOW);  
 digitalWrite(ledPin,LOW); 
 delay(1000);
}

Bluetooth (WIP)

Arduino
/*
 * Copyright (c) 2016 Intel Corporation.  All rights reserved.
 * See the bottom of this file for the license terms.
 * 
 * Based on LED tutorial: https://www.arduino.cc/en/Tutorial/Genuino101CurieBLELED
 */

/*
 * Sketch: led.ino
 *
 * Description:
 *   This is a Peripheral sketch that works with a connected Central.
 *   It allows the Central to write a value and set/reset the led
 *   accordingly.
 */

#include <CurieBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
BLEService reOneService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE Relay Service
BLEService reTwoService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE Relay Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedCharCharacteristic reOneCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedCharCharacteristic reTwoCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int RelayOne = 5;
const int RelayTwo = 9;             
const int ledPin = LED_BUILTIN;

void setup() {
  Serial.begin(9600);

  // set LED pin to output mode
   pinMode(RelayOne,OUTPUT);      
   pinMode(RelayTwo,OUTPUT);      
   pinMode(ledPin,OUTPUT);
   digitalWrite(RelayOne,LOW); 
   digitalWrite(RelayTwo,LOW); 
   digitalWrite(ledPin,LOW); 

  // begin initialization
  BLE.begin();

  // set advertised local name and service UUID:
  BLE.setLocalName("MIKROE RELAY - ALEX");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);
  reOneService.addCharacteristic(reOneCharacteristic);
  reTwoService.addCharacteristic(reTwoCharacteristic);
  
  // add service
  BLE.addService(ledService);
  BLE.addService(reOneService);
  BLE.addService(reTwoService);
  
  // set the initial value for the characeristic:
  switchCharacteristic.setValue(0);
  reOneCharacteristic.setValue(0);
  reTwoCharacteristic.setValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }
      if (reOneCharacteristic.written()) {
        if (reOneCharacteristic.value()) {   // any value other than 0
          Serial.println("Relay 1 on");
          digitalWrite(RelayOne, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("Relay 1 off"));
          digitalWrite(RelayOne, LOW);          // will turn the LED off
        }
      }if (reTwoCharacteristic.written()) {
        if (reTwoCharacteristic.value()) {   // any value other than 0
          Serial.println("Relay 2 on");
          digitalWrite(RelayTwo, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("Relay 2 off"));
          digitalWrite(RelayTwo, LOW);          // will turn the LED off
        }
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

/*
   Copyright (c) 2016 Intel Corporation.  All rights reserved.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

Credits

Alex Glow

Alex Glow

145 projects • 1570 followers
The Hackster team's resident Hardware Nerd. I love robots, music, EEG, wearables, and languages. FIRST Robotics kid.

Comments