# Coding your first servo motor on MakeCode

<figure><img src="https://images.unsplash.com/photo-1624719507903-7d8b41c7c9cb?crop=entropy&#x26;cs=tinysrgb&#x26;fm=jpg&#x26;ixid=MnwxOTcwMjR8MHwxfHNlYXJjaHw3fHxhY3Rpdml0eXxlbnwwfHx8fDE2NjE1MzIwOTI&#x26;ixlib=rb-1.2.1&#x26;q=80" alt=""><figcaption><p>Photo by <a href="https://unsplash.com/@kilimanjarostudioz?utm_source=unsplash&#x26;utm_medium=referral&#x26;utm_content=creditCopyText">Kilimanjaro STUDIOz</a> on <a href="https://unsplash.com/s/photos/activity?utm_source=unsplash&#x26;utm_medium=referral&#x26;utm_content=creditCopyText">Unsplash</a></p></figcaption></figure>

{% hint style="info" %}
There is no need to have a servo motor for this activity.  We will use the **emulator** and the **servo** **extension** in MakeCode to visualize the result of our code.
{% endhint %}

Open [**MakeCode**](https://makecode.microbit.org/) and follow along or read the goals of this activity and try to do it as a challenge.

#### Goals

* Have the micro:bit position the servo motor to 90 degrees at the start of the application.
* When the user presses the A button, change the angle to 0-degree.
* When the user presses the B button, change the angle to 180-degree.
* Reset the angle to 0-degree, when the user presses A and B at the same time.

### Our solution

<details>

<summary>Add the Servo Extension to your MakeCode project</summary>

* To start, we need to enable the Servo extension in MakeCode.  To do so, click on Extensions.

&#x20;                                ![](https://2550165886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fyb2qmcYgqwLw68em5mST%2Fuploads%2F671ggbuL9EctrsipLcJA%2Fsevo_extension.png?alt=media\&token=f54c1557-ec7d-4783-bdf9-87c1bbf845a7)

* Add the servo extension from the list

&#x20;                                   ![](https://2550165886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fyb2qmcYgqwLw68em5mST%2Fuploads%2FxTsOWZnntoGg2wVx3wyY%2Fservo_add.png?alt=media\&token=a34b6365-44a6-4add-8788-9ee66b5923e0)

Now you have access to a new group of blocks and the emulator includes a virtual servo motor.  They provide different ways of moving the servo.

&#x20;                                 ![](https://2550165886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fyb2qmcYgqwLw68em5mST%2Fuploads%2Fg3fX8FZRvEUA2jGx72t5%2Fservo_blocks_emulator.png?alt=media\&token=976e2097-4652-4d49-89dc-50ed6e2fa33c)

</details>

<details>

<summary>Initialize servo motor at a 90-degree angle</summary>

The following code will initialize the servo motor at a 90-degree angle as soon as the micro:bit gets electricity (when it starts).

&#x20;                                   ![](https://2550165886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fyb2qmcYgqwLw68em5mST%2Fuploads%2FZVKiyGpuNvOngEoUZQf4%2FScreen%20Shot%202022-08-22%20at%204.37.02%20PM.png?alt=media\&token=065dc031-8e78-4a6a-927c-6a428a54f528)

Test on the emulator and see how the servo moves its pointer to the expected position.

</details>

<details>

<summary>Press Button A, B and A+B to change positions</summary>

All these three actions follow the same pattern.  We introduce a block that will trigger action and depending on the button pressed, we change to the desired angle

#### Button A to 0 degrees

&#x20;                                  ![](https://2550165886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fyb2qmcYgqwLw68em5mST%2Fuploads%2FTWiRsASRWvV0CgxUHMtV%2FservoAtozero.png?alt=media\&token=9bcaaa9e-1722-4762-8455-bfbc8447efaf)

#### Button B to 180 degrees    &#x20;

&#x20;                                  ![](https://2550165886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fyb2qmcYgqwLw68em5mST%2Fuploads%2FhWrRLnsrRFKWED3eFCnT%2FservoBto180.png?alt=media\&token=5d3092de-82fd-46bf-80a3-e21f2de98ebe)     &#x20;

**Buttons A+B to the initial position (90 degrees)**

&#x20;                                          ![](https://2550165886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fyb2qmcYgqwLw68em5mST%2Fuploads%2F9QTwWnMCHdlSfnQUg8PQ%2FservoABto90.png?alt=media\&token=d84ba541-b0aa-46c5-b16d-cbcf263fb004)                   &#x20;

Test on the emulator and see how the servo moves its pointer to the expected position when you press different buttons.

</details>

The next tool we are learning about is called a **Neopixel**.  See you in the next section.&#x20;
