Lora Shield arduino Tutorial

LORA with arduino tutorial

We will practice how to use LORA shield with arduino UNO. You’ll need two arduinos, and two LORA shield. And don’t forget the USB cable.

A pair of LORA and a pair of arduino

Attach the shield to the arduino and you ready to program the arduino.

You have to upload every sketch for every arduino. There will be a client and a server. The client we use to send data, and the server will receive the data then gives reply.

In this tutorial we will use RadioHead library for LORA. you can download the library here.

Before we start the arduino program, we have to edit the frequency in library according to our LORA. In this case I use the 433Mhz. So I will open the Radiohead library and find file named RH_RF95.cpp. Find and Edit the setFrequency function to 433.0

// An innocuous ISM frequency, same as RF22's
    setFrequency(433.0);

After edit, save the file. and you are ready to open the arduino IDE.

This is the code for the client :

#include <SPI.h>
#include <RH_RF95.h>

// Singleton instance of the radio driver
RH_RF95 rf95;
//RH_RF95 rf95(5, 2); // Rocket Scream Mini Ultra Pro with the RFM95W
//RH_RF95 rf95(8, 3); // Adafruit Feather M0 with RFM95

// Need this on Arduino Zero with SerialUSB port (eg RocketScream Mini Ultra Pro)
//#define Serial SerialUSB

void setup()
{
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
  //  driver.setTxPower(14, true);
}

void loop()
{
  Serial.println("Sending to rf95_server");
  // Send a message to rf95_server
  uint8_t data[] = "Hello I'm from lora!";
  rf95.send(data, sizeof(data));

  rf95.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (rf95.waitAvailableTimeout(3000))
  {
    // Should be a reply message for us now
    if (rf95.recv(buf, &len))
    {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is rf95_server running?");
  }
  delay(400);
}

And this is for the server (another arduino) :

#include <SPI.h>
#include <RH_RF95.h>

// Singleton instance of the radio driver
RH_RF95 rf95;
//RH_RF95 rf95(5, 2); // Rocket Scream Mini Ultra Pro with the RFM95W
//RH_RF95 rf95(8, 3); // Adafruit Feather M0 with RFM95 

// Need this on Arduino Zero with SerialUSB port (eg RocketScream Mini Ultra Pro)
//#define Serial SerialUSB

int led = 9;

void setup() 
{
  // Rocket Scream Mini Ultra Pro with the RFM95W only:
  // Ensure serial flash is not interfering with radio communication on SPI bus
//  pinMode(4, OUTPUT);
//  digitalWrite(4, HIGH);

  pinMode(led, OUTPUT);     
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");  
  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

  // The default transmitter power is 13dBm, using PA_BOOST.
  // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then 
  // you can set transmitter powers from 5 to 23 dBm:
//  driver.setTxPower(23, false);
  // If you are using Modtronix inAir4 or inAir9,or any other module which uses the
  // transmitter RFO pins and not the PA_BOOST pins
  // then you can configure the power transmitter power for -1 to 14 dBm and with useRFO true. 
  // Failure to do that will result in extremely low transmit powers.
//  driver.setTxPower(14, true);
}

void loop()
{
  if (rf95.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf95.recv(buf, &len))
    {
      digitalWrite(led, HIGH);
//      RH_RF95::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);
//      Serial.print("RSSI: ");
//      Serial.println(rf95.lastRssi(), DEC);
      
      // Send a reply
      uint8_t data[] = "And hello back to you";
      rf95.send(data, sizeof(data));
      rf95.waitPacketSent();
      Serial.println("Sent a reply");
       digitalWrite(led, LOW);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

And if you want to send integer sensor data you can edit the data and use the code below :

#include <SPI.h>
#include <RH_RF95.h>

// Singleton instance of the radio driver
RH_RF95 rf95;
//RH_RF95 rf95(5, 2); // Rocket Scream Mini Ultra Pro with the RFM95W
//RH_RF95 rf95(8, 3); // Adafruit Feather M0 with RFM95

// Need this on Arduino Zero with SerialUSB port (eg RocketScream Mini Ultra Pro)
//#define Serial SerialUSB

void setup()
{
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
  //  driver.setTxPower(14, true);
}

void loop()
{
  Serial.println("Sending to rf95_server");
  // Send a message to rf95_server
  //We change the data we want to send with sensor_value
  int sensor_value = analogRead(A0);
  Serial.println("Sensor Value : "+(String)sensor_value);
  char data[3];
  itoa(sensor_value, data, 10);
  rf95.send(data, sizeof(data));

  rf95.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (rf95.waitAvailableTimeout(3000))
  {
    // Should be a reply message for us now
    if (rf95.recv(buf, &len))
    {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is rf95_server running?");
  }
  delay(400);
}

And if you want to watch the complete tutorial, you can watch the video below :

Easy Pulse Plugin Heartbeat Sensor with arduino Tutorial

easy pulse plugin sensor

There are some heartbeat sensor in market with cheap price. I’ve used three of them, that is heart rate from pulsesensor.com, max30100 and easy pulse Plugin heartbeart sensor. In my opinion, the most stable is easy pulse plugin. Maybe because it comes with a shell or cover. That make minimum interference from outside. Different with heart rate sensor form pulsesensor.com sometimes it’s has different output amplitude that make it harder to get stable value. It has many factor that make that unstable.

easy pulse plugin sensor
easy pulse plugin sensor

In this tutorial we will only examine how to use Easy Pulse Plugin Heartbeat Sensor with an arduino. This sensor has an analog output just like sensor from pulsesensor.com. Before we go far how to use this sensor, we will examine how this sensor works. Little basic theory about this sensor.

How Easy Pulse Plugin Sensor Works?

Easy Pulse Plugin is an open-source sensor based on photoplethysmography principle (PPG). It is non-invasive to measure the cadiovascular pulse wave by detect the blood volume changes in blood vessels. This sensor uses a infrared light emitting diode (IR-LED) and a photodetector.

photoplethysmography sensor
photoplethysmography sensor

Look at picture above, there is an IR-LED as transmitter. This LED will transmit an infrared light to the photo sensor. But if we place our finger between them, the photo sensor will receive only the light left after absorbed by finger. And the thing is, the light absorbed by finger is vary according to the blood. The light intensity that up to the photo sensor is related to the changes in blood volume inside the tissue.



Analog output from this sensor will form a wave that related to the beat of the heartbeat. it will rise when there’s a beat.

beat wave
beat wave

Using it with arduino

easy pulse plugin sensor with arduino wiring
easy pulse plugin sensor with arduino wiring

You can choose to use 5V or 3.3V for power of this sensor. To choose simply move the jumper in top left corner (yellow circle in picture above). And you can also choose the output to out at A0 or A1 (move the jumper in green circle).

Connect this sensor to arduino just like picture above.

5v -> 5v (if using 5V module)

GND -> GND

A0 -> A0 (if choose A0 as output).

Or you can just directly connect the sensor to the arduino UNO. Just like the picture below. The module is designed can connect directly. Of course if you don’t need another power pin.

arduino uno with easy pulse plugin
arduino uno with easy pulse plugin

The arduino code

In beat wave picture above is the picture of wave created by the beats from sensor when a finger placed in the sensor. The peak made when a beat is sensed by the sensor. To get the wave like in picture above you can use the sample code from arduino named analogReadSerial  with little modification. The modification should give longer delay. Or you can just copy the code below :

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(10);        // delay in between reads for stability
}

I modify the delay so it will only read the sensorValue every 10ms. Upload code above and open your Serial Plotter. Open it from tools -> Serial Plotter.



If we want to make a heart rate device we can measure by count how many peak from the sensor in one minute. To do this you need to define your threshold first. So you can decide where the beats happen. Look at picture below for example

beat threshold
beat threshold

I will give the threshold for my beats at 450 value of ADC. So every adc reach more than 450 I will count it as a beat.  And because I consider measure beats per minute will take a long time when it’s really count for one minute, I will just count for 20 seconds. And the result will multiply by 3. So it is just like 60 seconds or one minute.


int adc;
boolean counter;
int count;
unsigned long millisBefore;
unsigned long beatTime=20000;
const int threshold=450;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  counter = true;
  millisBefore = millis();
}

void loop() {
  // put your main code here, to run repeatedly:
  adc = analogRead(A0);
  Serial.println(adc);
  delay(10);

  if ((millis() - millisBefore) < beatTime) {
    if (counter == true) {
      if (adc >= threshold) {
        count++;
        counter = false;
        Serial.print("Beat : ");
        Serial.println(count);
      }
    }
    if (adc < threshold) {
      counter = true;
    }
  } else {
    Serial.print(count*(60000/beatTime));
    Serial.println(" BPM");
    count=0;
    millisBefore = millis();
  }
}

The code above will print number every beat detected and will print the beats per minute every 20 seconds. You can edit how long to get count the beat by edit variable beatTime. 20000 means 20 seconds, because it is in miliseconds. And of course you can edit the threshold too by edit number in threshold variable.

Non Contact Temperature Sensor MLX90614 With Arduino Tutorial

MLX90614 contactless temperature sensor

One of solution to build sensor system that can measure high temperature without damage the system is using contactless or non-contact temperature sensor. This sensor can sense temperature of an object without touch the object.


Mlx90614 is a infrared based sensor, it measure the temperature based on infrared emitted by an object. It senses electromagnetic waves in the range about 700 nm to 14,000 nm.

Mlx90614 is a great cheap sensor that cost under 10$.

""

Sensor Spesification

These are some good feature of this sensor taken from it’s datasheet.

  • Small size, low cost
  • Factory calibrated in wide temperature range:
    40 to 125 °C for sensor temperature and
    70 to 380 °C for object temperature.
  • High accuracy of 0.5°C over wide temperature
    range (0..+50°C for both Ta and To)
  • Measurement resolution of 0.02°C
  • Available in 3V and 5V versions

Wiring Diagram

wiring diagram mlx90614 and arduino
wiring diagram mlx90614 and arduino

Vin -> 3.3V

GND -> GND

SCL -> SCL or A5

SDA -> SDA or A4



You can choose to connect sensor I²C pin to arduino SCL, SDA or arduino pin A4,A5 they have the same function. Make sure use 3.3V for 3.3V module type. Because it will damage if connected to 5V. I damaged mine twice.

MLX90614 With Arduino Code

Before you start the code, make sure you have the library. Download library from adafruit here.

You can get the sample code to measure temperature using the library sample or copy code below :

/*************************************************** 
  This is a library example for the MLX90614 Temp Sensor

  Designed specifically to work with the MLX90614 sensors in the
  adafruit shop
  ----> https://www.adafruit.com/products/1748
  ----> https://www.adafruit.com/products/1749

  These sensors use I2C to communicate, 2 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

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

  Serial.println("Adafruit MLX90614 test");  

  mlx.begin();  
}

void loop() {
  Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC()); 
  Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C");
  Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempF()); 
  Serial.print("*F\tObject = "); Serial.print(mlx.readObjectTempF()); Serial.println("*F");

  Serial.println();
  delay(500);
}

Paste the code above to your arduino IDE and upload. After done uploading, you can test the sensor. It should works now!. You can measure an object temperature by face the sensor toward the object.



Watch the full video tutorial below :

How to convert resistance to voltage

convert resistance to voltage

convert resistance to voltage
convert resistance to voltage

Converting resistance to voltage in electronics sometimes are so important. This happens because many electronics device such as microcontroller only can read voltage as analog input signal. Analog signal then will be converted to digital using ADC. But before we talk about ADC or microcontoller, how if our sensor has resistance output instead of voltage?

Voltage Divider as resistance to voltage converter

The only thing we can do to make our sensor readable by microcontroller is convert resistance to voltage. Converting a resistance to voltage is far more easier than converting to other quantity. So, how we do that?

The answer is very simple. Use voltage divider !

Voltage divider has capability to divide the voltage by comparison of two resistance. You can read more details about voltage divider here.

Application Example

I use an LDR for a light sensor. LDR has linear resistance to the light that exposed to it surface. When light getting brighter, the resistance will decreased. Otherwise when light brightness dercreases the resistance will increases. Here’s my schematic example:

voltage divider with ldr
voltage divider with ldr

The output voltage will be :

voltage divider
voltage divider

In this example, when light density is changed, the LDR resistance will changed too and of course Voltage output will changed accordingly.

Since the voltage will change in every change of light density, then you can connect this voltage output to an microcontroller ADC.

Little Tips

Pick the right R2 has a little trick. You have to choose the closest one to LDR reistance range to make the voltage output difference better. For example, if your LDR resistance will vary between 800 ohm to 1k ohm then used 1k ohm resistor as R2 will be ok. But if you has LDR with resistance output vary from 8k-10k don’t use 1k as R2, using 10k as R2 will better.

Understanding Voltage Divider

voltage divider

Just like the term, voltage divider means dividing the voltage into smaller value. Sometimes this is very useful if we had only measurement instrument with limited maximum value. Such as pocket oscilloscope.

Understanding Voltage Divider

Dividing voltage is very simple job but very powerful. We just need two resistor to do it. Here’s the example how we do it.

voltage divider
voltage divider

Consider we have 10V voltage, using two resistor will result 5v at the output. This is how we calculate it :

Vout = Vin * R1/(R1+R2)

So if we want to divide the voltage by 3. You can simply change the R2 to 2k. So the output will be :

Vout = 10 * 1k/(1k+2k) = 3,333 V

Voltage Divider Application Example

In some cases, voltage divider is very useful for signal conditioning. For example, you have to sense a voltage from a power supply using an arduino or other microcontroller. The power supply can generate voltage from 0V-50V. Unfortunately, your arduino can sense voltage only from 0V-5V. That’s why voltage divider come in handy. You can just divide voltage from power supply by 10. So the output will only produce voltage between 0V to 5V and you can read using arduino ADC easily.

Beside divide the voltage, you can use voltage divider to convert resistance based sensor to voltage. For example a light dependent resistor (LDR).

voltage divider with ldr
voltage divider with ldr

Take a look at picture above. We already know that LDR will vary the resistance by how much light that exposed to it. This will make easier to read voltage output from different light brightness.

Voltage Divider is not for Power Supply

If you are new to electronics and think to use voltage divider for power supply then you are wrong. Why not for power supply? Voltage divider works with big resistors, so it will produce very small current. Small current cannot used for power supply. You can user linear regulator or switching power supply instead.

How to use Thermocouple type K (MAX6675) with arduino

max6675 with thermocouple type K

Thermocouple is powerful temperature sensor. In this case, using thermocouple type K and MAX6675 can read temperature in range 0ºC to 1024ºC,  this sensos also waterproof. So it’s okay if you want place this sensor in water for measure water temperature. Beside that, thermocouple has tons of module that you can get easily, not like RTD which usually we have to make signal conditioning by ourselves. Sometimes it is really annoying.

max6675 with thermocouple type K
max6675 with thermocouple type K

And, let’s get dirty!

First wire your max6675 to arduino. In this example I use arduino UNO.

Pin SCK -> 6

Pin CS -> 5

Pin SO -> 4

Pin VCC-> 5V

Pin GND -> GND

 

Gambar koneksi

 

Double check your wiring, make sure everythings is connected correctly.

Now, it’s time to code.

Before we start code, to make our life easier we can use a library. As example we can use a library from adafruit. You can download here.

After you download the library, copy the folder to your arduino directory/library. It is usually in C:\program files (x86)\arduino\library. If your arduino software is running when copying the library. Make sure you restart the software after add the library to let arduino recognized new library.

Hmmm now it’s real time to code. Haha

You can use code below, or you can find this code at library example.

 #include "max6675.h"

int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
 
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;
 
void setup() {
 Serial.begin(9600);
 // use Arduino pins 
 pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
 pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
 
 Serial.println("MAX6675 test");
 // wait for MAX chip to stabilize
 delay(500);
}

void loop() {
 // basic readout test, just print the current temp 
 Serial.print("C = "); 
 Serial.println(thermocouple.readCelsius());
 Serial.print("F = ");
 Serial.println(thermocouple.readFahrenheit());
 delay(1000);
}

 

The result :

Gambar contoh hasil