This is amazing it finally works hc-sr04 sensor works flawlessly but my accelerometer doesn't work I think
. Did I solder it badly or it is a bad wiring, it is supposed to light up a red led when it works no ?
This is amazing it finally works hc-sr04 sensor works flawlessly but my accelerometer doesn't work I think
Well, more problems.
Looks like the code is written for the older MPU6050 and not the MPU9250 that you have.
The I2C interface is 3.3V. Normally that would not be a problem but you already have the LCD connected to the I2C and it uses a 5V interface.
You could buy a 3.3V to 5V voltage tanslator to fix the voltage difference problem but I don't know anything about code for the MPU9250
Ah I see, thankfully I also have this version, I just need to solder it:
The code may work but we still have the 3.3V and 5V problem.
Dont connect either board just et
Ok could you provide me a image of what voltage translator best fitting for my situation ?
I found this:
Be careful!
There are some fake ones on the internet and I think that is one of them
Here are three good ones
You may find them on Amazon and other places
Ok Thank you I will order this part, solder the mpu6050 sensor and I will write back to you. Is there anything that I need to know/buy or any other problems ? If not Thanks again Jim. Could you also help my friend with his project he is struggling pretty hard : Problem with project - #14 by 1marekparek123 Thanks.
I can't see what else could go wrong. You basically re-did the entire project.
Hello Jim, I have a problem with deadline. My project has to be completed this Monday but my voltage translator will come Monday afternoon, where I will already hand over the project.
Should I connect the mpu6050 sensor an see what happens or is there a way to rewire something in a different way so it could work? Because I read multiple forums where people connect their sensor in 3.3v pin and it works just fine. Thanks.
Post the code you are using so I can see what you have so far
In the Arduino IDE click on EDIT the click on Copy for Forum.
Come back and Paste your code here
#include<Wire.h>
#include <LiquidCrystal_I2C.h> //Liraries for LCD display
#define button 4
const int MPU_addr=0x68; // I2C address of the MPU-6050
float AcX,AcY,AcZ,GyX,GyY,GyZ,Tmp;
int trigPin = 3;
int echoPin = 2; //Digital pin input value
float count_shot = 0;
float made_shot = 0;
float field_pct = 0;
int button_pos = 0;
float toll = 0.05;
float normal_z = 0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(trigPin, OUTPUT); //Ultrasonic Pins
pinMode(echoPin, INPUT);
lcd.begin(20, 4);
lcd.clear();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker");
lcd.setCursor(0, 1);
lcd.print("Field Goal:"); //LCD Print
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
}
void loop(){
for(int ind = 0; ind < 5; ind++) {
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) //X, Y, Z output realing calculations
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)// 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
float normal_z = AcZ/16384;
}
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) //X, Y, Z output realing calculations
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)// 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
float ac_x = AcX/16384;
float ac_y = AcY/16384;
float ac_z = AcZ/16384;
float gy_x = GyX/131;
float gy_y = GyY/131;
float gy_z = GyZ/131;
double duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW); //Inputs on Ultrasonic distance sensor
duration=pulseIn(echoPin, HIGH);
double dist = (duration/2.0/29.1); //Calculation for distance
if (dist < 22 or ac_z < (normal_z - toll) or ac_z > (normal_z + toll)) {
count_shot += 1;
if (dist < 22) {
made_shot += 1;
delay(2000);
} else {
bool flag = 0;
double time1 = millis();
double time_diff = 0;
while (flag == 0 and time_diff <= 2000) {
double duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW); //Inputs on Ultrasonic distance sensor
duration=pulseIn(echoPin, HIGH);
double dist = (duration/2.0/29.1);
if (dist < 22) {
made_shot += 1; //Calculation for whether shot actually went into hoop
flag = 1;
delay(2000 - time_diff);
}
double time2 = millis();
time_diff = time2 - time1;
}
// int extra_time = 2000 - time_diff;
// delay(extra_time);
}
// Serial.print("Field Goal: ");
// Serial.print(made_shot);
// Serial.print(" / ");
// Serial.println(count_shot);
// delay(1000);
if(count_shot > 0) {
field_pct = made_shot / count_shot * 100; //Field Goal % Calculation
} else {
field_pct = 0;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker"); //LCD display intially
lcd.setCursor(0, 1);
lcd.print("Field Goal:");
if(made_shot < 10) {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
} else if(made_shot < 100 and made_shot >= 10) {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(2, 2);
lcd.print("-");
lcd.setCursor(3, 2); //LCD Print field goals made / attempted
lcd.print(count_shot, 0);
} else {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(3, 2);
lcd.print("-");
lcd.setCursor(4, 2);
lcd.print(count_shot, 0);
}
lcd.setCursor(19, 2);
lcd.print("%"); //LCD print % sign for field goal percentage
lcd.setCursor(14, 2);
lcd.print(field_pct, 1);
}
//field goal percentage logic
// Serial.println(field_pct);
//lcd code
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//reset button code
button_pos = digitalRead(button);
if(button_pos == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RESET");
made_shot = 0;
count_shot = 0;
field_pct = 0;
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker"); //LCD print for if reset button is pressed
lcd.setCursor(0, 1);
lcd.print("Field Goal:");
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
}
}
I also tested the mpu6050 sensor and its working :
The problem now is hc-sr04 sensor is counting a scored hoop almost every second without movement(its too sensitive?) and a mpu6050 sensor is doing nothing.
How did you conect the MPU6050 wihout a voltage translator?
Which Liquid Crystal library are you using?
2.I use this library:
NewLiquidCrystal-master.zip (2,2 MB)
Leave that disconnected, see if it works.
Still the same thing mpu6050 is on, but it dosent do anything and the hc-sr04 sensor is counting without movement. I think there is a problem in code that makes mpu not active.
I have one more thing you can try.
I am listening.
This program uses a software version for I2C
MPU6050 connections:
VCC --> 3.3V
GND --> GND
SCL --> 7
SDA --> 6
Go to this library and follow the installation instructions.
Click on the <> code button near the top to download the zip file.
Run my program (it's a modification of your program)
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //Liraries for LCD display
#include <SoftI2C.h>
#define button 4
const int MPU_addr=0x68; // I2C address of the MPU-6050
int AcX,AcY,AcZ;
const int trigPin = 3;
const int echoPin = 2; //Digital pin input value
float count_shot = 0;
float made_shot = 0;
float field_pct = 0;
int button_pos = 0;
float toll = 0.05;
float normal_z = 0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftI2C SoftWire = SoftI2C(6,7); // SDA, SCL
void setup(){
Wire.begin();
SoftWire.begin();
SoftWire.beginTransmission(MPU_addr);
SoftWire.write(0x6B); // PWR_MGMT_1 register
SoftWire.write(0); // set to zero (wakes up the MPU-6050)
SoftWire.endTransmission(true);
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(trigPin, OUTPUT); //Ultrasonic Pins
pinMode(echoPin, INPUT);
lcd.begin(20, 4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker");
lcd.setCursor(0, 1);
lcd.print("Field Goal:"); //LCD Print
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
}
void loop(){
for(int ind = 0; ind < 5; ind++) {
SoftWire.beginTransmission(MPU_addr);
SoftWire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
SoftWire.endTransmission(false);
SoftWire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=SoftWire.read()<<8|SoftWire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=SoftWire.read()<<8|SoftWire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) //X, Y, Z output realing calculations
AcZ=SoftWire.read()<<8|SoftWire.read();
float normal_z = float(AcZ/16384);
}
SoftWire.beginTransmission(MPU_addr);
SoftWire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
SoftWire.endTransmission(false);
SoftWire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=SoftWire.read()<<8|SoftWire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=SoftWire.read()<<8|SoftWire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) //X, Y, Z output realing calculations
AcZ=SoftWire.read()<<8|SoftWire.read();
float ac_x = float(AcX/16384);
float ac_y = float(AcY/16384);
float ac_z = float(AcZ/16384);
double duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(12);
digitalWrite(trigPin, LOW); //Inputs on Ultrasonic distance sensor
duration=pulseIn(echoPin, HIGH);
double dist = (duration/2.0/29.1); //Calculation for distance
if (dist < 22 or ac_z < (normal_z - toll) or ac_z > (normal_z + toll)) {
count_shot += 1;
if (dist < 22) {
made_shot += 1;
delay(2000);
} else {
bool flag = 0;
double time1 = millis();
double time_diff = 0;
while (flag == 0 and time_diff <= 2000) {
double duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(12);
digitalWrite(trigPin, LOW); //Inputs on Ultrasonic distance sensor
duration=pulseIn(echoPin, HIGH);
double dist = (duration/2.0/29.1);
if (dist < 22) {
made_shot += 1; //Calculation for whether shot actually went into hoop
flag = 1;
delay(2000 - time_diff);
}
double time2 = millis();
time_diff = time2 - time1;
}
// int extra_time = 2000 - time_diff;
// delay(extra_time);
}
// Serial.print("Field Goal: ");
// Serial.print(made_shot);
// Serial.print(" / ");
// Serial.println(count_shot);
// delay(1000);
if(count_shot > 0) {
field_pct = made_shot / count_shot * 100; //Field Goal % Calculation
} else {
field_pct = 0;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker"); //LCD display intially
lcd.setCursor(0, 1);
lcd.print("Field Goal:");
if(made_shot < 10) {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
} else if(made_shot < 100 and made_shot >= 10) {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(2, 2);
lcd.print("-");
lcd.setCursor(3, 2); //LCD Print field goals made / attempted
lcd.print(count_shot, 0);
} else {
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(3, 2);
lcd.print("-");
lcd.setCursor(4, 2);
lcd.print(count_shot, 0);
}
lcd.setCursor(19, 2);
lcd.print("%"); //LCD print % sign for field goal percentage
lcd.setCursor(14, 2);
lcd.print(field_pct, 1);
}
//field goal percentage logic
// Serial.println(field_pct);
//lcd code
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//reset button code
button_pos = digitalRead(button);
if(button_pos == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RESET");
made_shot = 0;
count_shot = 0;
field_pct = 0;
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot Tracker"); //LCD print for if reset button is pressed
lcd.setCursor(0, 1);
lcd.print("Field Goal:");
lcd.setCursor(0, 2);
lcd.print(made_shot, 0);
lcd.setCursor(1, 2);
lcd.print("-");
lcd.setCursor(2, 2);
lcd.print(count_shot, 0);
}
}
Wow Jim, the hc-sr04 sensor works perfectly now. Only problem now is that the mpu6050 dosent count misses but acts as a second sensor (it counts makes not misses when I touch it). Is there a fix for that ?