Thomas Angielsky
Published © GPL3+

Drill Depth Display with Gyro Sensor

If you have to drill a certain depth for a project, you need a bench drill with depth display. See how simple it works in this project.

IntermediateFull instructions provided4 hours8,977

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

3D-Models STL and Autodesk Fusion 360 files

Schematics

bta-schaltung-arduino-gyro-7segment_pX6HtrjAi6.jpg

Code

drilling-depth.ino

Arduino
/* program: drilling-depth.ino
 * author:  Thomas Angielsky
 * version: 2021-03-20
 * 
 * https://techpluscode.de/
 * https://techpluscode.de/bohrtiefenanzeige-mit-gyro-sensor/
 * 
 * this sketch measures the drilling depth of a table drilling
 * machine by using data of GY-521 gyro sensor
 * depth is viewed by 7 segment display
 * 
 * idea of calc error compensation from electronoobs.com - thanks!
 * 
 */


#include <Wire.h>
#include <math.h>
#include "LedControl.h"

#define MPU 0x68

char txt[8];
String s;
float currentPos,stopPos;
float rad_to_deg;
float AccX, AccY, AccZ;  
float AccAngleX, AccAngleY;         
float AccAngleErrorX, AccAngleErrorY; 
float TotalAngleX, TotalAngleY;
float DrillingMachineAngle, delta;

//init LED's: pin D12, pin D11, pin D10, 1 module
LedControl lc=LedControl(12,11,10,1);


void setup() {
  //prepare serial connection
  //Serial.begin(9600);

  
  //start values
  stopPos=0.0;
  currentPos=0.0;  
  rad_to_deg = 180/PI;

  //prepare GY-521 sensor
  //we use only accelerator data
  Wire.begin(); 
  Wire.beginTransmission(MPU);         
  Wire.write(0x6B);
  Wire.write(0x00); // wake up MPU
  Wire.endTransmission(true); 

  //this delay was very necessary in my case!
  delay(1000);
  Wire.beginTransmission(MPU);
  Wire.write(0x1C); //register ACCEL_CONFIG 
  Wire.write(0x10); //Set as 00010000 for +/- 8g full scale range
  Wire.endTransmission(true); 


  //calculate the accellerator data error
  //do 100 measurments
  for(int a=0; a<100; a++) {
      Wire.beginTransmission(MPU);
      Wire.write(0x3B);
      Wire.endTransmission(false);
      Wire.requestFrom(MPU,6,true); 
      
      AccX=(Wire.read()<<8|Wire.read())/4096.0 ; 
      AccY=(Wire.read()<<8|Wire.read())/4096.0 ;
      AccZ=(Wire.read()<<8|Wire.read())/4096.0 ;
      
      AccAngleErrorX=AccAngleErrorX+((atan((AccY)/sqrt(pow((AccX),2)+pow((AccZ),2)))*rad_to_deg));
      //AccAngleErrorY=AccAngleErrorY+((atan(-1*(AccX)/sqrt(pow((AccY),2)+pow((AccZ),2)))*rad_to_deg)); 
    }
      
  AccAngleErrorX=AccAngleErrorX/100;
  //AccAngleErrorY=AccAngleErrorY/100;
  
  //prepare button: pin D9
  pinMode(9,INPUT_PULLUP);

  //prepare 7 segment display
  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
}


void loop() {
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,6,true);
  
  AccX=(Wire.read()<<8|Wire.read())/4096.0; 
  AccY=(Wire.read()<<8|Wire.read())/4096.0;
  AccZ=(Wire.read()<<8|Wire.read())/4096.0; 

  AccAngleX=(atan((AccY)/sqrt(pow((AccX),2)+pow((AccZ),2)))*rad_to_deg)-AccAngleErrorX;
  //AccAngleY=(atan(-1*(AccX)/sqrt(pow((AccY),2)+pow((AccZ),2)))*rad_to_deg)-AccAngleErrorY;    

  //smooth values
  TotalAngleX=0.5*(TotalAngleX)+0.5*AccAngleX;
  //TotalAngleY=0.5*(TotalAngleY)+0.5*AccAngleY;

  //calculate x angle to 360 by using +/- of X,Y,Z
  delta=0;
  
  if ((AccZ<0)) { 
    delta=180.0-TotalAngleX*2.0;
  }

 if ((AccZ>0)&&(AccY<0)) {
    delta=360.0;
  }

  DrillingMachineAngle=TotalAngleX+delta;
  //if near 360, display better 0
  if (DrillingMachineAngle>350) {DrillingMachineAngle=0;}

  //calculate drilling depth
  //max drilling depth: 50 mm (measured on machine)
  //max angle of hand wheel: 316 (measured with Serial.print data)
  currentPos=50.0/316.0*DrillingMachineAngle;


  /*
  Serial.print("X / X / Y / Z / E : BOHRTIEFE");
  Serial.print(TotalAngleX);
  Serial.print(" ");
  Serial.print(AccX);
  Serial.print(" ");
  Serial.print(AccY);
  Serial.print(" ");
  Serial.print(AccZ);  
  Serial.print(" >> ");
  Serial.print(DrillingMachineAngle);
  Serial.print(" >> ");
  Serial.print(currentPos);
  Serial.println(" ");
*/
  
  //button pressed?
  if (digitalRead(9)==LOW) {
    //store stop position
    stopPos=currentPos;        
    lc.setChar(0,3,'8',false);
    lc.setChar(0,2,'8',false);
    lc.setChar(0,1,'8',false);
    lc.setChar(0,0,'8',false); 
    //Serial.println("Button pressed");
    delay(1000);
  }

  if (stopPos>0) {
    //stop position reached?
    if (currentPos>=stopPos) {
      //yes: display STOP (5t0P) on right side
      lc.setChar(0,3,'5',false);
      lc.setRow(0,2,B00001111);
      lc.setChar(0,1,'0',false);
      lc.setChar(0,0,'P',false);
    } else {
      //no: display distance to stop position
      dtostrf(stopPos-currentPos,4,1,txt);
      s=' '+String(txt);
      lc.setChar(0,0,s.charAt(s.length()-1),false); 
      lc.setChar(0,1,s.charAt(s.length()-3),true); 
      lc.setChar(0,2,s.charAt(s.length()-4),false); 
      lc.setChar(0,3,s.charAt(s.length()-5),false); 
    }
  } else {
      //display nothing
      lc.setChar(0,3,' ',false);
      lc.setChar(0,2,' ',false);
      lc.setChar(0,1,' ',false);
      lc.setChar(0,0,' ',false);        
  }

  //display current drilling depth on left side
  //format as string
  dtostrf(currentPos,4,1,txt);
  s=' '+String(txt);
 
  lc.setChar(0,4,s.charAt(s.length()-1),false); 
  lc.setChar(0,5,s.charAt(s.length()-3),true); 
  lc.setChar(0,6,s.charAt(s.length()-4),false); 
  lc.setChar(0,7,s.charAt(s.length()-5),false); 

  delay(200);
}

Credits

Thomas Angielsky

Thomas Angielsky

18 projects • 36 followers
Mechanical engineer, maker, love woodwork, like Lazarus

Comments