Hacksternoon: Mikroe Relay + Bluetooth

Hacksternoon: Mikroe Relay + Bluetooth © GPL3+

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

  • 65 views
  • 0 comments
  • 2 respects

Components and supplies

About this project

For this build, we'll be going off of Katie's Hackster Live tutorial from March 2017 (!!). However, I also referenced this other tutorial, by Ingo Lohs.

You can use this to programmatically turn on and off:

  • a motor
  • an LED
  • whatever else you desire!

The Build

Plug your Arduino Click Shield into the Arduino 101 board, then plug the Relay Click into position 2. If you want to use position 1 instead, you'll need to change the code to use pins 6 and 10.

Your circuit is going to use the relay as a switch, meaning that one of the wired connections (ground or voltage) is going to be cut in half, and both cut ends will be plugged into the screw terminals on the shield.

For example, in the circuit below, the motor is powered by a green and a black wire (voltage and ground). (The green is arbitrary.) If you connect those wires directly to the battery, it will run forever. However, if you cut the green wire and run it through the relay, you can then switch the circuit on and off. The connection is PHYSICALLY connected and broken by electromagnets inside the relay! It makes an awesome clicking noise!! I love relays. :)

To connect the wires to the relay, strip the ends (if necessary) and twist the wire strands so they stay together (if necessary). Insert them into the top of the holes and tighten down the screws on top. In the images below, the two left holes are un-connected; see the metal glinting at the bottom of the holes? Those are little metal plates that raise up to clamp the wires in place, so make sure you screw them in tightly :)

The Code

Get the Arduino IDE from: https://www.arduino.cc/en/Main/Software

Go to Tools > Board > Boards Manager, and search for "Curie", then install the Intel Curie-based boards.

See the code below for my test, which was just switching the two relays on and off in sequence, with a motor to make a little rhythm machine. :)

You may need to hit the MASTER RESET button on the Arduino itself (next to the USB port) in order to upload code. Or, unplug the Arduino from your computer, click Upload, and then plug it back in.

// BLUETOOTH UPGRADE

Next, I'll be trying to get it working with Bluetooth via this other app, which seems a little simpler than the whole AWS shebang:

https://www.arduino.cc/en/Reference/CurieBLE

https://www.arduino.cc/en/Tutorial/Genuino101CurieBLELED

You can find it in Arduino under File > Examples > CurieBLE > Peripheral > LED.

I believe these become available once you install the Arduino 101 via the Boards Manager:

I'm able to get the device's built-in LED to turn on, by sending a "Byte Array" of 0x01 (or 0x00 for "off"). However, the relays won't do anything... yet. :)

You probably want to change the name of your device in the list; just edit the "LED" part of .setLocalName("LED") to say whatever you want to call it.

Code

Mikroe Two-Relay TestArduino
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
*/

Schematics

Circuit diagram!
Img 4889 soan6uqxkb

Comments

Add projectSign up / Login