Projects

Two-Way Communication with LoRa

Johann Wyss

Issue 23, June 2019

This article includes additional downloadable resources.
Please log in to access.

Log in

Assumed knowledge:

LoRa Weather Station

Issue #21, Page 54

https://diyode.io/021gztq

In Issue 021, we created a long-range LoRa weather station that collected sensor data and transmitted to a LoRa receiver up to 7km away. That project worked very well, but it was only one directional, using just one RX receiver and one TX transmitter.

For this project, we are going to perform bi-directional communication, enabling both units to have the ability to transmit and receive data.

Monitor sensors and control devices over long distances using LoRa technology.

BUILD TIME: 4 HOURS (+ 3D PRINTING TIME)
DIFFICULTY RATING: Intermediate

THE BROAD OVERVIEW

This project allows us to monitor two sensors on a sensor unit and have their on or off state displayed on a base unit, which can be located up to many kilometres away. We can then send a signal back to the sensor unit to trigger a relay, which, in turn, operates a connected device.

To keep things simple, we are going to use two very basic sensors to demonstrate the application. We have chosen a photoresistor (LDR) to detect light and a magnetic reed switch to detect magnetic fields.

The LDR could, for example, be used as a dusk/dawn sensor, to indicate if light has been left on or off, or if an object has crossed in front of the LDR.

The magnetic reed switch could be used to check if a door, window or gate, etc. is open or closed. A magnet is required to activate the reed switch.

Note: We have used a magnetic reed switch module in our design, but we’ve found that hall effect sensors are much more freely available from retailers. The hall effect modules are also more robust compared to the glass encapsulated reed switch. We will describe the project based on your build using a hall effect sensor, which we have listed in the parts list.

You could easily adapt the project for your own needs. For example, you may want to connect it to a water level sensor in a tank, allowing you to remotely activate a water pump to fill or empty the tank. Another application could be detecting if a car is at the front gate, and being able to open or close the gate remotely.

Both sensors are monitored by the Arduino UNO, which uses the LoRa to send a 2-bit binary code wirelessly to the control unit, based on the HIGH or LOW state of each sensor.

We can see the LED on the base unit illuminate to indicate the sensor’s state. We can then press one of the two push buttons on the base unit to trigger a relay on the sensor unit. This relay can be connected to a motor, actuator, pump, light, or similar electronic device.

HOW IT WORKS

SENSOR UNIT

The sensor unit is made up of an Arduino UNO, LoRa module, photoresistor (LDR), Hall Effect sensor, and the relay module.

PHOTORESISTOR

The photoresistor, sometimes referred to as a light dependent resistor, is a variable resistor that varies depending on the amount of light. When used in conjunction with a second resistor, in our case a 10kΩ resistor, we create a voltage divider. The voltage output of this voltage divider varies depending on the amount of light entering the LDR.

The LDR has a resistance of about 1KΩ when in bright light and about 10KΩ when it is in the dark. We can use the voltage divider equation to calculate the expected voltage in either full light or dark.

Vout = Vin × (R2 / R1 + R2)

In our case, R1 is the LDR and R2 is the 10KΩ fixed resistor, therefore in a bright room the signal potential will be calculated to be:

Vout = 5 × (10K / 1K + 10K) = 4.54V

While in the dark, the voltage will be closer to 2.5V as the voltage divider will be of equal values thus simply halving the voltage.

Vout = 5 × (10K / 10K + 10K) = 2.5V

In our circuit, all we need to do is read this voltage via an analogue input pin of the Arduino microcontroller and compare it against a threshold variable that we define. Simply put, we now know that the higher the voltage we read on the analogue pin the higher the ambient light is on the LDR sensor.

HALL EFFECT SENSOR

IMAGE CREDIT: Jaycar

The hall effect sensor is a magnetic switch. When it is in proximity to a strong magnetic field the contacts close, allowing current to flow and pulling the pin of the microcontroller high.

When the hall effect sensor isn’t near a magnet the contacts of the switch is open (Normally Open). In this open state, no current can flow from the 5V source to ground, therefore, the voltage potential at the Hall Junction will be 0V. Once you place a magnet close to the sensor, current will flow through the switch and resistor to ground. The voltage potential between ground and the Hall Junction would be 5V.

To use this as a door ajar sensor, all we need to do is secure a strong neodymium magnet to the door so that when the door is closed the hall sensor and magnet are close together. This way when the door is closed the Hall module 'S' output will be around 5VDC, which will drop to 0V if the door is opened. Our UNO microcontroller will detect this and have the program trigger.

The sensor we used is a fully mechanical reed switch, which is not as common today, however, the hall effect type we suggest in the parts list will work just the same. Just be sure to review the product’s datasheet to verify the pinout. It’s also worth mentioning that the module from Altronics states that the output is pulled to ground when it’s activated, which is the opposite to the Jaycar and Core modules. If this is the case you the code will need a slight modification to correct this discrepancy.

Reed Switch Module

RELAY MODULE

IMAGE CREDIT: Jaycar

Since we are going to want to switch large loads such as lights, motors or pumps, etc., we are going to need to use some circuitry that can handle the higher voltages and or currents. For this task, we are using a relay module. These modules have 250V 10A relays, which means we can use them to switch very large loads. We must point out that legally, only qualified professionals and electricians can work with mains voltages in Australia and some other countries. We will only be switching low power DC loads in our project.

Note: Contact ratings for 'ac' and 'DC' voltage or current can be very different. Get relays to suit your power application. Separation between low voltage and high voltage circuits is very important in ensuring operator safety. Do not use components on the wrong systems or you risk fire or electrocution.

We are using a relay module that has four relays (a 2 relay option was not available) and some circuitry to switch them. The signal from the microcontroller is connected to the IN pins, and from there it goes into an optoisolator. This isolator completely separates the input side from the rest of the circuit, which allows us to control other separate circuits that do not share common potential/ground. The other side of this isolator goes to the base of a transistor. This transistor is connected to the coil of the relay. This means when 5V is applied to the IN pin the transistor applies the VCC power to the relay coil. This coil energises and physically pulls the contacts closed on the normally open pin.

The circuit board also has a flyback diode to protect the module from the back EMF, which is created when the coils magnetic field collapses. Together this circuit allows us to easily and safely use a 5V microcontroller to control significantly higher voltages and loads.

Of course, the relay module is entirely optional, if you’re only wanting to control small 5V loads under 30mA, such as an LED or similar, you can simply power the device directly from the Arduino pins. For moderately higher voltage and current loads up to a few amps, you can easily use a MOSFET.

CONTROLLER UNIT

The controller unit has two LEDs and two switches on the front panel. In our application, we are using the LEDs to indicate the state of a door and light that the sensor unit monitors. The LEDs illuminate based on the last time a signal was received from the sensor unit. The switches allow the user to toggle the state eg. open/closed or on/off.

The switch state is read by the Arduino microcontroller and transmitted to the sensor unit. When the switch is depressed the microcontroller pin will read 5V, and then send this state to the sensor unit.

The Fundamental Build:

Sensor & Controller Prototypes

Parts Required: Jaycar
2 × Arduino Compatible UNOXC4410
2 × LoRa ShieldsXC4392
2 × 5mm Red LEDsZD1690
3 × 5mm Blue LEDsZD0185
2 × PC Mount Toggle Switch-
4 x 330Ω 1/4W Resistors*RR0560
3 x 10k 1/4W Resistors*RR0596
1 × PhotoresistorRD3480
1 × Hall Effect SensorXC4434
OPTIONAL: Jaycar
1 × Relay ModuleXC4440
1 × Strong MagnetZ6327

* Quantity required may be sold in packs. Breadboard and prototyping hardware also required.

Sensor Fritzing diagram with optional relay board attached.

*Our relay example shows a low voltage DC power source to power the light globe and motor. In Australia, a person cannot legally work on voltages above 50Vac without a licence. Any mains wiring is very dangerous for those not qualified and experienced with it. If you intend to use AC then consult a qualified electrician or professional.

Controller Fritzing diagram.

Before making a build on a PCB or perfboard we made a working prototype using breadboard. The following instructions have been provided to replicate the prototype. Take your time and carefully follow the provided Fritzing diagrams. Be sure to insert the LEDs in correctly because these only work one way.

Johann's controller prototype.

THE CODE

The code to program the sender and receiver UNOs is available on the Resources section of our website. We first need to install the LoRa library created by Sandeep Mistry, which you can find here: https://github.com/sandeepmistry/arduino-LoRa

In fact, due to its immense popularity, this library can now be downloaded directly from the Arduino IDE library manager. Just search for LoRa in the library manager and find LoRa by Sandeep Mistry. This way you will be sure to have the most recent version possible.

The sketch also uses the SPI library, however, this library forms part of the base Arduino environment and as such does not need to be installed.

TESTING

Connect power to the sensor module from either a computer, USB mains power supply or a powerbank. We used a powerbank in our prototype so we could also test the distance between it and the controller.

If you cover the LDR, the LED on the controller should toggle.

Likewise, if you bring a strong field magnet, such as a rare Earth magnet, close to the Hall Effect sensor / Reed switch, the corresponding LED on the controller will toggle.

If the LEDs on the Controller turn on or off when you perform the above two tests, then this proves that you have wireless communications from the sensor module to the controller. If not, go over all of your connections again.

Next, you want to test the communications from the controller to the sensor module. Pressing the buttons on the controller should toggle the corresponding LEDs on the sensor unit. Bear in mind, however, that there will be a few seconds of delay as the program does not transmit and receive live data, it waits for a specific time and then transmits.

The Main Build:

Sensor & Controller

ELECTRONICS

After proving the concept and verifying the device functions using our broadboard prototypes, we can now build something more permanent. We used perfboard to mount the components and attach the LoRa shield via pin headers. We will also include PCB files online if you want to make your own or send to a PCB making service. The following instructions and images show you how to construct the circuits on a perfboard.

Take extra care when securing the male headers, we recommend aligning them to the perfboard with the long end facing the PCB. This will give you sufficient room to solder the pin to the PCB. Make sure you solder each of the pins to multiple pads as the force of inserting and removing the PCB from the LoRa shield may be enough to cause the pads to lift.

Additional Parts Required:Jaycar
2 × 5cm × 7cm Perfboards-
1 × 40 Way Pin HeadersHM3211
1 × 40 Way Header SocketHM3230
1 × 2.5mm Bulkhead DC SocketPS0524
2 × M3 Machine ScrewsHP0554
2 × 2 Way Screw Terminal BlocksHM3130
OPTIONAL:Jaycar
2 × Panel Mount Toggle SwitchesSP0718--SP0718
2 × 5mm LED BezelsHP1102H1553-HP1102
1 × Male DC Jack for Testing-P0604--
1 × 330Ω 1/4W Resistors*RR0560R7546PRT-14490RR0560
1 × 5mm Green LEDZD0170Z0001CE05104*ZD0170

* Quantity required may be sold in packs. General purpose hook-up wire required.

Sensor Schematic with relay module.
Controller Schematic.

After soldering the pin headers, you can simply push the plastic retainer down toward the PCB exposing enough pin to insert into the LoRa shields female socket. When you’re done it should look something like the image here. Note that each pin has been soldered to three pads, this will give the header the required mechanical strength for repeated insertions. It’s also worth mentioning that you should triple check the spacing between the headers prior to soldering.

We found the best way to do this was to insert the header into the LoRa socket and align the PCB onto the exposed pins. This will help avoid accidentally soldering the headers into the incorrect holes and therefore incorrect spacing.

We soldered a header to the PCB so that the Hall Effect sensor could easily be wired remotely or just inserted into the PCB header. If you’re intending on mounting the sensor remotely we recommend using a male header to mate with the female header on the PCB and attaching it to the sensor with a suitable length of wire.

For the controller unit, we also created a perfboard shield. If you intend to house it in an enclosure the perfboard isn’t necessary as the small number of components can easily just be attached via jumper wires.

The tracks of the PCB were made using a combination of techniques; some were made by simply bending the component leads and soldering them to each of the solder pads. However, most were made by using a continuous solder bridge between two pads. This is done by first soldering each solder pad you intend to use with a bit of solder.

After you create the path of the track you simply connect two solder pads together along the path and move to the next path leaving a gap between each two soldered pads. This gap is important as the solder needs to cool back down and solidify, otherwise, it will be very hard to control.

The final step is to bridge the remaining gaps until you have one continuous track of solder as shown here. It may take a little practice, but this is by far the easiest and neatest way to make connections on perfboard PCBs. Just note that you’re using quite a bit of solder so it’s important to have good ventilation and to also be mindful that you’re using a substantial amount of heat so care is needed.

Once you’re done soldering all of the components the PCBs will look something like this.

We used the screw terminals on the sensor PCB to easily allow connection to the relay module.

3D PRINTED ENCLOSURE

CASE

The final step is to build an enclosure to hold and protect the electronics. The controller unit is designed to be placed on a desk, as such it needs to be enclosed to prevent damage. It is printed in three parts; base, lid and panel.

The main base was printed in Flashforge white PLA at 300-micron layer height and took around 6 hours to print.

The sensor unit will remain open to provide easy access to the relays and sensors, etc, We use a simple design that will allow all of the components to be mounted securely together.

FRONT PANEL

The front panel was printed with a single extruder. The two-tone colour was achieved by printing the first 2.5mm in white PLA and then switching filament to black PLA to finish the print. This can easily be done in most slicers or you can simply pause the print and replace the filament at your chosen time. Either method works fine and allows you to create some awesome front panels.

All up, the front panel took about 2 hours to print at a 100-micron layer height using a Flashforge Creator Pro.

FEET

The feet were printed in TPU rubber at a layer height of 100-microns. A very slow printing speed of 20mm/sec is recommended, which takes about 6 minutes to print each foot.

We printed them separately so that if one was to fail we didn’t lose the entire four of them. Of course, you don’t need to print the feet with flexible filament, PLA will work fine.

After printing the feet, they should be secured to the main base with a drop of epoxy or another suitable adhesive.

ENCLOSURE

We didn’t design an enclosure for the sensor unit due to the hardware being designed for customisation. However, we will include a blank front cover that you can import into a program such as a Tinkercad. This way you can easily modify the enclosure to allow for different sensors, wiring, etc.

Simply import the .stl file into Tinkercad and drag in the shape corresponding to the hole you wish to make in the front panel. Turn the shape into a hole and from there you can resize the shape to match the dimensions you need.

Once you have the cutouts you require you simply group the items with ctrl + g, which will result in a single object with the desired cutouts.

Doing this will allow you to modify both the sensor module and the controller front panels. This enables you to modify the functionality of the device to suit your needs without needing to start from scratch designing an enclosure.

We designed the enclosure to be large enough to add various components, batteries, etc. This saves you from having to modify the enclosure to fit things in. Also, note if your device is designed to be housed outside, you will need to take steps to protect the electronics from water ingress. As with the previous LoRa project, we recommend using a conformal coating to protect the exposed electronics and sealing the enclosure as best as you can.

ASSEMBLY

To assemble the controller unit, you need to wire the DC jack to the DC input on the Arduino UNO. Refer to the switch’s datasheet for the pinout, or use a multimeter if necessary to work out which is the centre pin and which is the negative pin. If in doubt you could use the same technique we use by using a DC plug to screw terminal adaptor.

Like you see here, we use a multimeter in continuity mode to check which pin is positive and which pin is negative.

Make 100% sure that positive goes to positive and negative to negative between the DC socket and the UNO. Getting this around the wrong way may destroy your Arduino and the LoRa shield, so triple check this.

To check that it works, connect a 7-12VDC power supply to the DC jack. Do this with the LoRa shield removed. If everything powers up correctly you can move on to the next step. If it does not power up double check your connections.

Now we want to assemble the front panel by first inserting the LEDs with their bezels, along with the two push buttons. Secure the switches in place using their washer and nut.

We now want to make all of the connections from the LEDs and switches to the Arduino UNO and LoRa shield.

An easy way to do this is by using breadboard jumper leads with the pin removed from one end. Cut the pin from one end, strip off a little of the insulation, and then tin the end with solder. A third-hand comes in handy for this. You need to make 7 leads:

  • 2 for Ground (Use black preferably)
  • 1 for 5V (Use red preferably)
  • 2 for the switches
  • 2 for the LEDs

Solder the two 330Ω resistors to the cathodes (short leg) of the LEDs and the two 10kΩ resistors to one terminal of each switch.

Trim each LED anode (longer leg) a little, and then solder one of our pre-made cables to the anode leg of both LED.

These wires will go to the corresponding digital pin of the Arduino.

Use a small length of wire to connect the two 10kΩ resistors so that we can attach a single ground wire to them both. Join the two remaining sides of the switch together using another piece of wire. This will be our 5V connection.

We recommend that you use heatshrink or electrical tape to prevent shorts.

Finally, we want to solder the remaining two wires to the switches. To do this, we simply tin the wires and solder them to the resistor side of the switch.

After connecting all of the wires to the front panel, you need to slot the pin header into the corresponding pin on the Arduino i.e. the 5V to the 5V pin, the LEDs to digital pin 3 and 4 and the switches to analogue pins A0 and A1.

Once everything is connected take some time to ensure there is no exposed wiring and cable tie the wires together to keep it neat.

Now you simply want to insert the DC jack into the hole in the case (You may need a hobby knife to remove the support material we designed into the case). Secure into place with the provided nut and lock washer.

Next, insert the I-PEX antenna connector into the antenna hole. Due to limited space requirements, and the 90° antenna on the LoRa shield, there is no way to secure the Arduino with screws.

We chose to secure the Arduino into place using a few dabs of hot glue, which can easily be removed later with a little Isopropyl alcohol.

Finally, attach the front panel to the base using a couple of M3 machine screws. Repeat the relevant assembly for the sensor unit to suit your required hardware and mounting requirements.

TESTING

We recommend testing the controller and sensor units in the same room first, before you place your sensor at the other end of the house or property.

We tested our units in our lab by connecting a buzzer to the relay outputs. The buzzer would beep if the door to the CNC mill was left open or the light left on. We simply glued a neodymium magnet to the mill door and positioned the sensor close to it. You may also notice that we added a green LED with 330Ω resistor to the Perfboard so we could see from a distance if the door was open or closed.

Apply power to both the sensor and control unit. You should see the lights on the Arduino's light up. Shortly after, the control LEDs or sensor LEDs for the light sensor or Hall Effect sensor should illuminate, depending on their state.

You should then be able to actuate the switches on the control unit, which will toggle the LEDs on the sensor unit and thus trigger the relay or device you have attached.

If you read our LoRa Weather Station project from Issue 021, we were able to achieve an unobstructed radio line-of-sight over 7km. In theory, you should be able to place your sensor up to that distance. Distance varies with terrain, environmental noise, and other users in the proximity.

WHERE TO FROM HERE?

As mentioned in the introduction, this project can work with many different types of sensors for different applications. Monitor water levels using a moisture sensor and have the device allow to actuate a pump remotely if the sensor is dry filling a water trough, etc.

You could add a level of automation. For example, have a sensor to detect if the water level is low and another sensor to detect if it’s high. This way you could have the pump automatically shut off if the trough is filled.