How to Capture Roll, Pitch, and Yaw Data Using Raspberry Pi and Arduino

In this post, I’ll show you how to capture roll, pitch, and yaw data using Raspberry Pi and Arduino.

Requirements

Here are the requirements:

  • Using the IMU connected to the Arduino, capture roll, pitch, and yaw data.
  • Send the data via Serial over USB to the Raspberry Pi.
  • Send the data via WiFi from Raspberry Pi to my host computer (e.g. my personal laptop).
  • Display the data on my host computer.
  • On the Arduino, use FreeRTOS for tasks.
  • To make things interesting, I mounted the IMU on a quadcopter.

Design

Hardware

The following components are used in this project. You will need:

Software

Here are the steps for the software of the system:

  • Define two tasks for Blink & IMURead. These are function prototypes.
    • The Blink task blinks the built-in LED on the Arduino board.
    • IMURead captures Roll+Pitch+Yaw data from the Adafruit BNO055.
  • Define a method that displays some basic information on the sensor.
  • Define a method that displays some basic information about the sensor status.
  • Define a method that displays the sensor calibration status.
  • Setup Function:
    • Initialize serial communication at 9600 bits per second
    • Wait for serial port to connect (Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards).
    • Initialize the sensor.
    • Display some basic information on this sensor.
    • Set up two tasks to run independently.
  • Empty loop since things are done in tasks.

Implementation

I began by testing the Adafruit BNO055 Absolute Orientation Sensor. The BNO055 is three sensors in one device. It includes an accelerometer to measure acceleration forces, a gyroscope that uses Earth’s gravity to help determine orientation, and a magnetometer that measures magnetism. Typical applications of this sensor include navigation, robotics, fitness, augmented reality, and tablet computers.

To setup the BNO055, I first need to prepare the header strip by cutting it to an appropriate length and placing it into the breadboard. I placed the long pins down.

I then added the BNO055 board on top of the pins.

I then soldered all of the pins in order to solidify electrical contact. Here are the steps I took:

  • I put on safety glasses and latex gloves.
  • I turned on the USB fan for ventilation, in order to to breathe in the smoke.
  • I turned on the soldering iron to the 4 heat setting.
  • I placed a damp sponge in the soldering station.
  • Once the iron was hot, I tinned the tip by adding a little bit of solder to cover the tip.
  • I wiped the tip off on the damp sponge to remove all but a thin layer of solder to help the tip last longer and to facilitate the transfer of heat from the soldering iron to the joint.
  • I placed the tip of the soldering iron to the joint to heat it up.
  • I fed a small amount of solder to the joints and removed the soldering iron after 2 seconds in order to not damage the board.
  • With the soldering iron hot, I wiped it off on a damp sponge.
  • I turned off the soldering iron and cleaned everything up.

I wired the BNO055 to the Arduino Uno using the solderless breadboard:

  • Connected Vin to the power supply of 5V
  • Connected GND to common power/data ground
  • Connected the SDA pin to the I2C data SDA pin on the Arduino (A4).
  • Connected the SCL pin to the I2C clock SCL pin on the Arduino (A5).
roll_pitch_yaw_1

Next, I attached the Arduino Uno to the USB port on my computer.

I opened the Library manager and installed the Adafruit_BNO055 driver which supports reading raw sensor data and the Adafruit Unified Sensor system to retrieve orientation data in a standard data format.

I used the Adafruit Sensor System code in order to retrieve the three axis orientation data.

To test the Unified Sensor System output, I opened and ran the sensorapi demo in the Adafruit_BNO055 examples folder.

I also opened and ran the rawdata demo in the Adafruit_BNO055 examples folder.

I opened the Sensor API and redefined the X, Y, and Z variables to Roll, Pitch, Yaw. I assumed that the front of the BNO055 was the part of the sensor labeled BNO055.

I used this website for the assumptions in order to determine which of the variables corresponded to Roll, Pitch, and Yaw. In my analysis, I found that Yaw = X, Pitch = Y, and Roll = Z. I determined this by rotating the BNO055 board around to 90 degrees yaw (East direction), then roll (full roll to the right), and then pitch (BNO055 pointing straight up at the sky), and observing which variable changed to 90 degrees.

I successfully tested the BNO055. It worked properly. Here was the output from the Serial monitor.

roll_pitch_yaw_2

I need to use FreeRTOS for the tasks in the Arduino code for the BNO055.

I opened up Arduino on my desktop computer. I then went to Sketch -> Manage Libraries and looked for FreeRTOS, and installed it.

roll_pitch_yaw_3

Then, I went under the Sketch->Include Library menu to ensure that the FreeRTOS library is included in my sketch.

roll_pitch_yaw_4

I then compiled and upload this empty sketch to my Arduino Uno.

FreeRTOS was now running on my device.

Then I uploaded and tested the example Blink sketch. It is under File -> Examples -> 01.Basics. I inserted #include <Arduino_FreeRTOS.h> at the start of the sketch. I then compiled it and uploaded it to the Arduino Uno.

Then I uploaded and tested the example Blink_AnalogRead.ino sketch. It is under File -> Examples -> FreeRTOS. I made sure #include <Arduino_FreeRTOS.h> was inserted at the start of the sketch. I then compiled it and uploaded it to the Arduino Uno. This sketch combines two basic Arduino examples, Blink and AnalogRead into one sketch with two separate tasks. Both tasks perform their duties independently, managed by the FreeRTOS scheduler.

Next, I downloaded Arduino to my Raspberry Pi. It took me a while to do this since a lot of the blogs and YouTube videos had incorrect instructions. I’ll walk you through everything I did below.

I first followed these steps. This process will enable me to execute the Arduino-Raspberry Pi serial communication.

roll_pitch_yaw_5

I right clicked on the extracted folder and click on open in terminal.

I then typed the below command in the terminal to install the Arduino IDE.

sudo ./install.sh

An error message showed up, which was ignored. The Arduino IDE was supposedly installed.

roll_pitch_yaw_6

I then typed the following command to open up the Arduino IDE

sudo ./arduino

Nothing opened up. This installation attempt failed.

So now, I tried an alternative way of installing Arduino. I typed the following commands as recommended here.

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install arduino

I then restarted the Raspberry Pi.

Still the Arduino IDE would not start. Maybe third time’s a charm.

I then went to the Arduino website and went under “Hourly Builds”. I clicked on “Linux ARM.” I followed these instructions:

I then went to the /home/pi/Downloads folder.

I right clicked on the tar.xz folder and I clicked “Archiver.” I then selected “All Files” and clicked “Extract.”

roll_pitch_yaw_7

I typed the following commands in the terminal window.

cd Downloads
ls
cd arduino-nightly
ls
./install.sh
sudo ./install.sh

I then went to Raspberry Pi in the upper left corner of the screen. Programming -> Arduino IDE.

The Arduino IDE successfully loaded!

roll_pitch_yaw_8

With the Arduino plugged in to the Raspberry Pi USB port, I tried the example Blink sketch under File -> Examples -> 01.Basics in the Arduino IDE. The LED on the Arduino began to blink.

I then opened the Library manager and installed the Adafruit_BNO055 driver which supports reading raw sensor data and the Adafruit Unified Sensor system to retrieve orientation data in a standard data format.

I then went to Sketch -> Manage Libraries and looked for FreeRTOS, and installed it.

To test the Unified Sensor System output, I opened and ran the sensorapi demo in the Adafruit_BNO055 examples folder.

I opened the sensorapi example sketch and redefined the X, Y, and Z variables to Roll, Pitch, Yaw. I assumed that the front of the BNO055 was the part of the sensor labeled BNO055.

I then modified the sensorapi sketch in the Arduino IDE in order to incorporate FreeRTOS.

I also created two tasks so that I could use FREERTOS and get experience with an RTOS.

  • The Blink task blinks the built-in LED on the Arduino board.
  • IMURead captures Roll+Pitch+Yaw data from the Adafruit BNO055.

Hardware

roll_pitch_yaw_9
roll_pitch_yaw_10
roll_pitch_yaw_11
roll_pitch_yaw_12

Software

Here is the code for the program I developed. (Note that after you load the sketch to Arduino, the code runs automatically whenever the Arduino is connected to power. That could be via a battery or power directly from the Raspberry Pi):

#include <Arduino_FreeRTOS.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>

/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
   which provides a common 'type' for sensor data and some helper functions.

   To use this driver you will also need to download the Adafruit_Sensor
   library and include it in your libraries folder.

   You should also assign a unique ID to this sensor for use with
   the Adafruit Sensor API so that you can identify this particular
   sensor in any data logs, etc.  To assign a unique ID, simply
   provide an appropriate value in the constructor below (12345
   is used by default in this example).

   Connections
   ===========
   Connect SCL to analog 5
   Connect SDA to analog 4
   Connect VDD to 3-5V DC
   Connect GROUND to common ground

   History
   =======
   2015/MAR/03  - First release (KTOWN)
   2015/AUG/27  - Added calibration and system status helpers


   In this program, we use the Adafruit BNO055 connected to the Arduino to capture
   Roll+Pitch+Yaw data.

   @author Addison Sears-Collins
   @version 1.0 2019-04-02
*/

Adafruit_BNO055 bno = Adafruit_BNO055(55);

// Define two tasks for Blink & IMURead. These are function prototypes.
// The Blink task blinks the built-in LED on the Arduino board.
// IMURead captures Roll+Pitch+Yaw data from the Adafruit BNO055.
void TaskBlink( void *pvParameters );
void TaskIMURead( void *pvParameters );


/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, LEONARDO, MEGA, and ZERO 
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care 
  of use the correct LED pin whatever is the board used.
  
  The MICRO does not have a LED_BUILTIN available. For the MICRO board please substitute
  the LED_BUILTIN definition with either LED_BUILTIN_RX or LED_BUILTIN_TX.
  e.g. pinMode(LED_BUILTIN_RX, OUTPUT); etc.
  
  If you want to know what pin the on-board LED is connected to on your Arduino model, check
  the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products
  
  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
  
  modified 2 Sep 2016
  by Arturo Guadalupi
*/

  // initialize digital LED_BUILTIN on pin 13 as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
  }
}

void TaskIMURead(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
  
/*
  IMUReadSerial
  Capture the Roll+Pitch+Yaw data

*/

  for (;;)
  {
    // Get a new sensor event
    sensors_event_t event;
    bno.getEvent(&event);
   
    /* Display the floating point data */
    Serial.print("Yaw: ");
    Serial.print(event.orientation.x, 4);
    Serial.print("\tPitch: ");
    Serial.print(event.orientation.y, 4);
    Serial.print("\tRoll: ");
    Serial.print(event.orientation.z, 4);

    /* Optional: Display calibration status */
    displayCalStatus();

    /* Optional: Display sensor status (debug only) */
    //displaySensorStatus();

    /* New line for the next sample */
    Serial.println("");
    
    vTaskDelay(7);  // seven tick delay (105ms) in between reads for stability
  }
}

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  bno.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" xxx");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" xxx");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" xxx");
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Display some basic info about the sensor status
*/
/**************************************************************************/
void displaySensorStatus(void)
{
  /* Get the system status values (mostly for debugging purposes) */
  uint8_t system_status, self_test_results, system_error;
  system_status = self_test_results = system_error = 0;
  bno.getSystemStatus(&system_status, &self_test_results, &system_error);

  /* Display the results in the Serial Monitor */
  Serial.println("");
  Serial.print("System Status: 0x");
  Serial.println(system_status, HEX);
  Serial.print("Self Test:     0x");
  Serial.println(self_test_results, HEX);
  Serial.print("System Error:  0x");
  Serial.println(system_error, HEX);
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Display sensor calibration status
*/
/**************************************************************************/
void displayCalStatus(void)
{
  /* Get the four calibration values (0..3) */
  /* Any sensor data reporting 0 should be ignored, */
  /* 3 means 'fully calibrated" */
  uint8_t system, gyro, accel, mag;
  system = gyro = accel = mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);

  /* The data should be ignored until the system calibration is > 0 */
  Serial.print("\t");
  if (!system)
  {
    Serial.print("! ");
  }

  /* Display the individual values */
  Serial.print("Sys:");
  Serial.print(system, DEC);
  Serial.print(" G:");
  Serial.print(gyro, DEC);
  Serial.print(" A:");
  Serial.print(accel, DEC);
  Serial.print(" M:");
  Serial.print(mag, DEC);
}

/*--------------------------------------------------*/
/*--------------------Setup and Loop----------------*/
/*--------------------------------------------------*/
// The setup function runs once when you press reset or power the board
void setup() {
  
  // Initialize serial communication at 9600 bits per second.
  Serial.begin(9600);
  
  while (!Serial) {
    ; // Wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
  }

  Serial.println("Orientation Sensor Test"); Serial.println("");

  /* Initialize the sensor */
  if(!bno.begin())
  {
    /* There was a problem detecting the BNO055 ... check your connections */
    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }

  delay(1000);

  /* Display some basic information on this sensor */
  displaySensorDetails();

  /* Optional: Display current status */
  displaySensorStatus();

  bno.setExtCrystalUse(true);
  
  // Set up two tasks to run independently.
  xTaskCreate(
    TaskBlink
    ,  (const portCHAR *)"Blink"   // A name just for humans
    ,  128  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  1  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,  NULL );

  xTaskCreate(
    TaskIMURead
    ,  (const portCHAR *) "IMURead"
    ,  128  // Stack size
    ,  NULL
    ,  2  // Priority
    ,  NULL );

  // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}

void loop()
{
  // Empty. Things are done in Tasks.
}

Video

How to Capture Temperature and Humidity Using the ESP8266 Weather Kit

In this post, I’ll show you how to set up the ESP8266 and how to capture temperature and humidity data using this device. For this project I followed the Uctronics user guide. To locate that, just type into Google, “UCTRONICS ESP8266 pdf user guide”, and it should be the first result.

Requirements

Here are the requirements:

  • Set up the ESP8266 and capture temperature and humidity data using this device.

Hardware Design

The following components are used in this project. You will need:

parts_for_esp8266

Here is the diagram of the hardware setup:

hardware_setup_esp8266PNG

Software Design

Software to read temperature and humidity comes from:
https://github.com/supprot/ArduCAM_esp8266- dht-thingspeak-logger.

Hardware Implementation

First, I did a test project to see if I can use an API to capture weather forecast data from the web on my OLED.

I downloaded and installed the Serial Driver available here: https://www.silabs.com/products/mcu/Pages/USBtoUARTBridgeVCPDrivers.aspx

device_driver_8266PNG

I installed the ESP8266 toolchain.

esp8266_toolchain

I selected the correct board NodeMCU 1.0 (ESP-12E Module): In the Arduino IDE, I went to Tools > Board: * > NodeMCU 1.0 (ESP-12E Module).

I set the correct port by going to Tools > Port and looking for a COM port labelled COM# (where # is a number).

I tested the setup of the Wifi Scanner. In the Menu of the Arduino IDE, I went to to File > Examples > ESP8266Wifi and select WiFiScan.

wifi_scan

I installed the libraries for the weather station: Sketch > Include Library… > Manage Libraries…

  • ESP8266 Weather Station Library
  • Json Streaming Parser Library
  • SSD1306 OLED Library

I opened the WeatherStationDemo example: Went to File > Examples > ESP8266 Weather Station >WeatherStationDemo and saved the new sketch with a new name.

I got the OpenWeatherMap API.

open_weather_map

I configured the weather station.

wx_station_demo

I connected the hardware.

esp8266_breadboard

I uploaded the firmware and ran the WeatherStation. The weather forecast and current weather outside were displayed on the OLED display as seen below.

Next, I collected and displayed temperature and humidity data in my home office. Here are the steps I took:

I set up Thingspeak.

thing_speak

I went to https://github.com/supprot/ArduCAM_esp8266- dht-thingspeak-logger and downloaded the code as a Zip file.

I adapted the settings to my needs.

I set up the hardware.

humidity_logger

I ran the temperature and humidity logger. Here are the results I got.

temp_results
humidity_results

Software Implementation

Here is the source code that you will need to load to your ESP8266 board. Where it says “xxxx”, that is where you enter your own values:

/**The MIT License (MIT)

Copyright (c) 2015 by Daniel Eichhorn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

See more at http://blog.squix.ch
// Modified by Addison Sears-Collins
*/

#include <ESP8266WiFi.h>
#include <DHT.h>


/***************************
 * Begin Settings
 **************************/


const char* ssid     = "xxxx";
const char* password = "xxxx";

const char* host = "api.thingspeak.com";

const char* THINGSPEAK_API_KEY = "xxxx";

// DHT Settings
#define DHTPIN D6     // what digital pin we're connected to. If you are not using NodeMCU change D6 to real pin


// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

const boolean IS_METRIC = true;

// Update every 30 seconds. Min with Thingspeak is ~20 seconds
const int UPDATE_INTERVAL_SECONDS = 60;

/***************************
 * End Settings
 **************************/
 
// Initialize the temperature/ humidity sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {      
    Serial.print("connecting to ");
    Serial.println(host);
    
    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
      Serial.println("connection failed");
      return;
    }

    // read values from the sensor
    float humidity = dht.readHumidity();
    float temperature = dht.readTemperature(!IS_METRIC);
    
    // We now create a URI for the request
    String url = "/update?api_key=";
    url += THINGSPEAK_API_KEY;
    url += "&field1=";
    url += String(temperature);
    url += "&field2=";
    url += String(humidity);
    
    Serial.print("Requesting URL: ");
    Serial.println(url);
    
    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");
    delay(10);
    while(!client.available()){
      delay(100);
      Serial.print(".");
    }
    // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }
    
    Serial.println();
    Serial.println("closing connection");


  // Go back to sleep. If your sensor is battery powered you might
  // want to use deep sleep here
  delay(1000 * UPDATE_INTERVAL_SECONDS);
}

Video

How to Develop an Arduino-Based Optical Tachometer

In this post, I’ll explain how to develop an Arduino-Based Optical Tachometer that measures the speed of a brushless propeller. The speed of the propeller must be measured using an IR Emitter/Detector pair using either Round Robin with Interrupts or Function Queue Scheduling. The RPMs (revolutions per minute) must be captured over time, downloaded to a Host, and graphed.

If you have ever driven a car or looked at the dashboard of a car, you have seen a tachometer. It is that meter on the other side of your speedometer that measures the rotation speed of your engine’s crankshaft in revolutions per minute (RPM). A tachometer is an instrument that measures the rotation speed of a shaft or disk, such as in a motor.

tachometer

Requirements

Here are the requirements I created for this project:

  • The system must execute on an Arduino.
  • The speed of a brushless propeller must be measured using an Infrared (IR) Emitter/Detector pair
  • The speed of a brushless propeller must be measured using Round Robin with Interrupts.
  • The revolutions per minute (RPMs) must be captured over time.
  • The RPMs must be downloaded to a Host.
  • Time vs. RPMs must be graphed.

Hardware Design

The following components are used in this project. You will need:

Here is the diagram of the hardware setup:

hardware_diagram_optical_tachometer

For the motor setup on the left of the image above, the current (when the transistor is “turned on”) goes from the 5V power supply, through the green wire, through the motor, and then down the yellow wire. It then passes through the collector towards the emitter, and then down to ground. If the transistor is shut off, it is no longer conducting. The residual peak current that would normally go through the transistor from the yellow wire needs somewhere to go. Instead of being forced through the non-conducting transistor (and potentially damaging it), the current redirects (shorts through the motor) and dissipates through the diode (from left to right through the diode in the schematic above) since diodes only allow current to travel in one direction.

optical_tachometer_arduino (1)
optical_tachometer_arduino (1)
optical_tachometer_arduino (2)
optical_tachometer_arduino (2)
optical_tachometer_arduino (3)

Troubleshooting Tips

  • There might be occasions where you start the motor (after you run the code in the Implementation section below), and the RPM readings start firing even though the propeller is nowhere near the IR emitter/receiver pair. If this happens to you, power the propeller/motor from a separate breadboard and Arduino.
    • You will also need to take out the pieces of code from the code below that pertain to the operation of the motor and upload that code in a separate sketch to the other Arduino (the one that will power the propeller). It is a pain in the butt to do all this and can take several extra hours or so of development time, but it will help solve this problem.
  • It is also helpful to cover the propeller blades with black electrical tape in case the IR emitter beam is passing right through the propeller blades.

Implementation

Here is the source code that you will need to load to your Arduino:

/**
 * In this program, we develop an Arduino-Based Optical Tachometer 
 * that measures the speed of a brushless propeller.
 * 
 * The Infrared LED is connected to pin 13.
 * The Infrared Phototransistor is connected to pin 2 (interrupts).
 * 
 * @version 1.0 2019-02-18
 * @author Addison Sears-Collins
 */
 
// Assign a name to the DC motor pin on the Arduino Uno
const unsigned int MOTOR_PIN = 3;

// Assign a name to the infrared LED pin on the Arduino Uno
const unsigned int IR_LED = 13;

// The number of blades on the propeller. Adjust accordingly.
const unsigned int BLADE_COUNT = 3;

// Volatile keyword is used with interrupts
// This variable is subject to change inside an interrupt
// service routine
volatile unsigned int break_number = 0;

// Flag used to stop the program
bool done = false;

// Used for capturing the time
unsigned long time;

// Used for capturing the rpm (revolutions per minute)
unsigned int rpm;

/**
 *  Function runs only once, after each powerup or reset of the Arduino Uno
 */
void setup() {

  // Open the serial port and set the data transmission rate to 9600 bits 
  // per second. 9600 is the default baud rate for Arduino Uno.
  Serial.begin(9600);

  // Show a welcome message as human-readable ASCII text
  Serial.println("PROPELLER RPM PROGRAM");
  Serial.println("This program transmits the time and RPM of a propeller.");
  Serial.println("Created by Addison Sears-Collins");
  Serial.println("");
  Serial.println("Press ! to end the program");
  Serial.println("");
  Serial.println("Please enter the desired speed of the motor.");
  Serial.println("Must be a value between 100 and 255.");
  Serial.println("");
  Serial.println("TIME, RPM");

  // The Infrared phototransistor is connected to pin 2.
  // Interrupt triggers when signal goes from HIGH to LOW
  attachInterrupt(digitalPinToInterrupt(2), isr_break_count, FALLING); 

  // Turn on the IR Led
  pinMode(IR_LED, OUTPUT);
  digitalWrite(IR_LED, HIGH);

  // Enable output for the motor
  pinMode(MOTOR_PIN, OUTPUT);

  break_number = 0;
  rpm = 0;  
 
}

/**
 *  Main function
 */
void loop() {

  display_time_and_rpm();
  
  start_motor();

  while(!done) {    

    // Update time and rpm every second
    delay(1000);

    // Don't process interrupts during this calculation
    noInterrupts();

    // Calculate the RPM. If a 3-blade propeller, 3 breaks
    // per second would yield 1 rpm, which is 60 rpm. 
    rpm = (60 * break_number) / BLADE_COUNT;

    // Display the time and rpm
    display_time_and_rpm();

    // End program if sentinel is entered 
    end_program();    
    
    break_number = 0;
    rpm = 0;

    // Restart interrupts
    interrupts();
  }
  
  // Do nothing
  while (true) {}
}

/**
  * This function starts the motor
  */
void start_motor() {

  // Wait for the user to enter the speed of the motor
  while (Serial.available() == 0){};

  // Activate the motor
  int speed = Serial.parseInt();
  if (speed >= 100 && speed <= 255) {
    analogWrite(MOTOR_PIN, speed);
  }
}

/**
  * Interrupt service routine.
  * This function counts the number of interrupts
  */
void isr_break_count() {

  break_number++;
  
}

/**
  * Function displays the time and rpm
  */
void display_time_and_rpm() {
  
  // Capture the time and covert to seconds
  time = millis() / 1000;

  // Display the time
  Serial.print(time); 
  Serial.print(" , ");
  // Println so the next line begins on a new line
  // Display the rpm
  Serial.println(rpm);   
}

/**
  * This function ends the program
  */
void end_program() {
  
  // Used for reading data from the serial monitor
  char ch;

  // Check to see if ! is available to be read
  if (Serial.available()) {     
  
    // Read the character
    // Serial.read() returns the first (oldest) character in the buffer 
    // and removes that byte of data from the buffer
    ch = Serial.read();    

    // End the program if an exclamation point is entered in the
    // serial monitor
    if (ch == '!') {
      done = true;  

      // Turn off the motor
      analogWrite(MOTOR_PIN, 0);

      // Turn off the IR LED
      digitalWrite(IR_LED, LOW);
      
      Serial.println("Finished recording RPM. Goodbye.");
    }
  }    
}

rpm_vs_time_2

Video