This is an old revision of the document!
In this scenario, you will control a servo to rotate it to the predefined positions.
Servo is under the red arrow you can see in the video stream, to let you easily observe its position.
Servo is connected to the GPIO D5.
Beginners
You need to understand how typical servomotor looks like and how it works.
The servo is controlled using predefined, precise PWM timing. Most servos share exactly the same model (0-180 degree servos), regardless of their size and power voltage. Classical, analogue servo frequency is 50Hz = 20ms period and duty cycle of the high signal is 1ms for 0deg and 2ms for 180deg. So changing the PWM duty cycle from 1ms to 2ms (5% to 10%) rotates servo. There is no need, however, to implement manually (still you can do) but you will use a predefined library instead of manually setting-up PWM. This brings features like range mapping to control servo logical way (i.e. providing angle, not duty cycle).
The library is Servo.h.
According to the algorithm of your choice to implement your code, you may hard code rotations one by one or i.e. declare a table with rotation targets and then iterate over it, so familiarity with array operations in C is essential in this case.
In this scenario, you will rotate the servo to the 0 degrees, then 45, 90, 135 and 180 degrees counter wise, then 180, 90, 0, clockwise. Note - Arrow pointing left means servo is set to 0, pointing right is 180 degrees, and when 90 degrees, arrow points down. We use LCD to get feedback about requested servo angle and to compare it with the result, but please note, it is just for information only and is not necessary to implement servo rotation.
loop() to implement infinite servo rotation as it will wear out quickly and can even burn. Servo is NOT INTENDED to rotate as a motor. Instead, implement your code to run once or twice in the setup() function. You may reset the node to restart your code without the need to recompile.
You should see the red arrow rotating as predefined in the scenario.
There are no special steps to be performed.
Include servo driver library:
#include <Servo.h>
Define a servo management class:
... Servo servo; ...
If you intend to implement your solution with rotation targets array, here is a hint on array declaration:
... int angles[] = {0,45,90,135,180,90,0}; ...
Instantiate software controller component for the LCD display:
... LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x3F for nodes 1 through 5, 8 and 9 for a 20 chars and 4 line display //LiquidCrystal_I2C lcd(0x27,20,4); // for nodes 10 and 11 only! ...
In the setup() function initialize LCD and attach servo, then implement your code:
... lcd.init(D2,D1); // initialize the lcd lcd.backlight(); lcd.home(); lcd.print("Starting"); ... servo.attach(servoPin); servo.write(0); // rotate to 0 degrees. ...
Setting servo to desired angle is as simple as calling servo.write(angle);. Default mapping is 0…180 degrees for micr
If you implement your solution using rotation targets array, here is a hint on how to implement the for loop to iterate over an array:
... for(int i=0; i<sizeof(angles); i++) { ...
Keep loop() dummy, empty method not to overheat the servo or to wear out it's gears:
... void loop() { }
bmp.readPressure() function returns air pressure in Pa. You must convert it hPa, dividing by 100.
To convert float into the string with formatting use sprintf function:
... sprintf(buffer,"%4.2f hPa",pres); ... delay(5000); ...
sprintf uses number of wildcards that are rendered with data. Refer to the c/c++ documentation on sprintf. Here %4.2f
means: having float number render it to string using four digits before decimal point and two after it. Air pressure readings should be somewhere between 890 and 1060 hPa.
delay(time) is measured in milliseconds.
Observe air pressure readings on the LCD. Note - small oscillations are natural - you can implement moving average (i.e. of 10 subsequent reads, FIFO model) to “flatten” readings.