I am working on servo motors control through serial communication protocol, I am sending data from Simulink to arduino and vice versa, The problem I am getting is that once I open Simulink Servo motors start turning while no command is sent or received from both, I tought that Simulink is initializing (reseting) arduino, so I decided to add a capacitor between Gnd & reset (I also tried with a resistor between 5v & reset), It actually works when I try to use Arduino IDE, Arduino is not reseting, but When i use it with Simulink it doesn't make change, Servo motors still turning randomly (I think that when I start or close simulation, Servo Are going to their initial position, I am not sure). I tried to add initial Position in Setup() function but in vain, It doesn't make sens! I tried using different arduino cards (MEGA, ESP32...Etc.) but in vain.

Does anyone have an idea about how to figure out this problem? Here is Simulink control code : & Arduino code :

#include <SCServo.h>

//Estructura Union
typedef union {
  float number;
  uint8_t bytes[4];
} valor;
valor velocidad;

unsigned long timeold;
int vel = 0;
float value;
void setup() {

  sc.pSerial = &Serial1;

void loop() {
  //Pregunta si tenemos un dato a recibir
  if (Serial.available() > 0) {
    value = recepcion();

  //Transforma el valor del voltaje (0-5) a velocidad
  vel = map(value, 0, 5, 0, 1023);
  //Activa el motor dirección Backward con la velocidad
  sc.WritePos(3,  value, 0);

  velocidad.number = sc.ReadPos(3);

  for (int i = 0; i < 4; i++) {

//Recibir Flotante
float recepcion() {
  int i;
  valor buf;
  for (i = 0; i < 4; i++)
    buf.bytes[i] = Serial.read();
  return buf.number;
//Función para la lectura del encoder

enter image description here

Thank you !

New contributor
HDne is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • first thing to try is to connect Simulink without sending any data at all
    – jsotola
    Oct 13 at 16:51
  • I tried to use Simulink to receive Servo motor angle from arduino, it works great, the servo is not turning randomly, I am getting the servo motor angle in real time. but when I start sending from simulink, the servo motor starts to turn to max & the min angle!
    – HDne
    Oct 13 at 17:32
  • 1
    sounds like a software issue
    – jsotola
    Oct 13 at 19:11
  • Without knowing what code you're running on your arduino or what you're actually trying to do (or what this "simulink" thing is you're talking about) we will be hard pressed to help you.
    – Majenko
    Oct 13 at 22:47
  • 1
    I added The simulink control code @Majenko
    – HDne
    Oct 14 at 10:36

Your Answer

HDne is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.