Lesson 88: Using tan() Function
Learn how tan() works with radians, slope-style ratios, and angle-based control math in Arduino.
Progress indicator
Lesson 88 of 88
Learning Objectives
- Understand what tan() means as a ratio of sin(angle) and cos(angle).
- Use tan() syntax correctly with radians in Arduino sketches.
- Recognize where tan() can become very large and why range limiting is important.
- Map tan() output safely into PWM-friendly hardware ranges.
- Avoid common tan() mistakes near PI/2 and other unstable angles.
- Build a practical mental model for handling unstable math safely in embedded code.
Concept Explanation
What is tan()
tan() computes the tangent of an angle. In math terms: tan(angle) = sin(angle) / cos(angle).
Tangent often behaves like a slope-like value. It changes slowly near 0, but grows very quickly as angle approaches points where cosine becomes near zero.
Compared with sin() and cos(), tan() needs extra care because its output can spike. In embedded systems, this means you usually combine tan() with angle limits and value clamps.
tan() Syntax
Basic syntax: tan(angleInRadians)
tan(0)returns 0tan(PI / 4)returns near 1tan(-PI / 4)returns near -1
Near PI/2 and -PI/2, tan values can become extremely large, so safe examples typically avoid these points directly.
How tan() Works
- Take an angle input in radians.
- Compute sine and cosine behavior internally.
- Return their ratio as a floating-point value.
Because tangent is a ratio, output can become very large when the denominator (cosine) is near zero.
Practical rule: treat tan() as a potentially high-gain function. Keep input range controlled and always post-process output before using it to drive hardware.
Radians vs Degrees
tan() expects radians. Convert degrees first: radians = degrees * PI / 180.0.
- 0 degrees = 0 rad
- 45 degrees = PI/4 rad
- 90 degrees = PI/2 rad (tan is unstable here)
If your output looks unexpectedly huge or noisy, check angle units first. Degree/radian mismatch is a top cause of tangent bugs.
tan() Return Value
Unlike sin() and cos(), tangent is not limited to -1..1. It can return very large positive or negative values near unstable angles.
For hardware use, clamp the output into a safe sub-range before mapping to PWM or control values.
In this lesson example, we clamp to -2.0..2.0, then map to PWM. This keeps LED behavior smooth and predictable.
When to Use tan()
- Slope-style calculations and angle-to-ratio conversions.
- Geometry-based embedded logic where ratio behavior is required.
- Educational trigonometry experiments with controlled angle ranges.
- Cases where you explicitly handle out-of-range and near-singularity behavior.
Real-life use case: converting angle to slope for line-following or orientation math, where a ratio is needed and input range is already controlled.
Example Code
This example uses tan() in a safe angle window and maps the result to LED PWM.
const int LED_PIN = 2;
float angleRad = 0.0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
// Keep angle in a safe range to avoid tan() singularity near PI/2.
angleRad = angleRad + 0.08;
if (angleRad > 1.30) {
angleRad = -1.30;
}
float slopeLike = tan(angleRad);
// Clamp tan output before mapping to PWM.
float clamped = constrain(slopeLike, -2.0, 2.0);
float normalized = (clamped + 2.0) / 4.0; // 0.0..1.0
int pwmValue = (int)(normalized * 255.0);
pwmValue = constrain(pwmValue, 0, 255);
analogWrite(LED_PIN, pwmValue);
Serial.print("angle(rad): ");
Serial.print(angleRad, 2);
Serial.print(" | tan: ");
Serial.print(slopeLike, 3);
Serial.print(" | pwm: ");
Serial.println(pwmValue);
delay(120);
}Example Code Explanation
- Increment angle in small steps to observe smooth tangent progression.
- Limit angle between -1.30 and 1.30 rad to avoid tan() instability near PI/2.
- Compute
tan(angleRad)and store it as a floating-point value. - Clamp tangent output to -2.0..2.0 for predictable PWM mapping.
- Normalize clamped value to 0..1 and scale to 0..255.
- Write PWM to LED pin and print debug values to Serial monitor.
- When tan is near 0, PWM stays near mid-brightness; higher/lower tan values push LED brighter or dimmer within the safe clamp range.
- The selected angle window avoids unstable spikes, so the simulator and hardware remain readable.
What Happens Inside
- CPU updates angle value in RAM each loop.
- Math library evaluates tangent for the current angle.
- Program clamps large values to avoid unstable output spikes.
- Clamped value is mapped into PWM range for hardware control.
- PWM register updates LED brightness accordingly.
- Serial output logs angle/tan/PWM values for visibility and debugging.
Common Mistakes with tan()
- Passing degree values directly without converting to radians.
- Using angles too close to PI/2 where tan() can spike sharply.
- Mapping raw tan() output directly to PWM without clamping.
- Using large angle steps that skip important behavior and cause jumps.
- Assuming tan() has a fixed range like sin() or cos().
Best Practices for tan()
- Keep angle in radians and document conversion in code comments.
- Avoid singularity zones by limiting angle range before tan().
- Clamp and normalize tangent output before hardware mapping.
- Use small angle increments for smooth and understandable output.
- Print debug values while tuning to catch spikes early.
Try it now
Open the simulator workspace and observe safe tan() mapping behavior with angle limits.