Sesh’o’meter – Using tilt sensor and humidity sensor to display messages based on movement and sweatiness

Introduction:

What can I do with my DHT11 temp and humidity sensor?

I have been looking online for projects involving this sensor, but all the projects I found revolved around using the sensor in automatic garden care devices

I wanted to do something different…

It dawned on me that I could use the sensor to monitor skin humidity…                            aka sweatiness. With this in mind, me and Chris thought about what we could do with this information and settled on trying to produce a funny piece of wearable tech.

Since Me and Chris are both fans of Dub, DnB and other bass heavy types of music, we thought it would be funny to make a device reacted to how hard the wearer was SKANKING.

Chris had selected a tilt sensor which we decided would work well with the DHT11.

Literature Review:

The basic premise of using the DHT sensor to measure skin humidity hasn’t been covered before but I had read generally about the sensor, as well as displaying it’s output on a LCD screen.

The Guide I read was:

http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-an-arduino/

I also used this for the LCD code.

On top of this, I read and re-read the reference section of the arduino website in order to familiarise myself with the programming language.

 

Hypothesis

Following my previous investigations using various displays with the DHT sensor, I felt it would be easy enough to use the LCD in the project. I expected the hardest part to be dealing with two completely seperate input variables and deciding how to use them.

The plan was to create a headband which could measure the movement of the wearer’s head as well the sweatiness. It would then display a funny message on an lcd on the front of the headband.

Experiments

My experiments were based around trying to integrate the two sensors into a cohesive circuit.

I had to find a proper minimum value for the humidity, since it would be next to someones skin. I tested it by holding it next to my skin and waiting for the value to stabilise.

The circuit I built was as follows:

Sesh'o'meter diagram_bb

28555404_1773841882634057_1007076480_o

 

After some successful test code, I went onto develop the code by adding IF ELSE statements and manipulating the input variables into a form that we could use.

This part was actually much easier than I had thought, I got the code working without any bugs pretty much straight away.

Here is a video showing the circuit in action:

*NB.   The reason the display goes blank at some points is that since the humidity sensor is not next to human skin, it’s value can go lower than the minimum I had set in the code.

 

The code I wrote can be found here:

https://create.arduino.cc/editor/AlexRossMartin/3147d8f0-1c25-4ba0-a7f4-53965995475f/preview?embed

 

Unfortunately, I ran out of time to make an enclosure for it, but will be working on one and will upload it to the blog soon!

 

 

Advertisements

Using tilt sensor to determine rate of movement.

Hello,

Welcome to another blog post about my arduino projects. This week, me and chris decided to investigate tilt sensors. The idea is that coupled with the DHT11 temperature and humidity sensor we can produce a device which can detect the wearer’s level of movement and excursion. We would use the DHT11 to detect the sweat levels of the user and the tilt sensor to detect their movement.

Chris has done a blog post about the general workings of the tilt sensor here.

So…

In order to use the tilt sensor to determine level of movement, I decided that if I could take an average of it’s position over a fixed time period, this could give me a rough idea, the closer the average was to 0.5, the better the level of the users movement. Obviously this is only a very rough estimation and can only really be described as detecting whether or not they passed to either side of an equilibrium point an equal number of times.

The sensor would have to be mounted on its side somewhere on the user’s head, such that it read HIGH when they leaned one way and LOW when they leaned the other.

My first go at writing the code for this was as follows:

https://create.arduino.cc/editor/AlexRossMartin/6181e318-0cd0-42b6-a3a9-62d337801bba/preview?embed

 

This sort of worked, it gave me an average value. However, the FOR statement cycled so fast that the value of the average could only be 0 or 1. This would not do.

Also, viewing through the serial monitor, the value was refreshing so fast that it was impossible to read.

I needed to make sure the sample rate of the tilt sensor was reasonable enough to give me a full range of averages. The idea here was to sample every 10 millisecs, to combat the display problem, I decided to print to serial monitor only every 5 seconds and to reset the average at the same time.

 

My next go at the code was this:

 

https://create.arduino.cc/editor/AlexRossMartin/4dfe05ef-4b3d-4c0c-b211-7fe6360db0b0/preview?embed

 

This was a much closer approximation to what I wanted, here’s what it displayed on the serial monitor:

ScreenShot tilt sensor time delay serial mon 01

The values displayed are:

average of the tilt switch

currentmillis (the count value for the sampling)

currentmillis2 (the time value for the display)

the total (at time of printing)

 

when shaken around violently, I hoped the average would be somewhere around 0.5 but it seemed to jump around from around 0.3 to 0.7 when i was shaking it at a constant rate.

I discovered a number of problems, which could be causing this:

even though I was refreshing the display every 5 seconds, sometimes the total was only sampling 49 times. I thought that this was causing the problem but, when I held the sensor so that there was a constant HIGH value it would still average to 1 or 0.98 which is close enough to be acceptable.

Next I tried holding it to give a constant low value, what I found was that it averaged to around 0.6 and the total (which should be 0) was around 32, which meant it was sampling HIGH over half the time.

Obviously something was up, some noise is clearly being generated causing it to read high 50% of the time.

After re-reading chris’ blog post I realised that I had forgotten to set the internal pull-up resistor. I added a line into the setup code to do just that and the average read as 1 or 0 correctly.

After evaluating the successive averages, I found that it was very difficult to get  it close to 0.5 as I needed. I experimented with different sample rates and found that sampling every 500 allowed me to get a reasonable range of averages including 0.5.

With this finished, It was time to build the circuit.

Sesh'o'meter diagram_bb

Once I am sure that the circuit and coding works, I will build the circuit on an arduino nano and replace the brightness potentiometer  with a resistor.

I then needed to test the circuit before trying to write the code to do what I wanted. I took the earlier code used for displaying ouput from the DHT on an LCD and combined it with the new code I had written for the tilt sensor.

Here’s the code:

https://create.arduino.cc/editor/AlexRossMartin/a137a128-1483-4580-915f-b384a2be3cae/preview?embed

Everything worked correctly!

Now I could try to write some code to add more functionality. My goal was to take the inputs from the two sensors and use them to generate a score which would flash up a corresponding message on the LCD screen.

I decided that the easiest way to use the two inputs was to make them comparable and take an average. To begin with the humidity had a range of 0-100 and the tilt sensor average had a range of 0-1 (where 0 and 1 were no movement and 0.5 was max).

First, I needed to adjust the range of the humidity, since although its range is 0 to 100, the actual minimum humidity of a human forehead would be way above 0. I tested this by placing the sensor against my own head, allowing it to stabilize and then taking this as the new minimum value, luckily this ended up being 49, which I rounded to 50, giving me a range of 50 to 100. I subtracted 50 from this to give a range of 0 to 50.

Next I addressed the tilt sensor average by subtracting 0.5, giving it a range of -0.5 to 0.5. Then I took the absolute value, giving a range of 0 to 0.5, with a max of 0.5. I then noticed that I could save myself some work by changing this line:

average = total / 10;

to this:

average = total * 10;

This meant that the range for tilt sensor average was now 0 – 50.

With both having the same range I took a mean average and called this the Party Value (PV). I then produced a series of IF ELSE statements for various PV values and corresponding  funny messages for the display.

Here’s the code I used:

https://create.arduino.cc/editor/AlexRossMartin/3147d8f0-1c25-4ba0-a7f4-53965995475f/preview?embed

 

L7805 voltage regulator

Essentially the voltage regulator supplies a 5v output with 1.5A  to within 2%. It works by dissipating excess energy as heat. If the circuit draws a lot, then a metal heat sink is required to dissipate more heat.

 

Pin on the left is voltage in

Centre pin is ground

Pin on the right is voltage out

(Z-HUT, 2017)

Links to the datasheets are in the references.

References

Authorship or Source, Year. Title of web document or web page. [type of medium] (date of update if available) Available at: include web address/URL * [Accessed date].

All Data Sheet, 2018. Electric components datasheet. [online] Available at: <http://www.library.nhs.uk/guidelinesFinder> [Accessed 24 February 2018].
Z-HUT, 2017. How to power your ARDUINO project with a L7805 voltage regulator.  Available at: <http://www.youtube.com/watch#!v=eTapK5dRaw4> [Accessed 24 February 2018].

SW-520 tilt sensor

SW-520D

The tilt sensor is essentially a switch. It contains a little ball that moves inside, making or breaking a circuit depending on its position. The ball can be heard by gently wobbling the sensor, and its effect can be shown by using a multimeter to test the continuity between its two legs, and leaning the sensor over.

 Tilt sensor fritzing

(Adafruit learning system, 2017)

This is the pseudo-code as I understand it so far, the loop and debounce are currently incomplete. The code follows the flowchart.

Tilt sensor pseudo-code

/* Better Debouncer
*
* This debouncing circuit is more rugged, and will work with tilt switches!
*
* http://www.ladyada.net/learn/sensor/tilt.html
*/

int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin

int LEDstate = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin

// the following variables are long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 50; // the debounce time, increase if the output flickers

void setup()
{
pinMode(inPin, INPUT);
digitalWrite(inPin, HIGH); // turn on the built in pull-up resistor
pinMode(outPin, OUTPUT);
}

void loop()
{
int switchstate;

reading = digitalRead(inPin);

// If the switch changed, due to bounce or pressing…
if (reading != previous) {
// reset the debouncing timer
time = millis();
}

if ((millis() – time) > debounce) {
// whatever the switch is at, its been there for a long time
// so lets settle on it!
switchstate = reading;

// Now invert the output on the pin13 LED
if (switchstate == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(outPin, LEDstate);

// Save the last reading so we keep a running tally
previous = reading;
}

(Lady ada, 2018)

“int inPin = 2;” refers to the input at digital pin 2,

“int outPin = 13;” refers to the output at digital pin 13.

“digitalWrite(inPin, HIGH);”  turns on the built in pull-up resistor

“long time = 0;         // the last time the output pin was toggled

“long debounce = 50;”   // the debounce time, increase if the output flickers

The program is set to read pin 2 every 50 milliseconds. As there is an actual ball bearing moving to make or break a circuit, the “debounce” section allows for the ball to settle. Otherwise as it moves into place, every small vibration will be read as a new command. This could be changed to make it more or less sensitive.

This video shows the basic setup. The on board LED is switched off when the tilt sensor moves past 90 degrees. It doesn’t matter which direction the sensor faces. 

References

Lady ada, 2018. Using a tilt sensor. [online] Available at: < https://learn.adafruit.com/tilt-sensor/using-a-tilt-sensor&gt; [Accessed 23 February 2018].

Adafruit learning system, 2017. Tilt sensor [pdf] Adafruit. Available at: <https://learn.adafruit.com/tilt-sensor/using-a-tilt-sensor [Accessed 23 February 2018].

DHT11 temperature and humidity sensor

This is a DHT11 temperature and humidity sensor.

Function:

To detect the ambient air temperature and relative humidity. It is mainly used in automated gardens to ensure that the conditions are right for the plant.

Range:

°Humidity 20%-90% RH (Relative Humidity)

°Humidity accuracy +-5% RH

°Temp range 0-50°C

°Temp accuracy +-2%°C

How does it work:

Humidity is measured by checking the resistance between two electrodes, these electrodes are placed on a water absorbing material. As the material absorbes more water, it releases electrons which make it easier for current to flow between the electrodes, this gives a lower resistance value which can be interpreted as a greater humidity.

The temperature is measured by a NTC thermistor, meaning the resistance decreases as temperature increases. The thermistor is made from a temperature sensitive semi-conductor, as the temperature increases the semi-conductor releases electrons which allow current to pass more easily.

Circuit:

Connecting the sensor is very simple it requires a 5v supply, ground and a signal wire.

It outputs a digital signal, which should be pulled-up by a 10k ohm resistor, this is usually built into the PCB.

The output can be any of the digital pins and must be set as an input in the IDE.

Code:

Before using the DHT11, you must import the DHTlib library.