Servo Motor Twitching & Not Positioning Problem with HMI

Hi,

I made a project about servo positioning according to the some inputs via nano board. It was running and there was not any trouble.
Lastly, I added a Dwin HMI to my system via serial communication for entering and checking some parameters from screen. When I activated this HMI hardware communication (pin 2 & pin 3) at void setup (), servos are becoming abnormal situation. They are twitching and can not go to set position. When I delete the new serial expression from software, they becomes normal. What can be the reason? Is there any idea or suggestion?
Thanks in advance.

Please post code.
Please post schematics.

2 Likes

I dont have any schematic about this project. I connected them on breadboard. I made all single point connection. Voltage supllier (5V) and GND's are common for all parts. Arduino, servos, and HMI's GND are connected to each other by single point connection.

#include <SoftwareSerial.h>
#include <Wire.h>
#include <EEPROM.h>
#include <Servo.h>

const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial mySerial (rxPin, txPin);

unsigned char Buffer[9];
unsigned char   vitesdeger[8] = {0x5a, 0xa5, 0x05, 0x82, 0x50 , 0x00, 0x00, 0x00};

boolean debounceDone;
unsigned long btnPressTime = 0;
int delayPeriod = 500;

int vites = 0;
byte servobirikideger;
byte servoucdortdeger;
int hafiza;
byte servobirikihafiza;
byte servoucdorthafiza;
Servo servobiriki;
Servo servoucdort;
char vitesekran[4];

void setup(){
  pinMode(7,INPUT);
  pinMode(4,INPUT);
  pinMode(15,INPUT);
  
  delay(100);
  servobirikihafiza = EEPROM.read(5);
  servoucdorthafiza = EEPROM.read(6);
  servobiriki.write(servobirikihafiza);
  servoucdort.write(servoucdorthafiza);
  
  servobiriki.attach(5);
  servoucdort.attach(6);
  Serial.begin(9600);
  mySerial.begin(9600);
  delay(500);
  hafiza = EEPROM.read(0); 
  vites = hafiza;
  delay(500); 
   
}

void loop(){
 // ************** Vites Sayici Bölüm *******
if(millis() - btnPressTime > delayPeriod) debounceDone = true;
if(digitalRead(7) == HIGH &&  vites<=3 && debounceDone == true){
  btnPressTime = millis();
  debounceDone = false;
  vites++;
 
  Serial.println(vites);
  }

if(millis() - btnPressTime > delayPeriod) debounceDone = true;
if(digitalRead(4) == HIGH &&  vites>0 && debounceDone == true){
  btnPressTime = millis();
  debounceDone = false;
  vites--;
  
  Serial.println(vites);
}

if(millis() - btnPressTime > delayPeriod) debounceDone = true;
if(digitalRead(15) == HIGH &&  vites>0 && debounceDone == true){
  btnPressTime = millis();
  debounceDone = false;
  vites = 0;
 
  Serial.println(vites);
}
// ********************* Servo Motor Pozisyonlama & Hafızaya - Ekrana Yazdırma Bölümü ************
if( vites == 0){
  servobirikideger = 105 ;
  servobiriki.write(servobirikideger);

  servoucdortdeger = 110;
  servoucdort.write(servoucdortdeger);
  

  if (digitalRead(2) == HIGH || digitalRead(3) == HIGH || digitalRead(7) == HIGH) {
  EEPROM.write(0,vites);
  EEPROM.write(5,servobirikideger);
  EEPROM.write(6,servoucdortdeger);
}
}


if(vites == 1) { 
  servoucdortdeger = 110;
  servoucdort.write(servoucdortdeger);
  servobirikideger = 134 ;
  servobiriki.write(servobirikideger);

    if (digitalRead(2) == HIGH || digitalRead(3) == HIGH || digitalRead(7) == HIGH) {
  EEPROM.write(0,vites);
  EEPROM.write(5,servobirikideger);
  EEPROM.write(6,servoucdortdeger);
  
}
}

if (vites == 2) {
  servoucdortdeger = 110;
  servoucdort.write(servoucdortdeger);
  delay(250);
  servobirikideger = 65 ;
  servobiriki.write(servobirikideger);

    if (digitalRead(2) == HIGH || digitalRead(3) == HIGH || digitalRead(7) == HIGH) {
  EEPROM.write(0,vites);
  EEPROM.write(5,servobirikideger);
  EEPROM.write(6,servoucdortdeger);
  
}
}

if (vites == 3) {
  servobirikideger = 105 ;
  servobiriki.write(servobirikideger);
  delay(400);
  servoucdortdeger = 69;
  servoucdort.write(servoucdortdeger);

  if (digitalRead(2) == HIGH || digitalRead(3) == HIGH || digitalRead(7) == HIGH) {
  EEPROM.write(0,vites);
  EEPROM.write(5,servobirikideger);
  EEPROM.write(6,servoucdortdeger);
}
}

if (vites == 4) {
  servobirikideger = 105 ;
  servobiriki.write(servobirikideger);
  servoucdortdeger = 137;
  servoucdort.write(servoucdortdeger);

  if (digitalRead(2) == HIGH || digitalRead(3) == HIGH || digitalRead(7) == HIGH) {
  EEPROM.write(0,vites);
  EEPROM.write(5,servobirikideger);
  EEPROM.write(6,servoucdortdeger);
}
}
 Data_Arduino_to_Display();
 delay(10);

}

void Data_Arduino_to_Display()
{

 vitesdeger[6] = highByte(vites);
 vitesdeger[7] = lowByte(vites);
 mySerial.write(vitesdeger, 8);

}

The power supply is inadequate. Use a separate power supply and budget 1 Ampere per servo for small ones like SG-90 and 2.5 Amperes/servo for MG996R. Never use the Arduino 5V output, as that can damage the board.

1 Like

Yes, i am using seperate power supply for this system. I am using 10 A 12v- 5v voltage regulator and feed all parts from this regulator seperately. Gnds are common.

For help on this forum, please provide complete and correct information about your project.

Post a wiring diagram, with all parts identified and pins and connections clearly labeled. Hand drawn diagrams are preferred.

I connected them on breadboard.

Breadboards cannot handle motor and servo currents, as the tracks burn. They are intended for temporary experiments with low power logic circuitry.

Basically, my schematic is this. Breadboard current capacity is 1 A almostly. SG90's current capacity is 220 mA. So it should be enough for this system. I am already powering HMI from power supply unit directly.

But suspicious point is this: when I cancelled hardware serial, system runs normally. Does the new serial communication cause over power consumption? Or is the problem related with software because of new serial communication?

I will try to cancel new defined software serial and try to send/receive data from Dwin by existing soft serial with Rx0 Tx1 pins.

You have a short circuit

I will try to cancel hardware serial and try to send/receive data from Dwin by soft serial.

According to your code you are using software serial and that is where the problem may be. Try using hardware serial.

It is drawing mistake. Fixed it. It was wired correctly on my board.

Have you tried hardware serial?

Yes it was also additional software serial. I wrote it wrong also.
Now, I cancelled new defined software serial and tried it with existing softserial via Rx0 and Tx1. It is running normally.
When I add new softserial to my system as Rx2 and Tx2, somehow it makes noise or something like that. I could not understand the reason.

Rx0 and Tx1 are the hardware serial pins, you do not need to use software serial on those pins.

Hım ok. I was thinking as they are soft.
Actually, i did not make any definition about that pins as software serial. I changed mySerial.write//read parts as Serial.write/read. And i replaced dwin RX TX cable from pin2&3 to pin0&1 and it works.

You don't need to. Hardware serial is fixed at pins 0 and 1. You just use Serial.print to send data to the display

1 Like

So why does it not run at pin2&pin3? Is there any guess?

People have reported that software serial interferes with servo because it blocks the servo software from updating.

1 Like

How can we solve this problem Jim? Do you know any method?

This might help, though I haven't tried it myself.

1 Like

Just use hardware serial.
Is there a problem with using hardware serial?

Jim is correct, the simplest solution would be to not use SerialMonitor, and use the hardware serial for your terminal. Does make debugging an order of magnitude harder, though.

Other alternative would be to switch to some other Arduino, like a Mega, or Every, that have multiple hardware serial.

1 Like