• Worldwide Shipping

  • 24/7 Customer Support

  • FAQ
  • Worldwide Shipping

  • 24/7 Customer Support

  • FAQ

*/ ?>

Project author: Domagoj Perković, [email protected]

INTRODUCTION

This mini-project shows Croduino Basic2 as a servo driver, and it is connected via USB to Raspberry Pi which sends information on how to control motors connected to Croduino. This way, Croduino power supply through Raspberry is also ensured, as well as communication between them. All digital outputs of the Croduino can be used for servo motor, except for pins 0 and 1, because they are used for serial communication. Therefore, it is interesting that digital output does not have to have the ability of PWM signal modulation. That way Croduino can control 12 servo motors. The only thing we should pay attention to is the fact that the servo motor of standard execution can pull around 250mA, so 12 of them would pull 3A which Croduino can not provide. In that case, you must provide a 5V external power supply of the servo motor.

PROGRAM CODES

Below, you will find the code for Croduino and Python script for Raspberry Pi. Croduino:
// 2018  e-radionica.com
// code by Domagoj Perkovic 

#include 

int servo_kut;
String tmp,servo_motor;
Servo myServo2,myServo3,myServo4,myServo5,myServo6,myServo7,myServo8,myServo9,myServo10,myServo11,myServo12,myServo13;

void setup() {
  myServo2.attach(2); // servo pin, does not have to be PWM
  myServo3.attach(3); // servo pin, does not have to be PWM
  myServo4.attach(4); // servo pin, does not have to be PWM
  myServo5.attach(5); // servo pin, does not have to be PWM
  myServo6.attach(6); // servo pin, does not have to be PWM
  myServo7.attach(7); // servo pin, does not have to be PWM
  myServo8.attach(8); // servo pin, does not have to be PWM
  myServo9.attach(9); // servo pin, does not have to be PWM
  myServo10.attach(10); // servo pin, does not have to be PWM
  myServo11.attach(11); // servo pin, does not have to be PWM
  myServo12.attach(12); // servo pin, does not have to be PWM
  myServo13.attach(13); // servo pin, does not have to be PWM
  Serial.begin(9600); // open a serial connection to your computer
}

void loop() {
  servo_motor="";
  if(Serial.available()>0)
  {
    tmp=Serial.readString();
    Parser(tmp);
    Serial.print("["); Serial.print(tmp); Serial.println("]");
    Serial.print("["); Serial.print(servo_motor); Serial.println("]");
    Serial.print("["); Serial.print(servo_kut); Serial.println("]");
  }
  if(servo_motor.equals("S2"))
  {
    myServo2.write(servo_kut);
    Serial.println("S2");
  }
  else if(servo_motor.equals("S3"))
  {
    myServo3.write(servo_kut);
    Serial.println("S3");
  }
  else if(servo_motor.equals("S4"))
  {
    myServo4.write(servo_kut);
    Serial.println("S4");
  }
  else if(servo_motor.equals("S5"))
  {
    myServo5.write(servo_kut);
    Serial.println("S5");
  }
  else if(servo_motor.equals("S6"))
  {
    myServo6.write(servo_kut);
    Serial.println("S6");
  }
  else if(servo_motor.equals("S7"))
  {
    myServo7.write(servo_kut);
    Serial.println("S7");
  }
  else if(servo_motor.equals("S8"))
  {
    myServo8.write(servo_kut);
    Serial.println("S8");
  }
  else if(servo_motor.equals("S9"))
  {
    myServo9.write(servo_kut);
    Serial.println("S9");
  }
  else if(servo_motor.equals("S10"))
  {
    myServo10.write(servo_kut);
    Serial.println("S10");
  }
  else if(servo_motor.equals("S11"))
  {
    myServo11.write(servo_kut);
    Serial.println("S11");
  }
  else if(servo_motor.equals("S12"))
  {
    myServo12.write(servo_kut);
    Serial.println("S12");
  }
  else if(servo_motor.equals("S13"))
  {
    myServo13.write(servo_kut);
    Serial.println("S13");
  }
    
  delay(100);
}//end loop

void Parser(String cmd_line)
{
  int x;
  String kut;
  cmd_line.trim(); cmd_line.toUpperCase();
  x=cmd_line.indexOf(" ");
  if(x<0) 
  {
    Serial.println("Kriva naredba...");
    return; //there are no spaces, it is an error 
  }
  else
  {
    servo_motor=cmd_line.substring(0,x);
    kut=cmd_line.substring(x); kut.trim();
    servo_kut=kut.toInt();
  }
}//end Parser
  

  
Python:
import serial
com_port=input("COM PORT:")
brzina=input("Baud rate [9600]:")
if(len(brzina)==0):
    brzina=9600
croduinoSerial = serial.Serial(com_port, brzina)

while True: #beskonacni loop
    servo=input("Unesi naredbu (Sx kut):")
    servo=servo.encode()
    croduinoSerial.write(servo)

EXAMPLE OF USE

In an example, we will show how to use servo motor connected to pin 2, which is not a PWM pin. Motor connection to the breadboard is seen in the picture. Servo motor is connected in the following way: red wire to Croduino +5V, black wire to Croduino GND and white wire (signal) to Croduino output 2. When Croduino is connected to Raspberry, we must know which communication channel is assigned to it. It is visible if you type the command  ls /dev/tty* in the console window which is seen in the following image. When Python program is launched on the Raspberry, at COM port we must enter the corresponding tty as shown in the following picture (do not forget the full name): Under Baud rate we enter the speed of serial communication which is defined by Croduino or, if it is 9600- which is the usual value, just press Enter. After that, the program requests entering a  command which consists of servo motor label and the absolute position of the head in degrees, with a space between these two parameters. Sx marks servo motor number and can be between S2 and S13, while the angle is between 0 and 180. S2 if servo motor is connected to digital output 2, S3 if it is connected to digital output 3, and so on.

CONCLUSION

This example is designed in order to enable servo motor startup via serial connection, which ensures application on different devices. In terms of programming, commands travel towards Croduino via serial connection in the ASCII format which makes communication applicable on different platforms. It is even possible to connect directly to Croduino's serial communication pins and that way issue commands - you do not have to use USB cable or USB-UART converter on the Croduino board. Raspberry Pi is used in this example, but you can also use PC, Mac, Microbit or an ordinary terminal. By modifying Python program, different applications of servo motor can be obtained, without changing Croduino program, i.e. we can practically perceive it as a "black box" which receives commands and executes them. This way, it is possible for users who do not have developing Croduino/Arduino environment, or who do not know how to use it, to use servo motors with other devices and other program languages. There is another reason to use Croduino as a servo driver: namely, Croduino Basic2 is almost two times cheaper than other modules for servo motor control of similar purpose and use.