Modular codePostsVision

Connecting a accelerometer to Particle Argon and sending on/off signals over RTW

In this tutorial I will show you how to connect a accelerometer to Particle Argon and using curl to send on/off commands over RTW. I will be using the MPU6050 accelerometer, which is a module that measures the force that is applied to the XYZ-axsises of the device it also has a gyro but we will not use this function in this example.


1 x Particle Argon

1 x MPU6050

4 x Wire


The MPU6050 has 8 pins which includes Vcc, Gnd, SCL and SDA.

Vcc Vcc



#include "MPU6050.h"
#include <cmath>
int ledPin = D7;

// MPU variables:
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;

//Variables to save states
int lastZ = 0;
int lastS = false;

//Initiation of the RTWparticle libary(No parameters passed since we are not using the recive capabilitys of the libary)
RTWparticle rtwParticle("", "", "", "", "");

void setup() {
//Calling Wire.begin() to be able to use the sda and scl capabilitys of the particle
//Starting the accelgyrofunction.


void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

//Setting the lastZ if it is not yet set
if(lastZ == 0){
lastZ = az;

//Setting the senstivity to 2000 so that there will be some force applied to the device to trigger.
int sensitivity = 2000;

//Calculating the treshold when the action should be triggered 
int nowZMi = az - sensitivity;
//Defining string for rtwTxt
String rtwTxt;

//Checking if lastZ is lower than the minimum trigger value
if(lastZ < nowZMi){
//Checking if last state is false if it is it sets rtwTxt to 1 and last state to true if not it does the oposite
lastS = true;
rtwTxt = "1";
lastS = false;
rtwTxt = "0";
//Sending rtw message
rtwParticle.sendRTW("ServiceName", "ObjectID", "User", "password", "onOff=" + rtwTxt, "Domain");
//Delay to avoid double registration

Related posts
IntegrationPostsThe Things Network

How we integrated an external service to RTW using Webhooks and PhP


Code used to connect to WIFI with Esp32 (Referenced as WifiCode)


Automatic trash can opener based on distance


Automatic adjust brightness according to how bright the environment is

Legg igjen en kommentar

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *