Control the Conveyor Belt with an Arduino Board

Control conveyor belt with an arduino board

Note

This tutorial is working from:
The version v3.0.0 of the ned_ros_stack
The version v3.0.0 of Niryo Studio

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

Objectives

The Conveyor Belt has a stepper motor that is controlled with the custom Niryo Arduino compatible board. In this tutorial, you will learn how to control the Conveyor Belt with an Arduino board.

This board has:

  • A communication interface: CAN bus (using MCP2515 to make a SPI-CAN interface), this interface is used to control the Conveyor Belt with Ned.

  • A digital input interface: this one is in the controller module.

  • An analog input.

In this tutorial, you will have to:

  • Update the Arduino code running on the NiryoStepper (the motor in the Conveyor Belt).

  • Use the Conveyor Belt Control Box, and connect it to another Arduino connected to your computer (we use the Arduino MEGA 2560).

  • Run a code on the Arduino connected to your computer that will manage an output pin’s value in order to trigger the Conveyor Belt.

You will be able to start and stop the Conveyor Belt using your Arduino Board.

What you will need

  • An Arduino Board (we used the Arduino MEGA 2560, but you can also use an Arduino Uno for instance).

  • A Niryo Conveyor Belt.

  • The Conveyor Belt Control Box and its power supply.

Update the Conveyor Belt firmware

Note

If you want to control the Conveyor Belt with another Arduino, you need to change the actual firmware.

First, you will have to update the Conveyor Belt firmware currently implemented in the Conveyor Belt’s motor. In order:

Step 1: Prepare your Arduino IDE

The micro-controller (SAMD type) used in NiryoStepper is not already included in the supported boards, so you will need to add the extension.

  • If you haven’t installed the Arduino IDE, download it and install it from here.

  • Open the boards manager:

Board manager Arduino
  • Now, search for ‘zero’:

Arduino 'zero' package
  • Click on the package and install it. This may take a few minutes.

  • Once done, restart your Arduino IDE.

Step 2: Get the Conveyor Belt firmware and change code

  • Download the firmware from our GitHub Repository.

  • Extract the archive. If the name of the folder is not “NiryoConveyor”, rename it with this name.

  • Open the folder and double click on NiryoConveyor.ino. The program will open in your Arduino IDE.

  • Add this function at the end of NiryoConveyor.ino:

    void control_conveyor_arduino(int conveyor_speed , int conveyor_direction)
    {
        if ((conveyor_speed ==0) or (digitalRead(digital_input_pin) == LOW)){
            relaxed_mode_with_resistance();
            fan_LOW();
        }
        else if (digitalRead(digital_input_pin) == HIGH){
            fan_HIGH();
            delay_steps = 10 * (100 - conveyor_speed ) + 200 ;
            output(-1800 * conveyor_direction * steps_position / 3, uMAX);
            steps_position = steps_position + 1;
            delayMicroseconds(delay_steps);
        }
    }
    
  • In the Loop function, change:

    void loop() {
    
        /*
            if the Conveyor Belt is not activated on the side of Ned --> drive it with the control box
        */
        if (!canBus.getConveyorStatus()) {
            autonomeConveyorControl();
        }
    
        // [...]
    

    to:

    void loop() {
    
        /*
            if the Conveyor Belt is not activated on the side of Ned --> drive it with the control box
        */
        if (!canBus.getConveyorStatus()) {
            //autonomeConveyorControl();
            control_conveyor_arduino(50 , 1);
    
        }
    
        // [...]
    

Note

The Conveyor Belt’s speed goes from 0 to 100 % and the Conveyor Belt’s direction can be 1 (forward) or -1 (backward).

When the digital_input_pin (used in other cases as the IR digital input), controlled by the Arduino MEGA 2560 as explained later, is LOW, the Conveyor Belt stops. When it is HIGH, the Conveyor Belt’s movement is triggered, depending on the chosen conveyor_speed and conveyor_direction.

Step 3: Upload changes on NiryoStepper

Now you will have to update the code on the Conveyor Belt. Connect the NiryoStepper of the Conveyor Belt to your computer with a microUSB to USB cable.

Make sure that you choose the right Board Arduino Zero (Native USB Port) and the right port in the Arduino IDE.

Now click on Upload (top button with an arrow) and wait a few seconds. You should see a message like this at the bottom of the IDE:

Upload code on NiryoStepper

Note

if the Arduino IDE fails to upload the program when connected to a USB 3.0 port, try using a USB 2.0 port instead.

Connect and manage your Arduino Board

Now, connect your computer to an Arduino Board. Here, we use the Arduino MEGA 2560.

Step 4: Connect your Arduino Board to the Conveyor Belt’s Control Box

Connect your Arduino Board to the digital interface of the Control Box of the Conveyor Belt:

Connect to conveyor

As you can see, the pin 2 of the Arduino Board is linked to the pin INPUT of the Control Box Digital input interface, which is itself used by the NiryoStepper as the digital input 0.

Warning

You should not plug the power supply now. Also, you should never plug the power supply before plugging the other wires.

Step 5: Create and upload a code on your Arduino Board

Upload this code on your Arduino Board:

const int conveyorPin = 2;

String incomingByte;
int conveyorState;

void setup() {

    // set the digital pin as output:
    pinMode(conveyorPin, OUTPUT);
    Serial.begin(9600);
}

void loop() {
    Serial.flush();
    incomingByte = Serial.readString();
    Serial.println(incomingByte);
    if (incomingByte.length() != 0){
        if (incomingByte == "go") {
            conveyorState = HIGH;
        }
        else if (incomingByte == "stop"){
            conveyorState = LOW;
        }
    }
    // send the state
    digitalWrite(conveyorPin, conveyorState);
}

Note

Make sure that the board and the port are correctly set up in you Arduino IDE when you upload the code.

This code will change the state of the pin 2 to HIGH or LOW, according to the string received through the serial port (meaning the keyboard input coming from the computer).

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 will 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.

If you send go, the PIN 2 will be set to HIGH, which will trigger the Conveyor Belt’s movement.

If you send stop, the PIN 2 will be set to LOW, which will stop the Conveyor Belt’s movement.

Control the Conveyor Belt with the Arduino Board

Now that you updated the code on your NiryoStepper and on your Arduino Board connected to your computer, you can plug the power supply on the Conveyor Belt’s Control Box, open the Serial Monitor of your Arduino Board from your computer and send “go” or “stop” to control the Conveyor Belt.