Thursday, May 18, 2023

0000 0000 1100 1010

The Dirty Dozen - Part Six: USB-C Nano, LIS3DH & Channel Update

The packet reveals an Arduino Nano? Hardly exciting, although I'd never had a Nano with a USBC interface before, but still it wasn't exactly a challenging unit!

So I dived backward to Dirty Dozen #5, dragged out the LIS3DH and interrogated it's temperature abilities. It's a little unusual in that the unit only reports the relative temperature based on when it was booted - strange!

I decided to use that with three LEDs to indicate relative temperature - here is the code:

/*
  Based on ADCUsage.ino

  Marshall Taylor @ SparkFun Electronics
  Nov 16, 2016
  https://github.com/sparkfun/LIS3DH_Breakout
  https://github.com/sparkfun/SparkFun_LIS3DH_Arduino_Library

  OneCircuit https://www.youtube.com/@onecircuit-as
  Mon 15 May 2023 11:02:06 AEST

*/

#include "SparkFunLIS3DH.h"
#include "Wire.h"

LIS3DH myIMU;
int myroomtemp = 0;
int currenttemp = 0;
int difftemp = 0;
#define blueled 4
#define greenled 3
#define yellowled 2

void setup() {

  Serial.begin(57600);
  delay(1000);
  pinMode(blueled, OUTPUT);
  pinMode(greenled, OUTPUT);
  pinMode(yellowled, OUTPUT);
  myIMU.settings.adcEnabled = 1;
  myIMU.settings.tempEnabled = 1; // from ADC3
  myIMU.begin();

  // take the temp
  for (byte stable = 0; stable < 5; stable++) {
  myroomtemp = myIMU.read10bitADC3(); // temp
  delay(100);    
  }
}

void loop()
{
  currenttemp = myIMU.read10bitADC3();
  difftemp = myroomtemp - currenttemp;

  Serial.println(difftemp);

  if (difftemp < 0) {
    digitalWrite(blueled, HIGH);
    digitalWrite(greenled, LOW);
    digitalWrite(yellowled, LOW);
  }
  if ((difftemp > 2) && (difftemp < 20)) {
    digitalWrite(blueled, LOW);
    digitalWrite(greenled, HIGH);
    digitalWrite(yellowled, LOW);
  }
  if (difftemp > 22) {
    digitalWrite(blueled, LOW);
    digitalWrite(greenled, LOW);
    digitalWrite(yellowled, HIGH);
  }
  delay(100);
}

It worked well and I can see some definite applications (e.g. is my module overheating!)

Some news as well in this video - we are moving house after 7 years. It is just down a road a bit, but the packing up and unpacking of the lab will take a few weeks.

If I get a moment I will try to post a video or two - but I might also go a bit "dark" over the Tasmanian winter.

Apologies if you have just joined the channel and blog - I hope the back catalogue of 200 videos can keep you busy while I rearrange my life!

Take care and see you on the other side.



Friday, May 12, 2023

0000 0000 1100 1001

200 videos - no way!

Nearly four years ago I started (belatedly) a blog and YT channel in order to bring some semblance of order to the chaos of my online electronics shopping addiction!

Some of the projects have been educative (for me!), some dismal failures (the great ferrules disaster of 2022), some excellent solutions for issues around the house (lighting, timing and temperature) and all of them a privilege and a joy.

They are all my children and of course I'm not supposed to have favourites, but I have cobbled together a 5+2 "best of" for the video below.

Thank you for your company on this journey - and I hope you'll stick around for the next 200! <gulp>



Friday, May 5, 2023

0000 0000 1100 1000

The Dirty Dozen - Part Five - 3 axes accelerometer as Fall Detector

Those of us who fall off things may want immediate help or maybe even notifications sent to relevant authorities. Perhaps you have a relative or friend, prone to falling, who needs some gentle silicon monitoring?

The LIS3DH Triaxial Acceleration Temperature Sensor might be just the ticket. When I pulled it out of the Dirty Dozen box I thought immediately that if I could talk to the thing and get some sense out of it then I'd take a crack at a fall detector.

The theory is that if there is sudden acceleration of the module (or the object in which it is contained) then the device would output, in three dimensions, the magnitude of that "overall" event.

To characterise it as a single event I thought I might use a virtual 3D box and plot values from the ideal position (no acceleration for zero) to a calculated threshold value (found in my case by experimentation).


That High School math sure does come in handy some days!

I went looking for all sorts of information on this module only to discover that I think it's a cheap knockoff of an existing product (surprise!).

Using a fair amount of optomistic pixie dust I sprinkled in some code from the legitimate product found here at this link.

After successfully "talking" to the module with the legit code and library, I then embarked upon coding the fake version. It took a bit longer than expected as my perfection gene kicked in halfway through the coding process.

/*
   based on code by Marshall Taylor @ SparkFun Electronics
   https://github.com/sparkfun/LIS3DH_Breakout
   https://github.com/sparkfun/SparkFun_LIS3DH_Arduino_Library

   OneCircuit https://www.youtube.com/@onecircuit-as
   Tue 18 Apr 2023 14:03:27 AEST
*/

#include "SparkFunLIS3DH.h"
#include "Wire.h"
#include "SPI.h"

#define alarmLED 4

float xmove = 0;
float ymove = 0;
float zmove = 0;
float falldetect = 0;   // how hard is fall?
boolean fell = false;
float biggestfall = 0;
int falltrigger = 300;  // how hard to fall?
long timer = 0;
long currenttime = 0;
long bigfalltime = 5000;
LIS3DH myIMU(I2C_MODE, 0x19);

void setup() {
  Serial.begin(57600);
  pinMode(alarmLED, OUTPUT);

  myIMU.settings.adcEnabled = 1;
  myIMU.settings.tempEnabled = 0;
  // in Hz: 0,1,10,25,50,100,200,400,1600,5000
  myIMU.settings.accelSampleRate = 200;
  // Max G force: 2, 4, 8, 16
  myIMU.settings.accelRange = 16;
  myIMU.settings.xAccelEnabled = 1;
  myIMU.settings.yAccelEnabled = 1;
  myIMU.settings.zAccelEnabled = 1;

  myIMU.begin();
  delay(1000); // settle petal

}

// here is where you would txt, email or call for help
void raisealarm() {
  digitalWrite(alarmLED, HIGH);
  fell = true;
  timer = millis();
}

void loop()
{
  // time at start of loop
  currenttime = millis();

  // measure and convert each axis
  xmove = abs(myIMU.readFloatAccelX() * 100);
  ymove = abs(myIMU.readFloatAccelY() * 100) - 5;
  zmove = abs(myIMU.readFloatAccelZ() * 100) - 93;

  // has the device fallen?
  falldetect = sqrt(xmove * xmove + ymove * ymove + zmove * zmove);
  if (falldetect > falltrigger) {
    raisealarm();
  }

  Serial.print(xmove);
  Serial.print(",");
  Serial.print(ymove);
  Serial.print(",");
  Serial.print(zmove);
  Serial.print(",");
  Serial.print(falldetect);
  Serial.print(",");
  Serial.println(biggestfall);

  if (falldetect > biggestfall) {
    biggestfall = falldetect;
  }

  if (((currenttime - timer) > bigfalltime) && fell) {
    digitalWrite(alarmLED, LOW);
    fell = false;
    biggestfall = 0;
    timer = 0;
  }

  delay(20);
}

The results were, I don't mind saying, pretty impressive! I'd like to maybe further develop this project to include an "IoT" twist - such that maybe fall detection is accompanied by a text or email message.

Your thoughts, as always, appreciated!