2

I am using Standalone ATMEGA328P-PU to get the accelerometer data from mpu6050 and send to to Serial at baudrate 115200 and also it sends the data to another serial(to HC05 bluetooth module). But the problem is that sometimes I am facing a strange scenerio, the atmega328p-pu accepts program through usb to ttl converter, but the controller cannot send any data through serial. The serial data in both hc05 bluetooth and usb serial are blank. Anyone knows any possible reasons for that. I am using the following code.

I have tried checking the connections on veroboard, but this situation sometimes fix, and sometimes reappear.

#include <SoftwareSerial.h>
#include "I2Cdev.h" // include the I2Cdev library
#include "MPU6050.h" // include the accelerometer library

SoftwareSerial bt(3,4); /* (Rx,Tx) */
MPU6050 accelgyro;  // set device to MPU6050
int16_t ax, ay, az, gx, gy, gz;  // define accel as ax,ay,az
int baselineX = 0;

void setup() {
  Wire.begin();      // join I2C bus
  Serial.begin(115200);    //  initialize serial communication
  bt.begin(9600);
  accelgyro.initialize();  // initialize the accelerometer
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  baselineX = gz;
}
void loop() {
  // read measurements from device
  sendAverage();
}

long sendAverage() {
  long totalX = 0, totalY = 0, totalZ = 0;
  long X, Y, Z;
  for (int i = 0; i < 20; i++) {
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    totalX = totalX + ax;
    totalY = totalY + ay;
    totalZ = totalZ + az;
    delay(1);
  }
  X = 500+ ((totalX/20)*0.05);
  Y = 500+ ((totalY/20)*0.05);
  Z = 500+ ((totalZ/20)*0.05);

  Serial.print(X);Serial.print(";");
  Serial.print(Y);Serial.print(";");
  Serial.println(Z);

  bt.print(X);bt.print(";");
  bt.print(Y);bt.print(";");
  bt.print(Z);bt.print("#");
}

1 Answers1

0

You're using the SoftwareSerial class to change the pins for the serial transmission, but in the setup() you're not setting the properties of the two pins. If you want to transmit through the SoftwareSerial class, add the pinMode :

SoftwareSerial bt =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx of SoftwareSerial:
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  // set the data rate for the SoftwareSerial port
  bt.begin(9600);
}

For a complete reference, see the SoftwareSerial.begin documentation page

pittix
  • 161
  • 12