Control Ned with an Arduino Board

Note

If you are using a Niryo One, please refer to this tutorial

There are endless programming activities you can do with Ned. Programming the robot from an Arduino board is one of those. In this tutorial we’ll go through a real life example to show you how to do that.

The concept

Using an Arduino board along with one (or many) Ned will allow you to create more complex applications. For example, you can easily setup a small assembly line demo with just one Arduino and 2 robots.

Let’s just start with one Arduino and one Ned.

The concept is simple: the Arduino board is the Master which decides which robot does what. It receives data from sensors or user inputs, and sends different commands to the robots via digital pins. To start a command on Ned, the Arduino will send a digital signal. It will have to wait for a response (another digital signal on another pin) to know that the robot has finished.

Each robot is a slave. It has predefined set of commands, and wait for the Arduino to send it a digital signal. Upon reception of the signal, a given command is started, and once finished, Ned will send another digital signal to the Arduino, to notify it has finish the command’s execution.

Electrical setup

Important

Before powering anything on, make sure that Ned and the Arduino share a common ground (black wire).

Then, you can plug up to 6 wires on the 6 digital pins at the back of Ned. On the Arduino, plug the wires on digital pins. You will then have to choose which pins are set as INPUTS and OUTPUTS (choose appropriate colors to avoid mistakes in the future).

In this example, we are using 4 wires:

  • 2 for sending commands from the Arduino to the robot (Arduino : OUTPUT, robot : INPUT).

  • 1 for sending back the command execution confirmation (Arduino : INPUT, robot : OUTPUT).

  • 1 to connect Arduino’s ground to Ned’s.

You will need to remember what you choose in the setup for writing the code of the application. In our case, we used an Arduino Mega 2560, and we plugged wires in the following way:

  • Digital Pin 54 (Arduino) –> Pin 1A (Ned)

  • Digital Pin 55 (Arduino) –> Pin 1B (Ned)

  • Digital Pin 56 (Arduino) <– Pin 1C (Ned)

  • GND (Arduino) – GND (Ned)

Note

When we write ” Digital Pin X (Arduino) –> Pin Y (Ned)”, it means that the Arduino Pin X is set as an output, and the Y Pin on Ned is set as an input.

You can also add 2 buttons powered with 5V, with 10kOhm pull-down resistors, so the user will be able to choose to execute the action “pick”, or “place”. For instance, you can plug each button to a digital pin on the Arduino like:

  • Digital Pin 57 (Arduino) <– Button 1

  • Digital Pin 58 (Arduino) <– Button 2

If you do so, you’ll have to change the Arduino code used in this demonstration. Refer to the Arduino Code explaination part to do so.

In our case, instead of using buttons, we used keyboard input from the computer connected in serial with the Arduino Board.

Programming Ned with Niryo Blocks (Niryo Studio) and the Arduino IDE

The Arduino code

Arduino IDE setup

First, you have to install the Arduino IDE, to programm your Arduino Board. To do so, follow this quick tutorial .

Once the Arduino IDE is installed on your computer, plug the Arduino Board to your computer, open the IDE and load the Arduino code used for this application, that you can download here. . Once downloaded, you can open it with Arduino IDE. Click “yes” to create a folder named as the .ino file, in order to store it.

You have to specify the type of Arduino Board you are using in Arduino IDE. In Tools >> Board, select the board you are working with. The processor field should be automatically filled.

Set type of board

On Windows:

Make sure you specified the right port for the Arduino Board. To do so, you can check on which port your board is plugged. To find out, you can disconnect your board and re-open the menu Tools >> Port, the entry that disappeared should be the Arduino Board. Reconnect the board and select that serial port.

On Linux:

The Arduino Board should be plugged to the serial port/dev/ttyACM0 (the 0 at the end of ACM might be a different number) If you get the error ” Error opening serial port … “, you need to set serial port permission. Open a terminal and type:

ls -l /dev/ttyACM*

You will get something like:

crw-rw---- 1 root dialout 166, 0 mars  17 10:21 /dev/ttyACM0

You need to add your user account to the group owner of the file, “dialout”:

sudo usermod -a -G dialout <username>

You need to replace <username> by your username account.

Note

to get your username account, just type in a terminal:

whoami

You should see your username.

Then, if you get an error like “Can’t open device … Permission denied”, you need to set read/write permissions with:

sudo chmod a+rw /dev/ttyACM0

Send and run the code

You can then click on the arrow button to transfer the code on the Arduino Board:

Send code on Arduino

The code should now be running on the Arduino Board. If you are facing issues, make sure that the port and the board are correctly specified.

Arduino code explaination

Note

commented code lines correspond to the usecase where we use buttons instead of keyboard input to trigger Blockly functions (pick and place). Uncomment them if you are in this usecase.

In the code, we first define the Board’s Digital Pins we’ll use. Here, as said before, we define 2 outputs and 1 output. Depending on your Arduino Board, you’ll have to check its pinout diagram to know Digital Pins number.

In the setup() function, we set pin mode for each pin, either output or input, and we define the communcation speed the Arduino will use (here, 9600 bits/sec, or bauds).

We then define two functions:

  • send_to_robot(int pin): set the specified pin to a HIGH state, wait for Ned’s answer on input pin, and put the specified pin back to LOW state.
  • send_to_robot_home_and_learning_mode: useful in the Blockly code to end the while loop. Set the two outputs to HIGH state.

Finally, in the loop() function, we:

  • Read a string from the serial port (meaning the keyboard input coming from the computer)

  • Depending on the string received, use a function described above.

In order to write on the serial port, open the IDE Serial Monitor, in Tools >> Serial Monitor:

Serial Monitor

Make sure the communication speed is set to 9600 bauds. Here, you’ll be able to see everything printed from the Arduino Board with the Serial.println() function. You are also able to send strings to your board, that are read with the Serial.readString() function.

Strings you can send to your Arduino Board to control Ned:

  • “pick” will set the first output to HIGH. In the Blockly code, it will trigger the pick steps.

  • “place” will set the second output to HIGH. In the Blockly code, it will trigger the place steps.

  • “finish” will set both outputs to HIGH. In the Blockly code, it will trigger the steps to end programm execution.

The Blockly Code

Blockly code explaination

You can download the XML used to create this sequence. All you need to do is to import this sequence in the Niryo Blocks tab of Niryo Studio, and run the sequence on Ned.

This Blockly code is composed of a main running program, and two functions used in the main program:

Blockly pick and place full program :align: right :width: 600px Blockly pick and place functions

In this example, you have to use a gripper. We define 2 functions, “Pick” and “Place”. The “Pick” function will pick an object with the gripper on the left of Ned, and the “Place” function will place the object on the right of Ned.

First, we define the pins 1A and 1B as INPUT, to receive a signal from the Arduino. The pin 1C is set as OUTPUT, to send a signal back upon command completion.

We then add a loop reading the continuously the 2 pins set as INPUT.

  • If one pin receives a HIGH signal while the other pin is LOW, the associated command will be started. At the end of the command, the pin set as OUTPUT on the robot will send a HIGH signal for 0.2 seconds (so the Arduino has the time to process it) and then set it back to LOW.
  • If both pins are HIGH, Ned goes back to Home position, activate learning mode, and the while loop is ended.

How to use - In a nutshell

Now that both Blockly and Arduino codes have been explained, here’s how to control Ned through the Serial Monitor of the Arduino IDE:

  • First, make sure that your Arduino code is running on the board, that you are connected to Ned with Niryo Studio, and that your board is plugged to your computer.

  • Load the provided Blockly code on Niryo Studio, and launch it. Ned will move to a starting position, waiting for orders.

  • In the Arduino IDE on your computer, open the Serial Monitor. Write and Send either:

    • pick

    • place

    • finish

It will trigger Blockly conditions and Ned will move accordingly.

Going further

What you’ve seen here is the foundation for programming Ned robots with Arduino Boards. You can easily scale the application with more robots, more Arduinos, more sensors, more buttons, etc…

The Niryo Block structure you’ve seen here will be pretty much the same: a loop that waits for a signal from the Arduino board, executes a command, and sends back a response.

Now, with your imagination (and a little bit of work), you could create some nice applications. Just a few ideas:

  • Tic Tac Toe: robot vs human

  • Assembly line that separates objects depending on their weight (along with a connected Arduino-balance)

  • Ned gives a chocolate to the user when he presses a button

If you build a nice application with Ned and Arduino (or any other electrical board), feel free to share it with the Niryo community on the social networks with #niryo. We’d be glad to feature your project!