Experiment #103 How to Modify the Project to Work with Face Cognitive Service and Servo Motor 👩‍💻📙

1.     Create the Azure Face Resource


First, we are going to create the Face Resource from our Azure portal under Cognitive Services group:



We must use the Face API Key and the Face API Endpoint credentials in our project.


2.     Add a Unique ID for Shared Whitelist


We will change the way IDs are made in the hackster.io project. It was created on the fly and randomly every time the application is launched:


string id = Guid.NewGuid().ToString();


Now, we are going to create a unique ID because we want a shared Person Group between desktop and IoT device applications:


public const string FixedWhiteListID = "FIXED-ID";
string id = GeneralConstants.FixedWhiteListID;


This way, we can use the desktop app to register, update and delete users (Persons and Faces) and verify their identity within the IoT device app.

You can learn how to use Face Service by checking the Face API Documentation.


3.     Set Up the IoT Device GPIO Pins


Then, we need to set up the General Purpose In and Out (GPIO) pins to control the doorbell button, the LED light and the servo motor from the Raspberry Pi 2 model B device:


public static class GpioConstants
    // The GPIO pin that the doorbell button is attached to.
    public const int ButtonPinID = 5;

    // The GPIO pin that the door LED light is attached to.
    public const int DoorLockPinID = 4;

    // The GPIO pin that the door servo motor is attached to.
    public const int ServoMotorPinID = 18;


We can check out those pin numbers at the Pin Mappings section under the Hardware guidance of the  Windows IoT documentation:



4.     Control Servo Motor with PWM Function


The GPIO pins we are using accept only two states: High voltage (1) and Low voltage (0):


namespace Windows.Devices.Gpio
    public enum GpioPinValue
        Low = 0,
        High = 1


That is great to control buttons and LED lights that also have two states: On and Off, but the servo motor has a lot of states between Moving and Stopped.

Because of that we need a Pulse Width Modulation (PWM) function to control the angle of the door:


public static void PWM_R (int pinNumber)
    var stopwatch = Stopwatch.StartNew();

    workItemThread = Windows.System.Threading.ThreadPool.RunAsync(
        (source) =>
            ManualResetEvent mre = new ManualResetEvent(false);

            ulong pulseTicks = ((ulong)(Stopwatch.Frequency)/1000)*2;
            ulong delta;
            var startTime = stopwatch.ElapsedMilliseconds;
            while (stopwatch.ElapsedMilliseconds - startTime <= 300)
                ulong starttick = (ulong)(stopwatch.ElapsedTicks);
                while (true)
                    delta = (ulong)(stopwatch.ElapsedTicks) - starttick;
                    if (delta > pulseTicks) break;
                starttick = (ulong)(stopwatch.ElapsedTicks);
                while (true)
                    delta = (ulong)(stopwatch.ElapsedTicks) - starttick;
                    if (delta > pulseTicks * 10) break;

Stay up to date!

Leave a comment