Go Down

Topic: reading digital calipers with arduino zero (Read 817 times) previous topic - next topic

papakel

Hi all, I am having great trouble getting an accurate reading from my digital calipers that I want to use as a lathe DRO I have two types of caliper one uses a 3volt battery which I would expect to generate an active high signal and the other a 1.2 volt battery and I have connected clock pin 13 and data pin 12 when I use the 3volt the readings work but are not correct the 1.2 volt does not trigger an active high and so does notwork at all. I have tried connecting the power for the caliper to the arduino 3.3volt pin  and ground but still I get incorrect readings.I enclose my edited copy of the files I have used can anyone please help.



/* Read the caliper data with Arduino and display mm 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
   This version of mine is a combination of the above sketch adapted to print on an I2C 20 x 4 lcd
   
  Caliper         |       Arduino
 
  GND (black)             GND + 200 ohm used only for the 1.2volt caliper
  DAT (purple)             D12
  CLK (orange)              D13
  VCC 3.3V (red)          3.3V
*/

#include <Wire.h>
#include <Scheduler.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  #define CLOCK_PIN 13
  #define DATA_PIN  12
  #define CLOCK1_PIN 11
  #define DATA1_PIN  10
 
  pinMode(CLOCK_PIN, INPUT);
  pinMode(DATA_PIN, INPUT);
  pinMode(CLOCK1_PIN, INPUT);
  pinMode(DATA1_PIN, INPUT);
  lcd.begin();    // initialize the LCD
  lcd.clear();
  lcd.backlight(); 
  lcd.setCursor(3, 0);
  lcd.print("My Digital DRO");
  lcd.setCursor(3, 1);
  lcd.print("Bed: 0.00 mm.");
  lcd.setCursor(0, 2);
  lcd.print("Xslide: 0.00 mm.");
  lcd.setCursor(1, 3);
  lcd.print("Powered by Arduino");
 Scheduler.startLoop(loop1);
  }



void loop()
{
  char buf[20];
  unsigned long tmpTime;
  while(digitalRead(CLOCK_PIN)==LOW) {} //if clock is high wait until it turns to high
  tmpTime=micros();
  while(digitalRead(CLOCK_PIN)==HIGH) {}  //wait for the end of the low pulse
  if((micros()-tmpTime)<500) return;  //if the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
  readCaliper();
  buf[0]=' ';

}
void readCaliper()
{
  char buf[20];
  int sign;
  //int inches;
  long value;
  float result;
  bool mm = true; //define mm to false if you want inces values
  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);
    lcd.setCursor(3, 1);lcd.print("                 ");
    lcd.setCursor(3, 1);lcd.print("Bed: ");lcd.print(result);lcd.print(" mm.  ");
    delay(100);
  }
  else
  {
//  result=(value*sign)/(inches?2000.0:100.0); //We map the values for inches, define mm to false if you want inces values
  }
 
}

void loop1()
{
  char buf1[20];
  unsigned long tmp1Time;
  while(digitalRead(CLOCK1_PIN)==LOW) {} //if clock is high wait until it turns to high
  tmp1Time=micros();
  while(digitalRead(CLOCK1_PIN)==HIGH) {}  //wait for the end of the low pulse
  if((micros()-tmp1Time)<500) return;  //if the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
  readCaliper1();
  buf1[0]=' ';

}
void readCaliper1()
{
  char buf1[20];
  int sign1;
  //int inches;
  long value1;
  float result1;
  bool mm = true; //define mm to false if you want inces values
  sign1=1;
  value1=0;
  //inches=0;
  for(int i=0;i<24;i++) {
    while(digitalRead(CLOCK1_PIN)==LOW) {}
    while(digitalRead(CLOCK1_PIN)==HIGH) {}
    if(digitalRead(DATA1_PIN)==HIGH) {
      if(i<20) value1|=(1<<i);
      if(i==20) sign1=-1;
      //if(i==23) inches=1;
     
   }
  }
  if(mm)
  {
   
    result1=((value1*sign1)/100.0);
    lcd.setCursor(3, 1);lcd.print("                 ");
    lcd.setCursor(0, 2);lcd.print("Xslide: ");lcd.print(result1);lcd.print(" mm.  ");  //intended to use for 2nd caliper
    delay(100);
  }
  else
  {
//  result=(value*sign)/(inches?2000.0:100.0); //We map the values for inches, define mm to false if you want inces values
  }
 
}

jremington

Please edit your post to add code tags, as described in "How to use this forum"

Post links to the calipers, and post a wiring diagram.

Go Up