Arduino - Trigonometric Functions
Arduino - Trigonometric Functions
Section titled “Arduino - Trigonometric Functions”Trigonometry is the key to unlocking motion, orientation, and oscillation in your projects. Whether you’re building a robotic arm, a self-balancing robot, or creating smooth visual effects, functions like sin(), cos(), and tan() are indispensable. The Arduino platform provides these standard C++ math functions via the <cmath> library, which is included automatically in your sketches.
Core Trigonometric Functions
Section titled “Core Trigonometric Functions”These functions operate on angles in radians, not degrees. The Arduino environment provides handy helper functions to convert between the two.
// Standard trigonometric functionsfloat sin(float rad); // Returns the sine of an angle in radiansfloat cos(float rad); // Returns the cosine of an angle in radiansfloat tan(float rad); // Returns the tangent of an angle in radians
// Inverse trigonometric functions (return angle in radians)float asin(float value); // Returns the arc sine for a value between -1.0 and 1.0float acos(float value); // Returns the arc cosine for a value between -1.0 and 1.0float atan(float value); // Returns the arc tangentfloat atan2(float y, float x); // A more robust arc tangent, often used for converting coordinates to an angle
// Arduino-specific angle conversion helpersfloat radians(float deg); // Converts degrees to radiansdouble degrees(double rad); // Converts radians to degreesPro-Tip: float vs. double
Section titled “Pro-Tip: float vs. double”On many classic Arduino boards (like the UNO, Nano, Mega, based on AVR microcontrollers), the double data type is a 32-bit floating-point number, exactly the same as float. Using double provides no extra precision and can be misleading. For these boards, it’s best practice to stick with float. On more powerful 32-bit boards (like the Arduino Due, ESP32, or MKR series), double provides true 64-bit precision.
Practical Example: The “Breathing” LED
Section titled “Practical Example: The “Breathing” LED”Let’s use the sin() function to create a smooth, organic pulsing effect on an LED. A sine wave naturally oscillates between -1.0 and 1.0. We can scale this value to control the brightness of an LED connected to a PWM pin.
constexpr int LED_PIN = 9; // A PWM-capable pin is required (e.g., 3, 5, 6, 9, 10, 11 on UNO)float angle = 0.0; // The current angle for the sine function, in radiansfloat increment = 0.02; // How fast the angle changes; controls the breathing speed
void setup() { // We don't need to initialize the pin for analogWrite, but it's good practice. pinMode(LED_PIN, OUTPUT); Serial.begin(9600); // For optional debugging}
void loop() { // 1. Calculate the sine value. It will oscillate between -1.0 and 1.0. float sinValue = sin(angle);
// 2. Map the sine value to the LED brightness range (0-255). // The map() function in Arduino works with integers, so we'll do it manually for floats. // First, shift the range from [-1.0, 1.0] to [0.0, 2.0] float shiftedValue = sinValue + 1.0; // Next, scale it to the [0, 255] range for PWM int brightness = (int)(shiftedValue * 127.5);
// 3. Set the LED brightness analogWrite(LED_PIN, brightness);
// 4. Increment the angle for the next loop iteration to create the animation angle += increment;
// Optional: Print values to the Serial Plotter to visualize the wave! // Serial.println(brightness);
delay(10); // A small delay to control the animation speed}Real-World Application Scenarios
Section titled “Real-World Application Scenarios”- Robotics: Calculating the (X, Y) position of a robotic arm’s gripper based on the angles of its joints (known as forward kinematics).
pos_x = L1 * cos(angle1) + L2 * cos(angle1 + angle2); - Navigation: Using
atan2(y_diff, x_diff)to calculate the heading (angle) your robot needs to travel to reach a target coordinate. - Data Visualization: Creating circular or wave-like patterns on an LED matrix or a graphical display.
- Audio Synthesis: Generating simple sound waves by outputting sine values to a DAC (Digital-to-Analog Converter).
Further Learning
Section titled “Further Learning”To better understand these concepts, try visualizing them! Use the Arduino IDE’s Serial Plotter (Tools > Serial Plotter) with the example code above (uncomment the Serial.println line). It will draw the brightness value over time, revealing the smooth sine wave.