Random Sensors: Adafruit MMA8451 Accelerometer & KY-028 Digital Temperature Sensor

For my project, I had a KY-028 Digital Temperature Sensor and an Adafruit MMA8451 Accelerometer to work with, so I decided to make a device that moves if it detects heat! Here’s some pictures/videos that I took while I was working:

KY-028 Digital Temperature Sensor

The KY-028 detects data from the environment on temperature. This data can be interpreted as either an analog value or a digital value. The analog value represents the sensor’s actual reading, and it decreases as temperature increases. For room temperature, I found that this value tended to be around 220. The digital value is determined by the sensor’s threshold, which can be altered by using a potentiometer. If the threshold is reached, the digital value will be HIGH, and it will be LOW otherwise.

It has four pins:

  • The A0 pin should be connected to an analog pin on the Arduino.
  • The G pin should be connected to ground.
  • The + pin should be connected to the 5V pin on the Arduino.
  • The D0 pin should be connected to a digital pin on the Arduino.

Starter code for KY-028:

#define KY028_D0 2
#define KY028_A0 A0

int digitalValue;
int analogValue;

void setup() {

    Serial.begin(9600);

    pinMode(KY028_D0, INPUT);
    pinMode(KY028_A0, INPUT);    

} // setup

void loop() {  

    digitalValue = digitalRead(KY028_D0);
    analogValue = analogRead(KY028_A0);

    Serial.print("Digital Temp Value: "); 
    Serial.print(digitalValue); 
    Serial.print("\tAnalog Temp Value: ");      
    Serial.println(analogValue);
    
    delay(100);

} // loop

External resource:

https://sensorkit.joy-it.net/en/sensors/ky-028#:~:text=This%20module%20contains%20an%20NTC,up%20to%20%2B125%C2%B0C.

Adafruit MMA8451 Accelerometer

The Adafruit MMA8451 Accelerometer is a sensor that measures its own acceleration and orientation. If you plan to use this sensor, make sure to download the Adafruit_MMA8451 external library and any of its dependencies.

In order to obtain the sensor’s measurements, you’ll need to create an Adafruit_MMA8451 object. If all the sensor pins are connected to the right places, the accelerometer will be recognized and usable in the code.

Acceleration is measured independently in the x, y, and z directions. Each component can be obtained from a sensor event. These values are represented both as raw count values and as values converted into SI units (m/s^2). I found that there wasn’t much value in recording the raw values as the converted values are much more usable in general.

Orientation is measured as an enumerated set of values, which can be obtained from the Adafruit_MMA8451 object. This value will differ based on the movement of the accelerometer, and it will be some combination of these three traits:

  • Portrait/Landscape
  • Up/Down/Left/Right
  • Front/Back

The sensor has 8 pins, but you really only need four to get it to work:

  • The VIN pin should be connected to the 5V pin on the Arduino.
  • The GND pin should be connected to ground.
  • The SCL pin should be connected to the A5 pin on the Arduino.
  • The SDA pin should be connected to the A4 pin on the Arduino.

I found that this website was a really good resource for learning how to use the accelerometer. Here’s a link if you want to learn more:

https://learn.adafruit.com/adafruit-mma8451-accelerometer-breakout/wiring-and-test

However, the example code on the website is a little bloated, so I reduced it to its most essential parts:

#include <Adafruit_MMA8451.h>

void printOrientation();

Adafruit_MMA8451 mma = Adafruit_MMA8451();

void setup() {

    Serial.begin(9600); 

    Serial.println("Adafruit MMA8451 test!");
  
    // Check if accelerometer is connected
    if (!mma.begin()) {
        Serial.println("Couldnt start");
        while (1);
    
    } // if

    Serial.println("MMA8451 found!");
    
    mma.setRange(MMA8451_RANGE_2_G);
    
    Serial.print("Range = "); 
    Serial.print(2 << mma.getRange());  
    Serial.println("G");

} // setup

void loop() {  

    /* Get a new sensor event */ 
    sensors_event_t event; 
    mma.getEvent(&event);

    int x = event.acceleration.x;
    int y = event.acceleration.y;

    /* Display the results (acceleration is measured in m/s^2) */
    
    Serial.print("X: \t"); Serial.print(x);   
    Serial.print("\t");
    Serial.print("Y: \t"); Serial.print(y); 
    Serial.print("\t");
    Serial.print("Z: \t"); Serial.print(event.acceleration.z); 
    Serial.print("\t"); Serial.println("m/s^2");

    printOrientation();
    
    delay(100);

} // loop

void printOrientation() {

    /* Get the orientation of the sensor */
    uint8_t o = mma.getOrientation();
    
    switch (o) {

        case MMA8451_PL_PUF: 
            Serial.println("Portrait Up Front");
            break;

        case MMA8451_PL_PUB: 
            Serial.println("Portrait Up Back");
            break;    

        case MMA8451_PL_PDF: 
            Serial.println("Portrait Down Front");
            break;

        case MMA8451_PL_PDB: 
            Serial.println("Portrait Down Back");
            break;

        case MMA8451_PL_LRF: 
            Serial.println("Landscape Right Front");
            break;

        case MMA8451_PL_LRB: 
            Serial.println("Landscape Right Back");
            break;

        case MMA8451_PL_LLF: 
            Serial.println("Landscape Left Front");
            break;

        case MMA8451_PL_LLB: 
            Serial.println("Landscape Left Back");
            break;

    } // switch

} // printOrientation

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *