We have some trigonometry functions in the Arduino. These are sine (sin), cosecant (cos) and tangent (tan). This make our calculation even simpler. Using this we can implement some other trigonometry function also. We will see that later. Let’s see that one by one.
Table of Contents
Sine (sin) function
sin function calculates the sine of an angle and angle should be in radian. This function returns the result between 1 and -1. Syntax of sin function looks like this sin(rad). Let’s see the example code. But we have to learn how to convert degree into radian. to convert an angle from degree to radian, we have to multiply p/180. Let’s convert 90° into radian.
90 x π/180
90 x (22/7)/180
(90 x 22)/(7 x 180)
1.5714
So, 90 degree equivalent 1.5714 in radian.
Example: –
void setup() { // put your setup code here, to run once:
Serial.begin(9600); //initializing serial communication
Serial.println(sin(1.5714)); //calculating sine of 90 degree
Serial.println(cos(1.5714)); //calculating cosine of 90 degree
Serial.println(tan(1.5714)); //calculating tangent of 90 degree
}
void loop() {
// put your main code here, to run repeatedly:
}
Output:
1.00
-0.00
-1656.40 (which is very big number for Arduino. Almost infinity)
cos function
cos function calculates the cosine of an angle given to it as argument. Angle should be in radian. The result will be in between 1 and -1.
Syntax of this function looks like this –
cos(rad)
Let’s see an example code –
void setup() { //put your setup code here, to run once:
Serial.begin(9600); //initializing the serial communication
Serial.println(cos(1.5714)); //calculating the value of cos90
Serial.println(cos(0)); //calculating the value of cos0
}
void loop() {
// put your main code here, to run repeatedly:
}
Output:
-0.00
1.00
So as you can see that value of cos90° is 0 and the value of the cos0° is 1, which is correct.
tan function
tan function calculates the tangent of an angle given to it as argument. Angle should be in radian. The result will be in between -¥ and ¥.
Syntax of this function looks like this –
tan(rad)
As we know that tan0° gives 0 and tan45° gives 1. So let’s convert these angles in radian. 0° in radian is 0. Now convert let’s convert the 45° in radian by using the method we used previously.
45 x π/180
45 x (22/7)/180
(45 x 22)/(7 x 180)
0.7857
So, 45 degree in the radian is 0.7857.
Let’s see an example code –
void setup() { //put your setup code here, to run once:
Serial.begin(9600); //initializing the serial communication
Serial.println(tan(0.7857)); //calculating the value of tan45
Serial.println(tan(0)); //calculating the value of tan0
}
void loop() {
// put your main code here, to run repeatedly:
}
Output:
0.00
1.00
So as you can see that value of tan0° is 0 and the value of the tan45° is 1, which is correct.
Convert angle from radian to degree in Arduino
As we have seen that we have to enter the radian value as argument to trigonometry functions to find the sin, cos or tangent of any angle in Arduino. So now lets see that how we can convert the angle form radian form to the degree form. We know the formula for do that. So, we will define a function that will do these calculation for us. Let’s see the code.
void setup() { //put your setup code here, to run once:
Serial.begin(9600); //initializing the serial communication
Serial.println(cos(degree(90))); //calculating the value of cos90
Serial.println(sin(degree(90))); //calculating the value of sin90
Serial.println(tan(degree(60))); //calculating the value of tan60
}
void loop() {
// put your main code here, to run repeatedly:
}
float degree(double deg){ //function declaration
return (deg*22)/(7*180); //calculation for the radian to degree conversion
}
You can see that I have defined a function called degree. It will take the degree value as argument which will be double data type. It will convert it to radian using given formula and return the radian value as float after conversion. Let’s see the output of this code.
Output:
-0.00
1.00
1.73
If you want to learn more about Arduino and programming language then follow this playlist – https://electronics-fun.com/category/arduino-programming-tutorial/