servo and load sensor not working together?

Hi all,

I am creating a box that opens when the load sensor feels something is on a scale. The box opens up with a servo motor.

The load sensor is working perfectly, using a hx711 signal amplifier.
The servo is working perfectly.

When I put them together, the load sensor stops working and i get 1 repetitive (wrong) weight.

I tried to debug and found that the line that makes this happen is:

myservo.attach(9); // attaches the servo on pin 9 to the servo object

Anyone?
I tried changing inputs and outputs, no luck.

Here is the complete code:

#include "HX711.h"
#include <Servo.h>

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

Servo myservo;  // create servo object to control a servo

HX711 scale;

int led = 13; // the pin the LED is connected to
const int openTime = 10;   // seconds to open box
unsigned long currentMillis; 
unsigned long startMillis; 
unsigned long elapsedMillis; 


void setup() {
  Serial.begin(38400);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(70);


  startMillis = millis();
  
  pinMode(led, OUTPUT); // Declare the LED as an output
  digitalWrite(led, LOW); // Make sure LED is off
    
  Serial.println("HX711 Demo");
  Serial.println("Initializing the scale");

  // Initialize library with data output pin, clock input pin and gain factor.
  // Channel selection is made by passing the appropriate gain:
  // - With a gain factor of 64 or 128, channel A is selected
  // - With a gain factor of 32, channel B is selected
  // By omitting the gain factor parameter, the library
  // default "128" (Channel A) is used here.
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  Serial.println("Before setting up the scale:");
  Serial.print("read: \t\t");
  Serial.println(scale.read());			// print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));  	// print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));		// print the average of 5 readings from the ADC minus the tare weight (not set yet)

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);	// print the average of 5 readings from the ADC minus tare weight (not set) divided
						// by the SCALE parameter (not set yet)

  scale.set_scale(2280.f);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();				        // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));		// print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
						// by the SCALE parameter set with set_scale

  Serial.println("Readings:");

}

void loop() {
  Serial.print("one reading:\t");
  Serial.print(scale.get_units()*2.53, 1);
  Serial.print("\t| average:\t");
  Serial.println(scale.get_units(10), 1);

  if (scale.get_units()*2.53 > 100)
      {
            currentMillis = millis()/1000;
            digitalWrite(led, HIGH); // Turn the LED on  
            delay(openTime*1000);  
  myservo.write(170);  
  while(scale.get_units()*2.53 > 100); {
  digitalWrite(led, LOW); // Turn the LED off          
  delay(1000);// Wait for 1000 milliseconds (1 second) // 1 h = 3600000 ms
  digitalWrite(led, HIGH); // Turn the LED on  
  delay(1000);// Wait for 1000 milliseconds (1 second) // 1 h = 3600000 ms
    }
      }
  scale.power_down();			        // put the ADC in sleep mode
  delay(1000);
  scale.power_up();
}

The usual cause of servo problems is lack of power. The Arduino generally can't provide enough current so you need a separate power supply for the servo. Connect the grounds.

Thanks.

Tried that - didn’t work.
What I did notice is that when the servo was disconnected from a power source, the load sensor worked fine with all the lines of code.

You may have poor layout. The HX711 needs separate power and ground wiring, not shared with
the servo. Often people use a single ground wire and get crosstalk from it.

I think you're absolutely right.

As a noob, can you explain how I can set up an alternative power source? Or point me to a tutorial? I'm not sure what to look for.

Yaron

You may still have problems. Servo.h and SoftwareSerial.h are known not to work well together. There are alternatives for both of them that might help e.g. ServoTimer2.h and/or AltSoftSerial.h or NeoSWSerial.h

Steve

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