搜索代码

How to Measure Tilt Angle Using a SCA60C Angle Sensor and Take Action Based on the Angle

How to Measure Tilt Angle Using a SCA60C Angle Sensor and Take Action Based on the Angle

Mastering Angle Measurement with the SCA60C Tilt Sensor and Arduino

In a detailed tutorial, robotics expert Ahmad Shamshiri of Robojax introduces the VTI Technologies SCA60C tilt sensor, a versatile and inexpensive module for measuring angles. This article, written on March 17, 2020, in Ajax, Ontario, Canada, is based on his video tutorial. This sensor is an excellent tool for a wide range of applications, especially in robotics where knowing the precise orientation of a component is often critical.

tilt angle sensor

This article will walk you through the features of the SCA60C module, how to wire it to an Arduino, and how to use the provided code to get it up and running.

Sensor Overview

The core of this module is the SCA60C angle sensor, which can measure angles from 0 to 180 degrees. It accomplishes this by outputting an analog voltage that changes based on its tilt. This voltage can be read by a microcontroller like an Arduino and converted into a precise angle. The sensor boasts a resolution of one degree, meaning it cannot measure fractional degrees.

Beyond simple angle measurement, the module includes a handy alarm feature. It has two onboard LEDs and corresponding digital outputs (D1 and D2) that can be triggered when the sensor deviates from a pre-set "flat" position. This is managed by an LM393 comparator chip and can be calibrated using two small potentiometers.

As noted in the video, these modules are widely available from online retailers like AliExpress, Amazon, and eBay.

Wiring the SCA60C to an Arduino

Connecting the sensor to your Arduino is simple. The video provides a clear demonstration at 00:05:07.

  • VCC: Connect this to the 5V pin on your Arduino.
  • GND: Connect this to any GND (Ground) pin on the Arduino.
  • VO (Voltage Out): This is the analog output that provides the angle reading. Connect it to an analog input pin on your Arduino. The provided code uses pin A0.
  • D1 Out & D2 Out (Optional): These are for the alarm functions. If you wish to use them, connect them to two other analog pins. The sample code uses A1 for D1 and A2 for D2. If you don't need the alarm feature, you can leave these disconnected to save pins for other uses.

Diving into the Arduino Code

The provided code is well-structured and easy to understand. It reads the sensor, processes the data, and displays it on the serial monitor.

Global Variables & Setup

At the beginning of the sketch, essential variables are defined:

const int anglePin =A0;
int angle =0;
const int angleCompensate =0;

const int d1Pin =A1;
int d1State =LOW;
int d1Delay =0;

const int d2Pin =A2;
int d2State =LOW;
int d2Delay =0;

char *lables[]={"Angle","Alarm D1","Alarm D2"};

The angleCompensate variable is particularly powerful. As shown at 00:08:49 in the video, you can change this value to recalibrate the sensor's "zero" point. For example, if you mount the sensor vertically and want that to be your 0-degree reference, you can set angleCompensate to 90 or -90. This provides immense flexibility for custom installations.

The setup() function simply initializes serial communication so you can view the output:

void setup()
{
  Serial.begin(9600);
  Serial.println("Robojax SCA60C Angle Sensor");
}

Core Functions

The magic happens in the getAngle() function:

void getAngle()
{
   angle =map(analogRead(A0), 96, 927, -90, 90) + angleCompensate;
}

This single line reads the raw analog value from pin A0 and uses the map() function to convert it. The values 96 and 927 represent the raw analog readings that correspond to the sensor's -90 and +90 degree limits. The function scales the input value into this degree range and then adds the angleCompensate value for calibration.

The d1Alarm() and d2Alarm() functions are used to read the state of the alarm pins. As demonstrated at 00:11:00, when the sensor is tilted enough to trigger an alarm LED, the corresponding digital output pin's voltage drops. This code reads that voltage, and if it falls below a 3.5V threshold, it sets the corresponding state variable (e.g., d1State) to HIGH. You can use this state change in your main loop to trigger other actions.

void d1Alarm()
{
  float vo = analogRead(d1Pin) * (5.0 / 1023.0);
  if(vo < 3.5)
  {
    d1State =HIGH;
    delay(d1Delay);    
  }else{
    d1State =LOW;    
  }
}

Putting It All Together

The main loop() function orchestrates everything:

void loop(){
  getAngle();
  d1Alarm();
  d2Alarm();

  Serial.print(lables[0]);
  Serial.print(":");
  Serial.print(angle);
  Serial.println("\xC2\xB0");

  delay(100);
}

In each cycle, it calls the functions to get the angle and check the alarms. It then neatly prints the current angle to the serial monitor, followed by a degree symbol. A 100-millisecond delay keeps the output readable and prevents flooding the serial port.

By following this guide and the excellent visual demonstration in the Robojax video, you can easily incorporate the SCA60C tilt sensor into your next Arduino project, opening up new possibilities for control and interaction.


Video Chapters

  • - Introduction to the SCA60C Angle Sensor
  • - Use Cases for Angle Sensing in Robotics
  • - Explanation of the Module Hardware
  • - Explanation of Module Pins
  • - Wiring to an Arduino
  • - Code Explanation
  • - Live Demonstration of Angle Measurement
  • - How to Use 'angleCompensate' to Calibrate
  • - Demonstration of the Alarm Feature
  • - Using the Sensor Without the Alarm Pins
307-Basic code to control the direction of rotation of a motor with two relays
语言: C++
/*
 * Tile Angle Sensor SCA60C1 N1000060
 * It displays the angle from 0 to 180 degrees.
 * 
 * Watch video instructions for this code: https://youtu.be/5DjO8Fo_AtE
 * 
 * Written by Ahmad Shamshiri on Mar 17, 2020 at 20:20 
 
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
  Need wiring diagram for this code: 
  Purchase My Arduino course on Udemy.com http://robojax.com/L/?id=62
 * 

 * Get this code and other Arduino codes from Robojax.com
 * Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
 * all in one place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

const int controPin = 2;//pin to for control
const int waitTime = 2000;//2000mS or 2 seconds wait
const int myAnle = 45;
 
const int anglePin =A0;
int angle =0;
const int angleCompensate =0;

const int d1Pin =A1;
int d1State =LOW;
int d1Delay =0;//watch 

const int d2Pin =A2;
int d2State =LOW;
int d2Delay =0;

char *lables[]={"Angle","Alarm D1","Alarm D2"};

void setup()

{

  Serial.begin(9600);          //  setup serial
  Serial.println("Robojax SCA60C Angle Sensor");
  pinMode(controPin, OUTPUT);
  digitalWrite(controPin, LOW);//LOW is initial state of buzzer. Set to HIGH if  you want to it the other way

}



void loop(){
  //Robojax Tilt Angle Sensor
  //Watch video for details: https://youtu.be/5DjO8Fo_AtE
  getAngle();
  d1Alarm();
  d2Alarm();
  
  //take action here
  if(angle >=myAnle)
  {
	  //turn the buzzer ON
	  digitalWrite(controPin, HIGH);//HIGH or LOW as desired
	  delay(waitTime);// keep it waiting for the waitTime
  }
  
  // print out the value you read:
  Serial.print(lables[0]);// 
  Serial.print(":");
  Serial.print(angle);
  Serial.println("\xC2\xB0");//degree symbol
//  Serial.print("\t");
//  Serial.print(lables[1]);
//  Serial.print(":");  
//  Serial.print(d1State);
//  Serial.print("\t");
//  Serial.print(lables[2]);
//  Serial.print(":");
//  Serial.println(d2State);  
//  Serial.println("======");
  delay(100);
//Robojax Tilt Angle Sensor
}

void getAngle()
{
 //watch video instructions: https://youtu.be/5DjO8Fo_AtE
   angle =map(analogRead(A0), 96, 927, -90, 90) + angleCompensate; 
 
}

/**************************************/
/*! 
    @brief  Get the state of D1 Alarm. HIGH if triggered.
    @param  no parameter
    @returns returns nothing
    Written by Ahmad Shamshiri on Mar 17, 2020 at 20:20 in Ajax, Ontario, Canada.
*/
/**************************************/
void d1Alarm()
{
  //watch video for details: https://youtu.be/5DjO8Fo_AtE
  //
  float vo = analogRead(d1Pin) * (5.0 / 1023.0);
  if(vo < 3.5)
  {
    d1State =HIGH;
    delay(d1Delay);    
  }else{
    d1State =LOW;    
  }

}//d1Alarm() end


/**************************************/
/*! 
    @brief  Get the state of D1 Alarm. HIGH if triggered.
    @param  no parameter
    @returns returns nothing
    Written by Ahmad Shamshiri on Mar 17, 2020 at 20:20 in Ajax, Ontario, Canada.
*/
/**************************************/
void d2Alarm()
{
  //watch video for details: https://youtu.be/5DjO8Fo_AtE
  //
  float vo = analogRead(d2Pin) * (5.0 / 1023.0);

  if(vo < 3.5)
  {
    d2State =HIGH;
    delay(d2Delay);    
  }else{
    d2State =LOW;    
  }  

}//d1Alarm() end

资源与参考

文件📁

没有可用的文件。