Problem with servomotor sg90

I have a problem when I attach the servo motor in this code, it cracks the code, I tried to put other codes for the servo motor but same thing. Can you please help me to optimize the code for servomotor?
I have a smartphone app made in the myth app inventor, when I start the servo motor it works, but when I want to stop it I get a message (Broken Pipe)

#include  <ServoTimer2.h> //libraria Servomotorului
#include  <SoftwareSerial.h>
#include  <DHT.h>
#include  <String.h>


#define DHT_PIN 6       // Pinul la care este conectat senzorul DHT11
#define DHTTYPE DHT11   // Specifică tipul de senzor DHT
int RELEU_PIN = 3;     // Pin citire date digitale
int SERVO_PIN = 9;
int LED_PIN = 2;
const byte rxPin = 10;
const byte txPin = 11;
int ANALOG_UMIDITATE = A4;
int ANALOG_PICATURI = A5;
int ProcentajPicaturi;
int ProcentajUmiditate;
String umiditate_picaturi;
int c ;
ServoTimer2 servomotor; // Declarați o instanță a obiectului Servo

SoftwareSerial bluetoothSerial(rxPin,txPin);
DHT dht(DHT_PIN, DHTTYPE);  // Inițializează obiectul pentru senzorul DHT11

void setup() {

  bluetoothSerial.begin(9600); //Initializare comunicare seriala cu modulul Bluetooth
  Serial.begin(9600);
  pinMode(RELEU_PIN, OUTPUT); // Atribuie pinului 3 valoarea output
  digitalWrite(RELEU_PIN,LOW);
  pinMode(LED_PIN,OUTPUT); 
  pinMode(SERVO_PIN,OUTPUT);
  servomotor.attach(SERVO_PIN); 
  dht.begin();         // Inițializează senzorul DHT11
}
 

void loop() { 

  ProcentajUmiditate = analogRead (ANALOG_UMIDITATE);
  int UmiditateSol = map(ProcentajUmiditate, 0, 1023, 100, 0);

  ProcentajPicaturi = analogRead (ANALOG_PICATURI);
  int PicaturiPloaie = map(ProcentajPicaturi, 0, 1023, 100, 0);

  float umiditate_aer = dht.readHumidity();       // Citeste umiditatea 
  float temperatura_aer = dht.readTemperature();  // Citeste temperatura 

  delay(1000);
  umiditate_picaturi = String(UmiditateSol) + ","+ String(PicaturiPloaie) + "," + String(umiditate_aer) + "," + String(temperatura_aer);
  bluetoothSerial.println(umiditate_picaturi);



    if (bluetoothSerial.available()) 
  {
      c = bluetoothSerial.read();
    if (c == '1')
    {
      digitalWrite(RELEU_PIN, HIGH);
      digitalWrite(LED_PIN,HIGH);
   


    }
    if (c == '0')
    {
      digitalWrite (RELEU_PIN, LOW);
      digitalWrite(LED_PIN,LOW);
      //servomotor.write(750);
      
      
    }

    
    
    } 
    if(c == '1'){
    servomotor.write(750);
     delay(1000);
    servomotor.write(1500);
    delay(1000);
  }
    
    
  delay(2000); //Pauza între citiri
}



Thank you for your time!

Where does the servo get its power from ?

1 Like

I try with power from PC, and i have a external power with regable voltage, same problem.

Disconnect the servo signal wire and run the sketch. Does the "broken pipe" occur?

1 Like

I connected the arduino uno board to 9V, the whole system works fine, I disconnected the servo motor from the control pin.

With the servomotor connected to the command pin, after 1-2 minutes of opening the system, it does not respond to commands, but the rest of the system works fine, the servomotor after 1-2 minutes has a small error moves one degree to the left and one to the right without receiving the command.

This might be a memory-issue because each time you assign a new value to a variable of type String it eats another piece of free RAM until the RAM gets corrupted

Use the SafeString-library instead
https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html

#include <ServoTimer2.h> //libraria Servomotorului
#include <SoftwareSerial.h>
#include <DHT.h>
#include <String.h>
#include <SafeString.h>

createSafeString(umiditate_picaturi_SS,32);

#define DHT_PIN 6       // Pinul la care este conectat senzorul DHT11
#define DHTTYPE DHT11   // Specifică tipul de senzor DHT
int RELEU_PIN = 3;     // Pin citire date digitale
int SERVO_PIN = 9;
int LED_PIN = 2;
const byte rxPin = 10;
const byte txPin = 11;
int ANALOG_UMIDITATE = A4;
int ANALOG_PICATURI = A5;
int ProcentajPicaturi;
int ProcentajUmiditate;
//String umiditate_picaturi;

int c ;
ServoTimer2 servomotor; // Declarați o instanță a obiectului Servo

SoftwareSerial bluetoothSerial(rxPin, txPin);
DHT dht(DHT_PIN, DHTTYPE);  // Inițializează obiectul pentru senzorul DHT11

void setup() {
  bluetoothSerial.begin(9600); //Initializare comunicare seriala cu modulul Bluetooth
  Serial.begin(9600);
  pinMode(RELEU_PIN, OUTPUT); // Atribuie pinului 3 valoarea output
  digitalWrite(RELEU_PIN, LOW);
  pinMode(LED_PIN, OUTPUT);
  pinMode(SERVO_PIN, OUTPUT);
  servomotor.attach(SERVO_PIN);
  dht.begin();         // Inițializează senzorul DHT11
}


void loop() {

  ProcentajUmiditate = analogRead (ANALOG_UMIDITATE);
  int UmiditateSol = map(ProcentajUmiditate, 0, 1023, 100, 0);

  ProcentajPicaturi = analogRead (ANALOG_PICATURI);
  int PicaturiPloaie = map(ProcentajPicaturi, 0, 1023, 100, 0);

  float umiditate_aer = dht.readHumidity();       // Citeste umiditatea
  float temperatura_aer = dht.readTemperature();  // Citeste temperatura

  delay(1000);
  //umiditate_picaturi = String(UmiditateSol) + "," + String(PicaturiPloaie) + "," + String(umiditate_aer) + "," + String(temperatura_aer);
  umiditate_picaturi_SS = UmiditateSol;     // 4 byte
  umiditate_picaturi_SS += ",";             // 1 byte
  umiditate_picaturi_SS += PicaturiPloaie;  // 4 byte
  umiditate_picaturi_SS += ",";             // 1 byte
  umiditate_picaturi_SS += umiditate_aer;   // 123.56 // 6 byte
  umiditate_picaturi_SS += ",";             // 1 byte
  umiditate_picaturi_SS += temperatura_aer; // 123.56 // 6 byte 
  Serial.println(umiditate_picaturi_SS);  
  bluetoothSerial.println(umiditate_picaturi_SS);

  if (bluetoothSerial.available()) {
    c = bluetoothSerial.read();
    if (c == '1') {
      digitalWrite(RELEU_PIN, HIGH);
      digitalWrite(LED_PIN, HIGH);
    }
    
    if (c == '0') {
      digitalWrite (RELEU_PIN, LOW);
      digitalWrite(LED_PIN, LOW);
      //servomotor.write(750);
    }
  }
  
  if (c == '1') {
    servomotor.write(750);
    delay(1000);
    servomotor.write(1500);
    delay(1000);
  }

  delay(2000); //Pauza între citiri
}

best regards Stefan

1 Like

Many thanks. It seems that now the system works very well and has no more problems. Thank you very much for your time. :pray: :pray:

So did you test it for at least 30 minutes better for hours if
after hours of operation the code still works as expected?

At the moment I've tested it for 15 minutes and it works fine, I'll come back with an update if anything happens. Can you please remove the code from this platform. Because I am using it on a project that is going through the plagiarism program. You can leave only the part you added to the code. I'm afraid it will detect the whole thing as copied.

You asked on a public forum for help.
You should have modified your code that much before posting publicly that it will not be detected by a plagiarism test.

Well you have been too lazy for learning it all yourself. So you aked in the arduino-forum.
Everything is OK with asking.

If you want to make sure that your own code will pass the plagiarism-test you will have to modify all variable names and some of the structure after you have the solution that it will pass the test.

I recommend of you do modify your variable-names and function names that you take time to think about names that really describe spot-on what their purpose is and what your functions do.

This will make a positive impression to the people that judge about the project

Your code

  ProcentajUmiditate = analogRead (ANALOG_UMIDITATE);
  int UmiditateSol = map(ProcentajUmiditate, 0, 1023, 100, 0);

uses the names vice versa to what it is

analogRead (ANALOG_UMIDITATE);

is a raw anlog-to digital converted value ranging from 0 up tp 1023
and

 map(ProcentajUmiditate, 0, 1023, 100, 0);

calculates a percentage-value ranging from 0 to 100
I might be wrong but
So far my impression is that you are trying to minimise your own effort
to get away "as cheap as possible"

best regards Stefan

I know what you're saying. I'll say I asked for help on some of the code, but in this case it's going to come out all plagiarized code. I sent it in this morning, I'll fill in the modified part, but they already have the first part of the code.

on saturday afternoon?
I'm very sure that you have time until next monday or at least sunday evening
So send them a new improved version of it all.

1 Like

I will, I hope there won't be any problems. Thanks again for your help

How about improving your code by adding functions where each function does one thing?

one function for switch on

      digitalWrite(RELEU_PIN, HIGH);
      digitalWrite(LED_PIN, HIGH);

one function for switching off

      digitalWrite (RELEU_PIN, LOW);
      digitalWrite(LED_PIN, LOW);

one function for servo-moving

    servomotor.write(750);
    delay(1000);
    servomotor.write(1500);
    delay(1000);

you can learn about it here

1 Like

I did this, but the servomotor if I add it above does not rotate in a continuous loop, only once 45 degrees left and 45 degrees right. Idk why..

this is what this code is doing.

if (c == '1') {
    servomotor.write(750);
    delay(1000);
    servomotor.write(1500);
    delay(1000);
  }

[/quote]

well using the support of a public forum would require to post your attempt how you tried it.
If your teacher / professor would find a thread where she/he can follow your learning process how to use functions I'm sure that this would make a good impression.

The way you are asking / posting has one possible plausible explanation:

You knowledge-level is rather low so that you do not yet understand what these lines of code do

you should post your own attempt how you tried to code it combined with a a specific question
so a detailed feedback can be given that you can learn from

best regards Stefan

In the servotimer2 library I used the 750 takes the value of 0 degrees and the 1500 of 90 degrees..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.