a weird phenomenon

Thread Starter

LAOADAM

Joined Nov 21, 2018
956
I am testing a MEGA 2560 to control a pair of motors by PWM at frequency about 3.9KHz. I made a interface circuit like the picture attached by manually soldering.

The motor on P10 lost control, measured the PWM waveform after interface board by oscilloscope shown the P10 has a base voltage of about 2.1 V.
Both P9 & P10 shown same nice and neat PWM measured directly from MEGA2560, checked the board nothing wrong.

Post here and see if any body had the similar situation or what can be the reason?

Thanks
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,237
Still can’t see picture.

And can you provide a code listing (use code tags)?

Have you tested the software without the interface board?
 

Thread Starter

LAOADAM

Joined Nov 21, 2018
956
Sorry, I don't know how to show the picture.
The 'lost control ' I mean the motor at P10, some time run ,some time not run, when the run button pressed, the motor at P9 runs follow the button.
The motor runs automatically when MEGA power on.
https://imgur.com/gallery/G7HNVn5
 

Thread Starter

LAOADAM

Joined Nov 21, 2018
956
Still can’t see picture.

And can you provide a code listing (use code tags)?

Have you tested the software without the interface board?
Yes, it is a 433MHz remote control unit. The sketch as:
Code:
#include <VirtualWire.h>

int STBY = 7; //standby  // mod: was 10 --2 ??

int count;

//Motor A
int PWMA = 9; //Speed control   // need mod: was 3 change into 9
int AIN1 = 5; //Direction  // was 9
int AIN2 = 4; //Direction

//Motor B
int PWMB = 10; //Speed control   // need mod: was 5 change into 10
int BIN1 = 6; //Direction   // was 6
int BIN2 = 3; //Direction   ///was 16 put into p7 cause of Arduino uno no p16.

const boolean FORWARD = HIGH;
const boolean REVERSE = LOW;

float speed_Max = 252; //pwm usually goes from was:0-255 ////0-255 changed into 0-252

float speed_Min = 0;
float analogInput_Max = 1023;
float analogInput_Min = 0;
float analogInput_Middle_X = 515;//ideally it would be analogInput_Max / 2
float analogInput_Middle_Y = 495;//ideally it would be analogInput_Max / 2
float deadBand = 0;
float middleMax = (analogInput_Max / 2) + deadBand;
float middleMin = (analogInput_Max / 2) - deadBand;
boolean pastDirection = FORWARD;

void setup()
{
  // Configure Timer 2 for PWM @ 14 kHz.
  TCCR2A = 0;           // undo the configuration done by...
  TCCR2B = 0;           // ...the Arduino core library
  TCNT2  = 0;           // reset timer

  TCCR2A = _BV(COM2A1)  // non-inverted PWM on ch. A
           | _BV(COM2B1)  // same on ch; B
           | _BV(WGM20);  // mode 10: ph. correct PWM, TOP = ICR1

 
    TCCR2B = (TCCR2B & 0b11111000) | 0x02; //3.92116 [kHz] //

    {

    Serial.begin(9600);  // Debugging only
    Serial.println("setup"); //Prints "Setup" to the serial monitor
   
    vw_set_rx_pin(2);       //Sets pin D12 as the RX Pin
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);      // Bits per sec
    vw_rx_start();       // Start the receiver PLL running
  
    pinMode(19, OUTPUT); //Relay one ////M1 added 2 was 2 -- 4
    pinMode(21, OUTPUT); //Relay two
    pinMode(35, OUTPUT); //Relay four
    pinMode(37, OUTPUT); //Relay five
    pinMode(39, OUTPUT); //Relay sex
    pinMode(41, OUTPUT); //Relay seven
    pinMode(51, OUTPUT); //Relay eight
    pinMode(42, OUTPUT); //Relay three

    digitalWrite(19, HIGH); ////M1 added 2 was 2 -- 4
    digitalWrite(21, HIGH);
    digitalWrite(35, HIGH);
    digitalWrite(37, HIGH);
    digitalWrite(39, HIGH);
    digitalWrite(41, HIGH);
    digitalWrite(51, HIGH);
    digitalWrite(42, HIGH);
     }
 
  pinMode(STBY, OUTPUT);

  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
}

void loop() {

button();
joystick();
}

void joystick() {

  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen)) // Non-blocking
  {
    int i;
    int column = 0;
    String message;
    int commands[30];
   
    for (i = 0; i < buflen; i++)
    {
      //DEBUG:
      //Serial.print(char(buf[i]));

      if (char(buf[i]) == '|') {
        commands[column] = message.toInt();
        message = "";
        column++;
      } else {
        message += char(buf[i]);
      }
    }

  
    commands[column] = message.toInt();

   

     motorControl(commands[0], commands[1]);
  
  }

}

void move(int motor, int speed, boolean direction) {
  digitalWrite(STBY, HIGH); //disable standby

   if (motor == 1) {
    digitalWrite(AIN1, direction);
    digitalWrite(AIN2, !direction);
    analogWrite(PWMA, speed);
  } else {
    digitalWrite(BIN1, !direction);
    digitalWrite(BIN2, direction);
    analogWrite(PWMB, speed);
  }
}

void motorControl(float x, float y) {
  boolean currentDirection = y >= analogInput_Middle_Y;

    //map(value, fromLow, fromHigh, toLow, toHigh);
  if (currentDirection == REVERSE) {
    y = map(y, analogInput_Middle_Y, analogInput_Min, speed_Min, speed_Max) ;
  } else {
    y = map(y, analogInput_Middle_Y, analogInput_Max, speed_Min, speed_Max);
  }

  int subtractFromLeft = map(x, analogInput_Middle_X, analogInput_Min, speed_Min, y);
  int subtractFromRight = map(x, analogInput_Middle_X, analogInput_Max, speed_Min, y);

  if (subtractFromRight < 0) {
    subtractFromRight = 0;
  }

  if (subtractFromLeft < 0) {
    subtractFromLeft = 0;
  }

  int Throttle_RIGHT = y - subtractFromRight;
  int Throttle_LEFT = y - subtractFromLeft;

  boolean currentDirection_LEFT = currentDirection;
  boolean currentDirection_RIGHT = currentDirection;


  if (Throttle_LEFT < 1 && Throttle_RIGHT > 1) {
    currentDirection_LEFT = !currentDirection;
    Throttle_LEFT = Throttle_RIGHT;
  }

  if (Throttle_RIGHT < 1 && Throttle_LEFT > 1) {
    currentDirection_RIGHT = !currentDirection;
    Throttle_RIGHT = Throttle_LEFT;
  }

  move(1, Throttle_LEFT, currentDirection_LEFT);
  move(2, Throttle_RIGHT, currentDirection_RIGHT);

  //    Serial.print("Throttle_LEFT: ");
  //    Serial.print(Throttle_LEFT);
  //    Serial.print(" Throttle_RIGHT: ");
  //    Serial.print(Throttle_RIGHT);
  //    Serial.println("");
  //
  //    if(Throttle_LEFT >= -400 && Throttle_LEFT <= 400){
  //        motors.setM1Speed( Throttle_LEFT );
  //    }
  //
  //    if(Throttle_RIGHT >= -400 && Throttle_RIGHT <= 400){
  //        motors.setM2Speed( Throttle_RIGHT );
  //    }
  //    delay(2);

}

void button()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen)) // Non-blocking
  {
    int i;
    digitalWrite(13, true);  // Flash a light to show received good message
    // Message with a good checksum received, dump it.
    Serial.print("Got: ");
   
    for (i = 0; i < buflen; i++)
    {
      int c = (buf[i]);
      Serial.print(c);
      Serial.print(" ");
      if (c == 65 ) {
        digitalWrite(19, !digitalRead(19));
      }

      if (c == 66 ) {
        digitalWrite(21, !digitalRead(21));
      }

      if (c == 67 ) {
        digitalWrite(35, !digitalRead(35));
      }

      if (c == 68 ) {
        digitalWrite(37, !digitalRead(37));
      }

      if (c == 69 ) {
        digitalWrite(39, !digitalRead(39));
      }

      if (c == 70 ) {
        digitalWrite(41, !digitalRead(41));
      }

      if (c == 71 ) {
        digitalWrite(51, !digitalRead(51));
      }

      if (c == 72 ) {
        digitalWrite(42, !digitalRead(42));
      }
    }
    count++;
    // Serial.print(count);
    Serial.println("");
    digitalWrite(13, false);
  }
}
 

Thread Starter

LAOADAM

Joined Nov 21, 2018
956
I guess the reason may be the electronic circuit interference? because the PWM work at about 3.9KHz, how to avoid?
 

bertus

Joined Apr 5, 2008
22,923
Hello,

The posted schematic is not complete and to small to read.
Please try to make a complete schematic and post it here.

Bertus
 
Top