This is an old revision of the document!


B7: Controlling servo

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.

Target group

Beginners

Prerequisites

The servo is controlled using predefined, precise PWM timing. You will use a predefined library instead of manually setting-up PWM:

#include <Servo.h>

Scenario

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.

Result

You should see the red arrow rotating as predefined in the scenario.

Start

There are no special steps to be performed.

Steps

Step 1

Include servo driver library:

#include <Servo.h>
Step 2

Instantiate software controler 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!
 
Adafruit_BMP280 bmp;
Step 3

Declare a buffer for sprintf function and floating point value (single precision is enough) for storing last air pressure reading:

char buffer [11];
float pres;
Step 4

Initialize LCD and BMP sensors:

...
  lcd.init(D2,D1);     // initialize the lcd 
  lcd.backlight();
  lcd.home();
...
  if(!bmp.begin(0x76))
  {
    lcd.print("Bosch Sensor Error!");
    delay(1000);
  };
  lcd.home();
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
...

The BMP sensor address is 0x76 and, if not initialised, display error on the LCD screen.

Step 5

Read in the loop:

...
  pres = bmp.readPressure()/100;
...

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.

Result validation

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.

en/iot-open/remotelab/sut/generalpurpose2/b7.1588093056.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0