UltraSonic Sensor Tutorial #1: How to Use HR-SR04 Sensor

Picture of Tinee9: DIY Drone Avoidance Sensor

Tinee9 is back with another quick tutorial. This tutorial will talk about the HC-SR04 Ultrasonic sensor, how to use it with arduino, and how it could be used for Drone obstacle avoidance and landing.

Tutorial Difficulty: Beginner

Step 1: Drones and Environment Sensing

Picture of Drones and Environment Sensing
Picture of Drones and Environment Sensing
Picture of Drones and Environment Sensing

DJI Mavic Air has 6 visual sensor and 2 downward infrared sensor. The sensors’ information combined, allow the Air to sufficiently detect and avoid obstacles in nearly every direction. There are 2 cameras in front, 2 on the bottom, and 2 in the rear of the drone. These cameras take in the information around, which then the computer on board stitches the pieces together and creates a 3D environment. This environment is then dissected in software to determine what is an obstacle and how far way it is. When the camera looking downward does not have enough information, the infrared sensor facing down ward sends out a infrared signal and then receives it in the other channel. This allows the drone to land because it gives the information of how far away the drone is from the ground.

Step 2: DIY Drone Avoidance Sensor

Picture of DIY Drone Avoidance Sensor
Picture of DIY Drone Avoidance Sensor

Since the DJI camera sensors are lot more complicated and probably cost more money, so we will use a different sensor. The sensor we will use today is the HC-SR04 Ultrasonic Sensor. This sensor sends out an sound wave that we can’t hear but maybe dogs can and waits until the sound comes back to the receiving sensor on board. Once the receiving sensor on board receives the signal, the computer makes a quick calculation of how far away an object is. This calculation is based on the speed of sound and the time it took to get back.

In the picture on the left above, this is an example of how the sound wave leaves one sensor, hits the ruler that I am holding, and then bounces back to the receiving sensor.

So how would this work on a self made drone, well if we put enough of these on we would be able to determine objects above, behind, below, and in front of us at all times. And if the sensors had enough resolution and range we could possibly and more accurately hold a position in air more accurately than a GPS signal.

Step 3: Materials

Picture of Materials
Picture of Materials

Materials list is pretty simple:

1x Arduino Nano

4x Male to Male Jumpers

1x HC-SR04 Ultrasonic Sensor

1x Breadboard

1x USB to Mini USB cable

1x PC with Arduino IDE

You can collect all materials (except PC and USB cable) in the link here. Link

Step 4: Connection

Picture of Connection
Picture of Connection

1. Place the Arduino on the breadboard (This is saying you have headers on your Arduino already)

2. Place HC-SR04 Sensor facing out of the Breadboard like above

3. Connect a Jumper from Arduino D13 Pin to HC-SR04 Trigger Pin

4. Connect a Male to Male Jumper from Arduino D13 Pin to HC-SR04 Trigger Pin

5. Connect a Male to Male Jumper from Arduino D12 Pin to HC-SR04 Echo Pin

6. Connect a Male to Male Jumper from Arduino +5V Pin to HC-SR04 VCC Pin

7. Connect a Male to Male Jumper from Arduino GND Pin to HC-SR04 GND Pin

8. Connect USB to USB mini from Computer USB to Arduino Nano USB Mini

Step 5: Load Code and Test

IMG_20180619_225659.jpg
Ultrasonic sensor

Turn On your PC and load up your Arduino IDE. If you do not have Arduino IDE please visit Arduino.org and follow their instructions.

Paste the Code Below into your Arduino IDE and Program. Once you have programmed and place your hand or an object in front of the sensor, you should see the arduino monitor show the distance changing as the object moves closer or father away.

By learning how the code below work you can then manipulate it for your need for an RC car or drone that avoids objects or even an automatic doorbell that has no button.

#define trigPin 13

#define echoPin 12

#define led 11

#define led2 10

void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); pinMode(led2, OUTPUT); }

void loop() { long duration, distance; digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH); // delayMicroseconds(1000); – Removed this line delayMicroseconds(10); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance < 4) { // This is where the LED On/Off happens digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off digitalWrite(led2,LOW); } else { digitalWrite(led,LOW); digitalWrite(led2,HIGH); } if (distance >= 200 || distance <= 0){ Serial.println(“Out of range”); } else { Serial.print(distance); Serial.println(” cm”); } delay(500); }

Below is a video of someone performing an obstacle avoidance test for the DJI Mavic Air

Arduino Controlled Motor ESC

4 Years ago I made my own drone for a cost of $300 back when the first commercial drone was about $1500. The Arducopter controller controlled the motor ESC, I used the DJI DIY frame, and bought a 720MHZ remote control. Here is a modified KIT of what I built 4 years ago. KIT

Now that I have bit more experience in electronics aerospace engineering, I want to build my own arducopter controller to control the drone. I will be using Arduino for my platform. For today, I will demonstrate that we can control an ESC with and arduino nano.

Materials: Get Most Materials Here
ESC
Motor
Battery that will run the motor
Solder
Solder Iron
Arduino Nano
Bread Board
Jumper Wire
PC
USB Cable
Arduino IDE

Step 1: Solder your Motor to the ESC controller.
Step 2: Attach your Arduino Nano to a bread board.
Step 3: Attach your Battery – to the ESC Black Wire.IMG_20180605_005410
Step 4: Attach your ESC Black to the Arduino GND Pin.
Step 5: Attach your ESC White wire to Arduino D9 Pin.IMG_20180605_005423

Step 6: Attach Arduino Nano to PC with USB Cable.
Step 7: Program Arduino Nano with this Code in Arduino IDE.
Code:
#include ;
Servo esc;
int Pin = 0;
int x = 0;
void setup()
{
esc.attach(9);
}
void loop()
{
int throttle = analogRead(Pin);
throttle = map(throttle, 0, 1023, 0, 179);
for(x = 0; x < 175; x++){
esc.write(x);
delay (250);
}
esc.write(0);
delay(10000);
}
Step 8: Attach your ESC Red wire to Battery +.
Step 9: Enjoy your Arduino Nano commanding the ESC with PWM commands.


Initial Development of a Medical Device

While on my journey to educate people about electronics, technology, and drones, I came across an idea, which was inspired by the death of a co-worker at my aerospace company during work.

The idea so far in development is a glorified sports watch/band that tracks your heart rate and oxygen stats. Eventually it will include and track a more health stats. All these stats combined will help save the lives of those like possibly my co-worker if place on the patient.

IMG_20180519_215614

IMG_20180519_203836IMG_20180519_215607

Unfortunately, as seen above the proof of concept circuit is bulky and thick. Not very convenient of a product, but luckily this not the end product and very early in its development.IMG_20180519_220906

To start the development process, I have decided to start with the arduino nano platform which I am familiar with. With the arduino platform I am flexible with the software, it is familiar, also there is a lot of libraries out there for the devices I want to use. The devices I have initially picked are a Bluetooth device to connect my phone so the user can read the health stats, a particle sensor, and a battery. Below are pictures with links to Amazon for the modules I used. (no battery in the links)

After putting it all together I was able to get  a heart rate reading sent to my phone via bluetooth. My goal is to get enough money to hire a patent lawyer to patent the idea which I hope to share with you my readers in the future. But for now I will just show the progression of what it looks like and basic functions of the eventual product that will save people’s lives.

Below is a practical use of the device.IMG_20180520_115509

 

Little Bits vs Sparkfun Review

As I started my company last year initially making modules for Arduino or Raspberry Pi platforms I wanted to make a module that was as small as possible, have the ability to connect to it via stacking or planerly, and simple. As I started selling I already knew about Sparkfun but found Little Bits. There are some pros and cons about each of these companies and that is what I wanted to do a review on to show you the difference between the two companies.
Sparkfun:

Sparkfun ScreenShot

Photo: Screenshot of Sparkfun website

Pros:

-Thousands of Modules

-Lots of software and hardware documentation via sparkfun or third party people

-Engage both electrically and software

-Well made and popular

-Versatile: works with arduino, raspberry pi, or own custom platforms

Cons:

-Need some understanding of electronics

-Need extra items; Breadboard, wires, solder iron, etc

-Not color coordinated

-Not always intuitive

-Modules may not be reusable

Photo: Screen shot from Little bits website
Little Bits:

Pros:

-Lots of Modules

-Lots of software documentation

-Engage more in software

-Well made and popular

-Color Coordinated

-Intuitive

-Easy connectability

-Don’t need to understand electronics as much

-Don’t need extra parts; Breadboard, wires, solder iron, etc

-Reusable

Cons:

-Does not engage teaching in the technical electrical side as much

-can be really bulky prototypes

-Software is not regular programming language

“Update saw that Little bits does integrate Arduino. Still not primary focus”

Overall Suggestion:

If you are a teacher or parent of a elementary or middle schooler and want them to learn about some electrical engineering or software development Little Bits is best.

Once your kid is in middle school to highschool to get more complex prototypes and smaller inventions spark fun modules will be the next step.


Disclaimer: I am not affiliated with Little bits or Sparkfun just the Amazon Affiliate program. I may get paid if you click on my links and buy things from Amazon which will help this blog/tech company keep going.

Accelerometer Tutorial #2: Basic Earthquake Dectector

Tinee9 is back and today we are going to make a simple Arduino earthquake detector. Please visit my instructable to interface with Tinee9’s LIS2HH12 in the below link to set up the device so all you have to do is add a 3 resistors and 3 Light Emitting Diodes (LEDs)This instructable is considered beginner level with some experience with Arduino software.

*This instructables does not reflect all the possible or correct acceleration changes for earthquakes in the richter scale

Step 1: Earthquakes

Picture of Earthquakes

The picture is a google search capture of an earthquake. As a child, I lived through the 1994 Northridge earthquake. I don’t remember too much about the earthquake other than these things below:

-House was cracked in half and one half now has a step down to it.
-One of the walls in my bedroom had a hole in it to the backyard.
-I lost my favorite toy rattle at the time. It had beads in the rattle that you could see go up and down.
-Sidewalk cement across the street literally flipped upside down.
-The street had a mini “mountain” made out of it.

Needless to say big earthquakes are not fun.

We haven’t had any big earthquakes (Greater than a 5.0) in Southern California for quite a while but one of these days we will. So lets build a Earthquake detector!!!

Ad: Arduino Starter Kit

Step 2: Materials

Picture of Materials

We need:

Click on the pictures below to go the the Amazon page if you need to buy these items for the project. or this link which will put everything in your Amazon kart for you- KIT

-The setup from the LIS2HH12 tutorial

– 3x 690 ohm resistors
-1x Green LED
-1x Yellow LED
-1x Red LED
-Optional: Wire Stripper

Step 3: Quick Lesson on V = I*R

In Electrical Engineering you have the equation V = I * R which invades your life every day.

V = Voltage (Volts, V)
I = Current (Amps,A)
R = Resistance (Ohms)

In a circuit this equation is never violated. So if I connect a 5V source to a 690 Ohm resistor and then to an LED to ground, the current in the circuit is going to be this:

Example LED voltage drop = 2.5V
(Source – LED) = Current * Resistance
5V-2.5V = I * 690 Ohms
I = 2.5V/690 Ohms = 3.62 milliAmps or 3.62 mA

Typical LEDs do not like to exceed 10mA-20mA or they will burn out.

Step 4: LED Polarity

Picture of LED Polarity
Picture of LED Polarity

LEDs have polarity that lets a person know which way it needs to be placed to allow current to flow through it.

Current goes through the Anode of the LED to the Cathode of the LED. It cannot go the other way. If placed backwards it will not work or blow up if voltages exceed its specifications.

If not enough current then there may not be any light emitting from the LED.
The long side on the Red LED is the + Anode and Short side it the – Cathode side.

Step 5: Set Up the Earthquake Dectetor

Picture of Set Up the Earthquake Dectetor
Picture of Set Up the Earthquake Dectetor
Picture of Set Up the Earthquake Dectetor

Steps of setting up the 3x 690 resistors and the 3 LEDs.

1. Place a 690 ohm resistor from D4 (Row 55) of the arduino nano to row 37 of the breadboard

2. Place a Red LED Anode on the top half of the breadboard on row 37 and the Cathode place in the blue rail (GND)

3. Place a 690 ohm resistor from D3 (row 54) of the arduino nano to row 38 of the breadboard

4. Place a Yellow LED Anode on the top half of the breadboard on row 38 and the Cathode place in the blue rail (GND)

5. Place a 690 ohm resistor from D2 (row 53) of the arduino nano to row 39 of the breadboard
6. Place a Green LED Anode on the top half of the breadboard on row 39 and the Cathode place in the blue rail (GND)

7. Make sure none of the wires, resistors, or LED leads are shorted together by accident or you may cause damage to your circuit.

Add TipAsk QuestionCommentDownload

Step 6: Download .Ino

Download the Tiny9_LIS2HH12_Earthquake_mon.ino file from here: github

Step 7: Enjoy

Now you should be able to upload your .ino into your arduino nano.

What will happen is if there is a minor earthquake the Yellow LED will light up.

If there is a major earthquake a Red Led will light up.

Once a minor or major earthquake has been detected you must reset the arduino if you want to turn off the LEDs.

*This sketch does not reflect all the possible or correct acceleration changes for earthquakes in the richter scale.

Accelerometer Tutorial #1: Working with an Accelerometer

Introduction: 3 Axis Accelerometer LIS2HH12 Module

Picture of 3 Axis Accelerometer LIS2HH12 Module

This tutorial is considered beginner level with some experience with arduino software and soldering.

What does an accelerometer do?

There are at least two purposes of an accelerometer: To determine an angle in particular axes. (X,Y,or Z or all), or to determine acceleration change in an axes.

Accelerometers are used everywhere. They are used in:

Phones, Fitness bands, Drones, Robotics, Missiles, and Helicopters just to name a few. How you want to use an accelerometer is up to your imagination.

Step 1: Materials

Picture of Materials

Materials you need are:

Arduino Nano or preferred arduino device

USB to Arduino Nano Cable

Wire Strippers and Wire, or Point to point Breadboard Jumpers

LIS2HH12 Module,(Amazon link on the picture)

2x 10 Kohm resistors

You can click on the amazon links below if you need an individual item or you can click the for the full kit here  KIT



Step 2: The Sensor

Picture of The SesnorPicture of The Sesnor

LIS2HH12 module is based off the ST 3-Axis accerlerometer. The module is a tiny package and allows for 2 5-pin headers to be soldered to it. This mitigates vibration noise that is introduced to the accelerometer from external sources of varying frequencies.

Main features for this chip are:
-Low-power mode 5uA draw
-16-bit resolution
-Performs +/-2 g,4 g,8 g
-0.2% noise
-I2C or SPI protocol
-Typical Voltage
-3.3V

Max Rating 4.8V (Do not go above 4.8 volts or you will break the Accelerometer chip)

Step 3: Project Platform

Picture of Project Platform

Project platform for the accelerometer is Arduino.

The development board I am using is a Arduino Nano.

Currently the Tinee9 LIS2HH12 accelerometer has only basic code for the Arduino but will be hopefully expanding the code for more technical projects and for Raspberry Pi or any platform that has enough fan base recommended by YOU. 🙂

Step 4: Breadboard

Picture of Breadboard

If you have headers on both of your Arduino nano and LIS2HH12 Module you can put the Arduino Nano and accelerometer on the Breadboard like this, straddling the split line allowing access to the breakout pins.

Make sure the 3.3V pins on the Module is facing the Arduino.

If you do not have header on them get some and solder them to the boards.

Step 5: Placing Resistors on the Board

Picture of Placing Resistors on the Board

The I2C protocol that we will be using in this project needs 2 10 Kohm pull-up resistors to the supply rail on the chip (+3.3 Pins); one on the Clock line (CL) and one on the Data Line (DA)

Since the LIS2HH12 accelerometer max voltage is 4.8V and in this project we are using the 5V off of the Nano, I have placed a 100 ohm resistor from the 5V pin on the Nano to the red supply rail on the breadboard to bring down the supply rail a little.

Step 6: Connecting the Rest of the Board

Picture of Connecting the Rest of the Board

Now we are going to connect the rest of the module to the arduino.

The Gnd Pin on the module and arduino should have a jumper wires going from it to the Blue Rail on the Breadboard.

Connect the +3.3 Pin on the module to the red supply rail on the breadboard.

These last two step allowed us to power up the module when we power the arduino via battery or USB

Jumper Wire from the +3.3 Pin on the Module to the CS pin on the module (This enables the I2C bus on the module)

Jumper wire from the Gnd Pin on the module to the A0 pin on the module (This tells the accelerometer which address it will respond to when talking on the I2C Bus)

Jumper wire from A5 on the arduino to CL on the Module (This allows the clock on the arduino to sync with the acceleromter.

Jumper wire from A4 on the arduino to DA on the module (This allows the data to be transferred between the arduino and the module.)

Step 7: Download Files

Picture of Download Files

Go to Github address https://github.com/Tinee9/LIS2HH12TR and download the files.

Go to this location on your computer

C:Program Files (x86)Arduinolibraries

Create a Folder Called Tiny9

Place the .h and .cpp Files in that Tiny9 Folder

Step 8: Open Up .ino

Picture of Open Up .ino

Open up the .ino file you downloaded in the Arduino IDE (Program/software)

Step 9: Upload Sketch

Picture of Upload Sketch

Once you have connected your arduino via USB cable to the computer, there should be a port number highlighted under tools tab in the arduino IDE.

My port happens to be COM 4 but yours might be 1 or 9 or something else.

If you have multiple COM options then choose the one that represents the Arduino that you are using. (How to determine which COM port for multiple choices can be on a different instructable if requested.)

Once you have the Arduino Port chosen, click the upload button.

Step 10: Enjoy

Picture of Enjoy

After it has finished Uploading you should be able to open the Serial Monitor in the Tool Tab and you should see something like this popping on your Monitor.

The Graph displays the x,y, and z axis in that order.

Z axis should say close to 1.0 +/- some counts because Z is pointing up.

Now you can rotate your breadboard and enjoy watching the numbers change showing you how the module’s axises are affected by gravity and acceleration.

Accelerometer Tutorial#1: Working with an Accelerometer

Introduction: 3 Axis Accelerometer LIS2HH12 Module

Picture of 3 Axis Accelerometer LIS2HH12 Module

This tutorial is considered beginner level with some experience with arduino software and soldering.

There are at least two purposes of an accelerometer: To determine an angle in particular axes. (X,Y,or Z or all), or to determine acceleration change in an axes.

Accelerometers are used everywhere. They are used in:

Phones, Fitness bands, Drones, Robotics, Missiles, and Helicopters just to name a few. How you want to use an accelerometer is up to a person’s imagination.

Step 1: Materials

Picture of Materials

Materials you need are:

Arduino Nano or preferred arduino device

USB to Arduino Cable

LIS2HH12 Module,(Amazon link on the picture)

Wire Strippers and Wire, or Point to point Breadboard Jumpers

2x 10 Kohm resistors
https://kit.com/embed?url=https%3A%2F%2Fkit.com%2FTinee9%2Ftinee9-basic-arduino-kit

Step 2: The Sesnor

Picture of The Sesnor
Picture of The Sesnor

LIS2HH12 module is based off the ST 3-Axis accerlerometer. The module is a tiny package and allows for 2 5-pin headers to be soldered to it. This mitigates vibration noise that is introduced to the accelerometer. from external sources of varying frequencies.

Main features for this chip are:
-Low-power mode 5uA draw
-16-bit resolution
-Performs +/-2 g,4 g,8 g
-0.2% noise
-I2C or SPI protocol
-Typical Voltage
-3.3V

Max Rating 4.8V (Do not go above 4.8 volts or you will break the Accelerometer chip)

Step 3: Project Platform

Picture of Project Platform

Project Platform for the accelerometer is Arduino.

The Development board I am using is a Arduino Nano.

Currently the Tinee9 LIS2HH12 accelerometer has only basic code for the Arduino but will be hopefully expanding the code for more technical projects and for Raspberry Pi or any platform that has enough fan base recommended by YOU. 🙂

Step 4: Breadboard

Picture of Breadboard

If you have headers on both of your Arduino nano and LIS2HH12 Module you can put the Arduino Nano and accelerometer on the Breadboard like this, straddling the split line allowing access to the breakout pins.

Make sure the 3.3V pins on the Module is facing the Arduino.

If you do not have header on them get some and solder them to the boards.

Step 5: Placing Resistors on the Board

Picture of Placing Resistors on the Board

The I2C protocol that we will be using in this project needs 2 10 Kohm pull-up resistors to the supply rail on the chip (+3.3 Pins); one on the Clock line (CL) and one on the Data Line (DA)

Since the LIS2HH12 accelerometer max voltage is 4.8V and in this project we are using the 5V off of the Nano, I have placed a 100 ohm resistor from the 5V pin on the Nano to the red supply rail on the breadboard to bring down the supply rail a little.

Step 6: Connecting the Rest of the Board

Picture of Connecting the Rest of the Board

Now we are going to connect the rest of the module to the arduino.

The Gnd Pin on the module and arduino should have a jumper wires going from it to the Blue Rail on the Breadboard.

Connect the +3.3 Pin on the module to the red supply rail on the breadboard.

These last two step allowed us to power up the module when we power the arduino via battery or USB

Jumper Wire from the +3.3 Pin on the Module to the CS pin on the module (This enables the I2C bus on the module)

Jumper wire from the Gnd Pin on the module to the A0 pin on the module (This tells the accelerometer which address it will respond to when talking on the I2C Bus)

Jumper wire from A5 on the arduino to CL on the Module (This allows the clock on the arduino to sync with the acceleromter.

Jumper wire from A4 on the arduino to DA on the module (This allows the data to be transferred between the arduino and the module.)

Step 7: Download Files

Picture of Download Files

Go to Github address https://github.com/Tinee9/LIS2HH12TR and download the files.

Go to this location on your computer

C:\Program Files (x86)\Arduino\libraries

Create a Folder Called Tiny9

Place the .h and .cpp Files in that Tiny9 Folder

Step 8: Open Up .ino

Picture of Open Up .ino

Open up the .ino file you downloaded in the Arduino IDE (Program/software)

Step 9: Upload Sketch

Picture of Upload Sketch

Once you have connected your arduino via USB cable to the computer, there should be a port number highlighted under tools tab in the arduino IDE.

My port happens to be COM 4 but yours might be 1 or 9 or something else.

If you have multiple COM options then choose the one that represents the Arduino that you are using. (How to determine which COM port for multiple choices can be on a different instructable if requested.)

Once you have the Arduino Port chosen, click the upload button.

Step 10: Enjoy

Picture of Enjoy

After it has finished Uploading you should be able to open the Serial Monitor in the Tool Tab and you should see something like this popping on your Monitor.

The Graph displays the x,y, and z axis in that order.

Z axis should say close to 1.0 +/- some counts because Z is pointing up.

Now you can rotate your breadboard and enjoy watching the numbers change showing you how the module’s axises are affected by gravity and acceleration.