ESP 32-(NOW)- Flexsensor and servo motor

For a project we are trying to use an ESP32 with flex sensors, to control the servo motors attached to another ESP32 (connected via ESP NOW). The transmission sequence works and the data gets received, however the servo motors do nothing. We used the codes and library below, any hints on why it isn't working would be much appreciated.

TRANSMITTER CODE:
#include <esp_now.h>
#include <WiFi.h>

//Naampie van de flex sensor input pins AANPASSEN WEGENS VERANDERINGEN
int flexsensor_5 = 35; //Ring D35
int flexsensor_4 = 34; // Pink D34
int flexsensor_3 = 33; // WIjs D33
int flexsensor_2 = 32; //Middelfinger D32
int flexsensor_1 = 25;//Duim D25

//Naampie van de variabelen voor de flex sensor waardes
int flexvariable_5;
int flexvariable_4;
int flexvariable_3;
int flexvariable_2;
int flexvariable_1;

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x3C, 0x71, 0xBF, 0x52, 0xBF, 0x60};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int a;
int b;
int c;
int d;
int e;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup() {
// Init Serial Monitor
Serial.begin(115200);

// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);

// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}

// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);

// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;

// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}

void loop() {{
flexvariable_5 = analogRead(flexsensor_5);
flexvariable_5 = map(flexvariable_5, 630, 730, 80, 20);

flexvariable_4 = analogRead(flexsensor_4);
flexvariable_4 = map(flexvariable_4, 520, 710, 70, 175);

flexvariable_3 = analogRead(flexsensor_3);
flexvariable_3 = map(flexvariable_3, 510, 680, 140, 10);

flexvariable_2 = analogRead(flexsensor_2);
flexvariable_2 = map(flexvariable_2, 580, 715, 90, 175);

flexvariable_1 = analogRead(flexsensor_1);
flexvariable_1 = map(flexvariable_1, 550, 700, 90, 175);
}

// Set values to send
myData.a = flexvariable_1;
myData.b = flexvariable_2;
myData.c = flexvariable_3;
myData.d = flexvariable_4;
myData.e = flexvariable_5;

// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(100);
}

RECEIVER CODE:

#include <ESP32_Servo.h>

//Receiver Code (Hand) - Mert Arduino and Tech
#include <esp_now.h>
#include <WiFi.h>

//Servo naampies
Servo Servomotor1;
Servo Servomotor3;
Servo Servomotor4;
Servo Servomotor2;
Servo Servomotor5;

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int a;
int b;
int c;
int d;
int e;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("int: ");
Serial.println(myData.a);
Serial.print("int: ");
Serial.println(myData.b);
Serial.print("int: ");
Serial.println(myData.c);
Serial.print("int: ");
Serial.println(myData.d);
Serial.print("int: ");
Serial.println(myData.e);
Serial.println();
}

int flexvariable_1 = myData.a;
int flexvariable_2 = myData.b;
int flexvariable_3 = myData.c;
int flexvariable_4 = myData.d;
int flexvariable_5 = myData.e;

void setup() {

//Naampies servo motor input pins INPUT PINS AANPASSEN VANWEGE VERANDERINGEN
Servomotor1.attach(18); //A1 D18
Servomotor2.attach(4); //A2 D4
Servomotor3.attach(5); //A3 D5
Servomotor4.attach(21); //A4 D21
Servomotor5.attach(19); //A5 D19

// Initialize Serial Monitor
Serial.begin(115200);

// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);

// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}

// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void loop(){

Servomotor1.write(-(flexvariable_1)); //A1 KIJKEN WELKE PINS BIJ DEZE NUMMERS HOREN
Servomotor2.write(-(flexvariable_2)-150); //A2
Servomotor3.write(-(flexvariable_3)-514); //A3
Servomotor4.write(-(flexvariable_4)-350); //A4
Servomotor5.write(-(flexvariable_5)-340); //A5
}

ServoESP32-master.zip (10.9 KB)

Instead of using

 #include <Servo.h>

#include <Servo.h>

Use #include ESP32Servo
GitHub - jkb-git/ESP32Servo: Arduino-compatible servo library for the ESP32.

Thanks for the advice, I edited the code and library files, but it sadly still didn't work. Any other ideas?

@auxiliaproject

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

What does "didn't work" mean? Do your Serial.prints show that the correct data is being received? If so quite a lot of it did work.

What servos are you using. How exactly are they connected and powered? Many/most servo problems we see turn out to be power or connection problems. Have you tried it with just one servo connected?

Steve

auxiliaproject:
Thanks for the advice, I edited the code and library files, but it sadly still didn't work. Any other ideas?

Have you got the servos working by themselves?

Say, using the servo sweep example but using the ESP32Servo Library.

slipstick:
What does "didn't work" mean? Do your Serial.prints show that the correct data is being received? If so quite a lot of it did work.

What servos are you using. How exactly are they connected and powered? Many/most servo problems we see turn out to be power or connection problems. Have you tried it with just one servo connected?

Steve

The serial.prints do give us a live feed on how the flex sensors are operating, we are receiving the data properly as far as we know. The issue is that the servo motors simply aren't doing anything.

Whiles you are working on getting the servos to work by themselves, why not edit your original post to include code tags?

Idahowalker:
Have you got the servos working by themselves?

Say, using the servo sweep example but using the ESP32Servo Library.

I just tested, it didn't work. We're pretty sure it's a hardware issue now, but we still don't know exactly what's wrong. We'll add a picture of the setup we're using to the thread, can you find anything wrong with it?

#include <ESP32Servo.h>
// create four servo objects
Servo servo1;
Servo servo2;
Servo servo3;
// Servo servo4;

// Published values for SG90 servos; adjust if needed
#define MINservo 1400
#define MAXservo 1600
#define ServoIncrement 1
// #define ServoIncrement 11
#define SerialDataBits 115200
#define iX_90z 1475 // 1400 is to the right for Chappie
#define iX_90y 1435 // 1400 tilts forward
#define iX_90x 1500

// These are all GPIO pins on the ESP32
// Recommended pins include 2,4,12-19,21-23,25-27,32-33
int servo1Pin = 4;
int servo2Pin = 32;
int servo3Pin = 33;
// int servo4Pin = 23;

int pos = 500;      // position in microseconds

void setup()
{
  Serial.begin( SerialDataBits );

  servo1.setPeriodHertz(50);      // Standard 50hz servo
  servo2.setPeriodHertz(50);      // Standard 50hz servo
  servo3.setPeriodHertz(50);      // Standard 50hz servo
  //	servo4.setPeriodHertz(50);      // Standard 50hz servo

  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin, MINservo, MAXservo);
  servo3.attach(servo3Pin, MINservo, MAXservo);
  // servo4.attach(servo4Pin, MINservo, MAXservo);
  servo1.writeMicroseconds( iX_90z );
  delay(20);
  servo2.writeMicroseconds( iX_90y );
  delay(20);
  servo3.writeMicroseconds( iX_90x );
  delay(20);


}

void loop()
{


  for (pos = MINservo; pos <= MAXservo; pos += ServoIncrement) { // sweep from A degrees to Z degrees
    Serial.print ( "+" );
    Serial.println ( pos );
    servo1.writeMicroseconds ( pos ); // z servo
    delay(10);             // waits Xms for the servo to reach the position
    servo2.writeMicroseconds ( pos );
    delay(10);             // waits Xms for the servo to reach the position
    servo3.writeMicroseconds ( pos );
    delay(10);             // waits 20ms for the servo to reach the position
  }
  for (pos = MAXservo; pos >= MINservo; pos -= ServoIncrement) { // sweep from A degrees to Z degrees
    Serial.print ( "-" );
    Serial.println ( pos );
    servo1.writeMicroseconds ( pos );
    delay(10);             // waits Xms for the servo to reach the position
    servo2.writeMicroseconds ( pos );
    delay(10);
    servo3.writeMicroseconds ( pos );
    delay(10);             // waits Xms for the servo to reach the position
  }
  
}

When you make big code updates means the code originally posted is useless, so update your code in your postings. Could you post the magic code you used to test the servos by themselves?

How are you supplying power to the servos?

Are the servos connected to a breadboard?

Besides 'it does not work' which is not very helpful, what does it does not work mean?

Are you powering the servos with a 9V battery?

Here's the current code I'm using to test a single motor, there's also a schematic posted underneath the code on the current total setup of the motors. They are receiving power from multiple 1.5V batteries instead of a 9V

#include <ESP32_Servo.h>

Servo myservo; // create servo object to control a servo
// 16 servo objects can be created on the ESP32

int pos = 0; // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
int servoPin = 4;

void setup() {
myservo.attach(servoPin); // attaches the servo on pin 18 to the servo object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
}

void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

I tried but, it is obvious you'll not follow instructions or answer questions. Good luck.

How many is "multiple" AA batteries? Your hopeless picture seems to have 5 plus another one making 6.

And I'll ask once more...WHAT SERVOS ARE YOU USING? There are very few servos that can tolerate 9V.

Also servos use more current than can safely be carried by a breadboard so that's another likely failure point.

Steve

Simple servo test code to see if you can get just one servo to work.

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {

  while (Serial.available()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}