My Cart

How to Remotely Control a Servo Using LoRa Extreme Range Connectivity Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to move a servo to different angles depending on the data sent by the LoRa gateway.

Ingredients:

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the SX1272 LoRa module, consult the main tutorial.

Step 1: Connection

Connect the multiprotocol shield to Arduino or to Raspberry Pi connection bridge, and then connect LoRa module. Connect the servo in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to the 3.3 volt supply and ground. Servo has 3 wires with different colors. Connect Black/Brown wire to GND, Red wire to 3.3 volt and the remaining wire, Yellow/Orange, to pin 9 of Arduino. Pin 9 has PWM control so you can move your servo from 0 to 180 degrees.

On the other hand you must connect a gateway with a LoRa module to a PC.

Step 2: The Code

First you will need to open a terminal program like Cutecom (Linux), Hyperterminal or Putty (Windows). Follow the instructions in this tutorial, specially point 2 to make the communication setup.

Programs as Cutecom allow to enter hexadecimal numbers to enter these characters (Select Hex input). If you are using another program, you can use some tools to edit hexadecimal characters.

Type the following hexadecimal code to program the LoRa gateway with this specifications:

  • Mode=4 (BW=500 CR=4/5 SF=12)
  • Node address=3
  • Channel=12_868

LoRa gateway:

Code:
01 53 45 54 23 46 52 45 43 3a 43 48 5f 31 32 5f 38 36 38 3b 41 44 44 52 3a 33 3b 42 57 3a 42 57 5f 35 30 30 3b 43 52 3a 43 52 5f 35 3b 53 46 3a 53 46 5f 31 32 0d 0a 37 39 43 41 04
        

Now you can send this five different packages to move the servo to different positions:

Angle: 0 degrees (send broadcast the letter 'A')

01 44 41 54 41 23 30 23 41 53 43 49 49 23 41 0d 0a 37 44 39 32 04

Angle: 45 degrees (send broadcast the letter 'B')

01 44 41 54 41 23 30 23 41 53 43 49 49 23 42 0d 0a 37 43 44 32 04

Angle: 90 degrees (send broadcast the letter 'C')

01 44 41 54 41 23 30 23 41 53 43 49 49 23 43 0d 0a 42 43 31 33 04

Angle: 135 degrees (send broadcast the letter 'D')

01 44 41 54 41 23 30 23 41 53 43 49 49 23 44 0d 0a 37 45 35 32 04

Angle: 180 degrees (send broadcast the letter 'E')

01 44 41 54 41 23 30 23 41 53 43 49 49 23 45 0d 0a 42 45 39 33 04

If you send for example the 'R' package you will see the RGB LED turn red, now you have two options, send a 'R' package again to turn off the LED or send another color package, then both color will mix. Experiment and get your own colors.

For more information on how to make the packages needed for LoRa gateway communication read point 3 in this tutorial.


Arduino:

Code:
/*  
 *  LoRa 868 / 915MHz SX1272 Module
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program 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 General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Victor Boria & Luis Miguel Marti
 */
 
// Include the SX1272 and SPI library: 
#include "SX1272.h"
#include <SPI.h>
#include <Servo.h>

int e;
char packet[1];
Servo myServo;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  //Attach servo to pin 9
  myServo.attach(9);

  // Power ON the module
  sx1272.ON();
  
  // Set transmission mode and print the result
  e = sx1272.setMode(4);
  Serial.println(F("Setting Mode: state "));
  Serial.println(e, DEC);
  
  // Select frequency channel
  e = sx1272.setChannel(CH_12_868);
  Serial.println(F("Setting Channel: state "));
  Serial.println(e, DEC);
  
  // Select output power (Max, High or Low)
  e = sx1272.setPower('H');
  Serial.println(F("Setting Power: state "));
  Serial.println(e, DEC);
  
  // Set the node address and print the result
  e = sx1272.setNodeAddress(2);
  Serial.println(F("Setting node address: state "));
  Serial.println(e, DEC);
  
  // Print a success message
  Serial.println(F("SX1272 successfully configured"));
 
}

void loop(void)
{
  // Receive message
  e = sx1272.receivePacketTimeoutACK(5000);  
  if( e == 0 )
  {  
      packet[0] = char(sx1272.packet_received.data[0]);
      Serial.print(packet[0]);
      Serial.print(F("\n"));
    
      if (packet[0]=='A'){
        
        myServo.write(1);
        Serial.println(F("0 degrees"));
       
      }else if(packet[0]=='B'){
        
        myServo.write(45);
        Serial.println(F("45 degrees"));
        
      }else if (packet[0]=='C'){
        
        myServo.write(90);
        Serial.println(F("90 degrees"));
        
      }else if (packet[0]=='D'){
        
        myServo.write(135);
        Serial.println(F("135 degrees"));
        
      }else if (packet[0]=='E'){
        
        myServo.write(179);
        Serial.println(F("180 degrees"));
        
      }
  }
}

        

Raspberry Pi:

Code:
/*  
 *  LoRa 868 / 915MHz SX1272 Module
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program 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 General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Victor Boria & Luis Miguel Marti
 */
 
// Include the SX1272 and SPI library: 
#include "SX1272.h"

int e;
char packet[1];
int servo = 9;
int angle;
unsigned int us;
void servoWrite(int servo, int angle);

void setup()
{ 
  pinMode(servo, OUTPUT);
	 
  // Power ON the module
  sx1272.ON();
   
  // Set transmission mode and print the result
  e = sx1272.setMode(4);
  printf("Setting Mode: state ");
  printf("%d\n",e);
  
  // Select frequency channel
  e = sx1272.setChannel(CH_12_868);
  printf("Setting Channel: state ");
  printf("%d\n",e);
  
  // Select output power (Max, High or Low)
  e = sx1272.setPower('H');
  printf("Setting Power: state ");
  printf("%d\n",e);
  
  // Set the node address and print the result
  e = sx1272.setNodeAddress(2);
  printf("Setting node address: state ");
  printf("%d\n",e);
  
  // Print a success message
  printf("SX1272 successfully configured\n");
}

void loop(void)
{
  // Receive message
  e = sx1272.receivePacketTimeoutACK(5000);  
  if( e == 0 )
  {  
      packet[0] = char(sx1272.packet_received.data[0]);
      printf("%c\n", packet[0]);
    
      if (packet[0]=='A'){
        
        servoWrite(servo, 0); 
        printf("0 degrees\n");
       
      }else if(packet[0]=='B'){
        
        servoWrite(servo, 45); 
        printf("45 degrees\n");
        
      }else if (packet[0]=='C'){
        
        servoWrite(servo, 90); 
        printf("90 degrees\n");
        
      }else if (packet[0]=='D'){
        
        servoWrite(servo, 135); 
        printf("135 degrees\n");
        
      }else if (packet[0]=='E'){
        
        servoWrite(servo, 180); 
        printf("180 degrees\n");
        
      }
  }
}


void servoWrite(int servo, int angle)
{
  for(int i=0; i<20; i++){
    us = (angle*11) + 450;      // Convert angle to microseconds
    digitalWrite(servo, HIGH);
    delayMicroseconds(us);
    digitalWrite(servo, LOW);
    delay(50);                  // Refresh cycle of servo
  }               
}


int main (){
	setup();
	while(1){
		loop();
	}
	return (0);
}

        

Intel Galileo:

Code:
/*  
 *  LoRa 868 / 915MHz SX1272 Module
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program 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 General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Victor Boria & Luis Miguel Marti
 */
 
// Include the SX1272 and SPI library: 
#include "SX1272.h"
#include <SPI.h>
#include <Servo.h>

int e;
char packet[1];
Servo myServo;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  //Attach servo to pin 9
  myServo.attach(9);

  // Power ON the module
  sx1272.ON();
  
  // Set transmission mode and print the result
  e = sx1272.setMode(4);
  Serial.println("Setting Mode: state ");
  Serial.println(e, DEC);
  
  // Select frequency channel
  e = sx1272.setChannel(CH_12_868);
  Serial.println("Setting Channel: state ");
  Serial.println(e, DEC);
  
  // Select output power (Max, High or Low)
  e = sx1272.setPower('H');
  Serial.println("Setting Power: state ");
  Serial.println(e, DEC);
  
  // Set the node address and print the result
  e = sx1272.setNodeAddress(2);
  Serial.println("Setting node address: state ");
  Serial.println(e, DEC);
  
  // Print a success message
  Serial.println("SX1272 successfully configured");
 
}

void loop(void)
{
  // Receive message
  e = sx1272.receivePacketTimeoutACK(5000);  
  if( e == 0 )
  {  
      packet[0] = char(sx1272.packet_received.data[0]);
      Serial.print(packet[0]);
      Serial.print("\n");
    
      if (packet[0]=='A'){
        
        myServo.write(1);
        Serial.println("0 degrees");
       
      }else if(packet[0]=='B'){
        
        myServo.write(45);
        Serial.println("45 degrees");
        
      }else if (packet[0]=='C'){
        
        myServo.write(90);
        Serial.println("90 degrees");
        
      }else if (packet[0]=='D'){
        
        myServo.write(135);
        Serial.println("135 degrees");
        
      }else if (packet[0]=='E'){
        
        myServo.write(179);
        Serial.println("180 degrees");
        
      }
  }
}
        

Links and Documentation

Related Tutorials

Datasheets and Manuals



If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.

Know more at:

Get the Starter Kits at: