• Download
  • Tutorials
  • Buy
  • Library
  • Docs
  • Forum
  • Log In

This tutorial shows how to use a Custom Arduino node to control a servo using Arduino's Servo library. The same technique can be used to use just about any Arduino library code in an Embrio project.

Download Project: Hobby Servo.zip

View Comments (0)

Servos are really fun to play with. In this tutorial I’ll show you how to control one with Embrio. I’ve got a typical hobby servo. It has 3 wires, voltage, ground, and signal. Using jumper wires I plug the voltage and ground wires into the appropriate channels on the bread board, and connect the signal wire to one of the pwm pins, in my case digital pin 3. To control the servo I have a potentiometer. The side pins are plugged into the voltage and ground channels, and the middle pin is plugged into the analog input pin A2.

I’ll start with a new empty project in Embrio. Embrio doesn’t currently have a built in node to control servos. What it does have is a type of node that lets you enter Arduino code, and there is a servo library for the Arduino that we can use. This video also acts as an introduction to the custom Arduino node.

Using the right click menu I add a custom Arduino node from the Input Output menu. If you’ve done any programming in Arduino you will recognize these code blocks. On this node is a place for includes, declarations, and setup code. To find out what code I need to add here, I go to the Arduino website and search for the Servo library documentation. I go to the examples section in the learning menu, then scroll down until I find the servo library. You can use any Aruino library code in the same way described in this example. Ardunio is very good about posting simple example code, which can easily be copy and pasted into Embrio.

First I copy the include line, and paste it into the include section in my node. Next I copy the line that declares a servo variable, the one that reads “Servo myservo”, and paste it into the declaration box on the node. Note that variables declared on custom Arduino nodes are global to the Embrio project, so make sure the names are unique. For example if you control multiple servos by copying and pasting this node, make sure to change the variable names. The Arduino example code has another variable called pos, which we don’t need. Next copy the code from the setup function into the setup block on the node, and change the pin setting from 9 to 3, or whatever PWM pin you have your servo plugged into. Now the servo object is set up, the last step is to add some update code.

In the Arduino loop is the line “myservo.write”, which passes a value between 0 and 179 to control the servo. On the Embrio node, we need an input activation value to control the servo. To add an input or output to a custom Arduino node, you can either right click on the node and select what to add, or use the little buttons on top of the node. I click on the Add Numeric Input button and leave the default name. Now I click on the New Code Block button to add some update code. There are a few options for when this code will be called, but I’ll leave it at the default of Every Update. I copy past the myservo.write function into the update code block. But I don’t have a variable called pos. I want to use the activation input. To reference an input or output in the Arduino code, you write the work Input or Output with a starting capital letter, than an under score, then the name of the input or output, without any spaces. So in this case I would write Input_Activation. This isn’t quite right though. The input activation value is in the range 0 to 1, but the servo write function expects a number between 0 and 179. To do this I use a build in Embrio function called transform, with a lower case t. This function works the same as the Arduino Map function, but it works with floating point numbers. So I type transform, open parentheses, Input _ Activation comma 0.0 comma 1.0, which is the range of the input activation, then another comma, 0, comma, 170, close parentheses.

My custom node is done, so I click on the connection icon to connect to the Arduino. If I typed everything in correctly, I can now control my servo by dragging on the activation input. To finish the project, I add a controller input node, select analog pin 2, connect it’s activation to the custom node, and refresh the connection program. Now I can control my servo by turning the potentiometer!

View Comments (0)