Arduino provides built-in trigonometric functions that are part of the standard C math library (math.h). These functions allow you to perform calculations involving angles, sine waves, or geometric computations. Trigonometric functions are especially useful in projects involving robotics, wave generation, or sensor data.
This tutorial explains how to use trigonometric functions in Arduino and provides practical examples.
Table of Contents
1. What Are Trigonometric Functions?
Trigonometric functions relate the angles of a triangle to the lengths of its sides. These functions are essential for working with circular motion, waveforms, and geometric calculations.
Key Functions:
- Sine (sin): Opposite / Hypotenuse
- Cosine (cos): Adjacent / Hypotenuse
- Tangent (tan): Opposite / Adjacent
2. Available Trigonometric Functions in Arduino
Arduino includes the following trigonometric functions from the <math.h> library:
2.1 sin(x)
- Computes the sine of angle x in radians.
- Returns a value between -1 and 1.
Example:
float result = sin(PI / 2); // Sine of 90 degrees (PI/2 radians)
2.2 cos(x)
- Computes the cosine of angle x in radians.
- Returns a value between -1 and 1.
Example:
float result = cos(0); // Cosine of 0 degrees
2.3 tan(x)
- Computes the tangent of angle x in radians.
- Returns a value, but beware of undefined values (e.g., 90 degrees).
Example:
float result = tan(PI / 4); // Tangent of 45 degrees
2.4 asin(x)
- Computes the arcsine (inverse sine) of x.
- Returns an angle in radians between -PI/2 and PI/2.
Example:
float angle = asin(0.5); // Arcsine of 0.5
2.5 acos(x)
- Computes the arccosine (inverse cosine) of x.
- Returns an angle in radians between 0 and PI.
Example:
float angle = acos(0.5); // Arccosine of 0.5
2.6 atan(x)
- Computes the arctangent (inverse tangent) of x.
- Returns an angle in radians between -PI/2 and PI/2.
Example:
float angle = atan(1); // Arctangent of 1
2.7 atan2(y, x)
- Computes the arctangent of y/x and considers the quadrant.
- Useful for converting Cartesian coordinates to polar coordinates.
Example:
float angle = atan2(1, 1); // Arctangent of y=1, x=1
3. Angle Units (Degrees vs. Radians)
Trigonometric functions in Arduino expect angles in radians. If your input is in degrees, you need to convert it to radians:
Formula:
radians = degrees * (PI / 180); degrees = radians * (180 / PI);
Example:
float degrees = 90; float radians = degrees * (PI / 180); // Convert to radians float result = sin(radians);
4. Practical Examples
4.1 Calculate the Height of a Triangle
Given the base and angle of a right triangle, calculate its height using sine.
Code:
void setup() { Serial.begin(9600); float base = 10.0; // Base length float angleDegrees = 30.0; // Angle in degrees float angleRadians = angleDegrees * (PI / 180); float height = base * tan(angleRadians); // Height = base * tan(angle) Serial.print("Height: "); Serial.println(height); } void loop() { // Empty }
4.2 Generate a Sine Wave
Use sin() to generate a sine wave for controlling an LED’s brightness.
Code:
const int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { for (int i = 0; i < 360; i++) { float angle = i * (PI / 180); // Convert degrees to radians int brightness = (sin(angle) + 1) * 127.5; // Map sine to 0-255 analogWrite(ledPin, brightness); // Set LED brightness delay(10); } }
4.3 Calculate Angles for a Robotic Arm
Given the coordinates of a target point (x, y), calculate the angle to reach it.
Code:
void setup() { Serial.begin(9600); float x = 5.0; // Target x-coordinate float y = 5.0; // Target y-coordinate float angleRadians = atan2(y, x); // Calculate angle in radians float angleDegrees = angleRadians * (180 / PI); // Convert to degrees Serial.print("Angle: "); Serial.print(angleDegrees); Serial.println(" degrees"); } void loop() { // Empty }
4.4 Smooth LED Brightness with a Sine Wave
Smoothly increase and decrease LED brightness using a sine function.
Code:
const int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { for (int i = 0; i < 360; i++) { float angle = i * (PI / 180); // Convert degrees to radians int brightness = (sin(angle) + 1) * 127.5; // Map sine to 0-255 analogWrite(ledPin, brightness); // Set LED brightness delay(10); // Adjust speed of fading } }
5. Best Practices for Using Trigonometric Functions
- Use Radians:
- Always convert degrees to radians for trigonometric functions.
- Validate Input Ranges:
- Ensure input values for asin() and acos() are within the range -1 to 1.
- Optimize Floating-Point Operations:
- Trigonometric functions are computationally intensive. Use lookup tables for repeated calculations in real-time applications.
- Test Boundary Conditions:
- Verify behavior near edge cases like 90° (PI/2 radians) where some functions (e.g., tan()) may produce undefined results.
- Use atan2() for Quadrant-Aware Calculations:
- Prefer atan2(y, x) over atan(y / x) for angle calculations.
Conclusion
Trigonometric functions in Arduino provide powerful tools for calculating angles, generating waveforms, and solving geometric problems. By understanding how these functions work and using them effectively, you can implement a wide range of applications in robotics, electronics, and more.
This tutorial covered the most commonly used trigonometric functions with practical examples. For more details, visit the official Arduino reference.