Control A Servo With A Push Button Using Meadow

Photo of jorgedevs

Made by jorgedevs

About the project

Learn how to control a servo using a push button with just a few lines of code using a Meadow F7 Micro board and Meadow. Foundation library.

Project info

Difficulty: Easy

Platforms: MicrosoftSparkFunMeadowWilderness Labs

Estimated time: 1 hour

License: Apache License 2.0 (Apache-2.0)

Items used in this project

Hardware components

Tiny Breadboard Tiny Breadboard x 1
Resistor Network - 10K Ohm (6-pin bussed) Resistor Network - 10K Ohm (6-pin bussed) x 1
Momentary Pushbutton Switch - 12mm Square Momentary Pushbutton Switch - 12mm Square x 1
Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM x 1
Jumper Wire Kit - 140pcs Jumper Wire Kit - 140pcs x 1
Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM x 1

Software apps and online services

Microsoft Visual Studio 2019 Microsoft Visual Studio 2019

Story

In this project we're going to control a servo when pressing a push button using Meadow. Everything you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro. We'll create a Meadow Application project and use Meadow.Foundation to easily write the logic.

Meadow.Foundation a platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.

If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LED project to properly set up your development environment.

Step 1 - Assemble the circuit

Connect your button and servo like the following Fritzing diagram:

ServoButton circuit diagram

ServoButton circuit diagram

ServoButton circuit diagram

Step 2 - Create a Meadow Application project

Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it ServoButton.

Step 3 - Add the required NuGet packages

Windows

Right-click on your ServoButton project and click Manage NuGet Packages. In the Browse tab, search for Meadow.Foundation.Servo and click Install to add it to your project.

macOS

Alt-click on your ServoButton project in the Solution Explorer, and click Add => Add Nuget Package to open the NuGet Package window. Search for Meadow.Foundation.Servo and click Install to add it to your project.

Step 4 - Write the code for ServoButton

Copy the following code below:

using System;using System.Threading;using Meadow;using Meadow.Devices;using Meadow.Foundation.Sensors.Buttons;using Meadow.Foundation.Servos;using Meadow.Hardware;namespace ServoButton{    public class MeadowApp : App<F7Micro, MeadowApp>    {        Servo servo;        PushButton button;        public MeadowApp()        {            var servoConfig = new ServoConfig(                minimumAngle: 0,                maximumAngle: 180,                minimumPulseDuration: 700,                maximumPulseDuration: 3000,                frequency: 50);            servo = new Servo(Device.CreatePwmPort(Device.Pins.D03), servoConfig);            servo.RotateTo(0);            button = new PushButton(Device, Device.Pins.D04);            button.Clicked += ButtonClicked;            // Keeps the app running            Thread.Sleep(Timeout.Infinite);        }        void ButtonClicked(object sender, EventArgs e)        {            servo.RotateTo(75);            Thread.Sleep(1000);            servo.RotateTo(0);        }    }}

In the MeadowApp's constructor the first thing the app does is initialize a Servo object, passing a ServoConfig for the SG90 model so it can rotate at its full range. Once Initialized, servo rotates to angle 0 using RotateTo(int angle); method. Next the app initializes a PushButton by passing an IIODevice which is Meadow in this case, and the IPin which the button is connected (D04). Now that our button is initialized, we can register the Clicked event handler, and will call ButtonClicked, which rotated the servo to 75 degrees for one second, and then goes back to its initial position.

Step 5 - Run the project

Click the Run button in Visual Studio, It should look like to the following GIF:

ServoButton project running

ServoButton project running

ServoButton project running

Check out Meadow.Foundation!

This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.

  • It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
  • The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
  • This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.

References

Schematics, diagrams and documents

ServoButton circuit diagram

Code

ServoButton complete project

You can check the ServoButton project in Meadow.Samples/Source/MeadowSamples/Projects/ServoButton

Credits

Leave your feedback...