Arduino caliper i2c read
Help me by sharing this post

This is the code for the Arduino i2c caliper serial data read. Read all comments in the code in order to understand more.





schematic arduino caliper i2c read



/* Read the caliper data with Arduino and display mm or inch on serial monitor and LCD
 * Tutorial on: https://www.electronoobs.com/eng_arduino_tut93.php
 * Schematic: https://www.electronoobs.com/eng_arduino_tut93_sch1.php
 * 
Caliper         |       Arduino
GND (black)             GND + 200 ohm 
DAT (brown)             D11
CLK (blue)              D12
VCC 3.3V (red)          3.3V
 */

#define CLOCK_PIN 12
#define DATA_PIN  11


void setup() 
{
  Serial.begin(9600);  
  pinMode(CLOCK_PIN, INPUT);
  pinMode(DATA_PIN, INPUT);
  
}

char buf[20];
unsigned long tmpTime;
int sign;
int inches;
long value;
float result;
bool mm = true; //define mm to false if you want inces values

void loop()
{
  while(digitalRead(CLOCK_PIN)==LOW) {}
  tmpTime=micros();
  while(digitalRead(CLOCK_PIN)==HIGH) {}
  if((micros()-tmpTime)<500) return;
  readCaliper(); 
  buf[0]=' ';
  dtostrf(result,6,3,buf+1); strcat(buf," in ");  
  dtostrf(result*2.54,6,3,buf+1); strcat(buf," cm "); 

  if(mm)
  {
    Serial.print(result); Serial.println(" mm");    
    delay(100);
  }
  else
  {
    Serial.print(result); Serial.println(" in");    
    delay(100);
  }
}
void readCaliper()
{
  sign=1;
  value=0;
  inches=0;
  for(int i=0;i<24;i++) {
    while(digitalRead(CLOCK_PIN)==LOW) {}
    while(digitalRead(CLOCK_PIN)==HIGH) {}
    if(digitalRead(DATA_PIN)==HIGH) {
      if(i<20) value|=(1<<i);
      if(i==20) sign=-1;
      if(i==23) inches=1; 
    }
  }
  if(mm)
  {
    result=(value*sign)/100.0;
  }
  else
  {
  result=(value*sign)/(inches?2000.0:100.0); //We map the values for inches, define mm to false if you want inces values
  }
  
}








yt_link
insta_link
fb_link
twitter_link

Arduino caliper


ADVERTISERS
PCBWAY PCB service





Gearbest Alfawise U30 2.8 inch Touch Screen DIY Desktop 3D Printer - U30 EU Plug Black
Alfawise U30 2.8 inch Touch Screen DIY Desktop 3D Printer - U30 EU Plug Black only $$162.89

AFFILATE

Gearbest Creality3D CR - 10S 3D Printer - EU Plug Upgrade Version Coffee and Black
Creality3D CR - 10S 3D Printer - EU Plug Upgrade Version Coffee and Black only $$352.94

Gearbest Creality 3D Ender-3 V-slot Prusa I3 DIY 3D Printer Kit 220 x 220 x 250mm Printing Size - EU Plug Black
Creality 3D Ender-3 V-slot Prusa I3 DIY 3D Printer Kit 220 x 220 x 250mm Printing Size - EU Plug Black only $$153.84

Gearbest Alfawise U20 Large Scale 2.8 inch Touch Screen DIY 3D Printer - EU - U20 EU Plug Black
Alfawise U20 Large Scale 2.8 inch Touch Screen DIY 3D Printer - EU - U20 EU Plug Black only $$253.39