This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TDC7201-ZAX-EVM: Interface between Arduino, Ultrasonic sensor and TDC

Part Number: TDC7201-ZAX-EVM
Other Parts Discussed in Thread: TDC7201, TDC7200

I'm trying to use the received signal from the HCSR04 ultrasonic sensor as a trigger for the TDC7201 and am controlling both using an arduino. While I can get an output from the ultrasonic sensor, I don't seem to be able to get anything from the TDC. I'm using the code below:

#include <SPI.h>
#include "tdc7200.h"
#include <NewPing.h>

// HC-SR04 configuration
#define TRIGGER_PIN 8
#define ECHO_PIN 9
#define MAX_DISTANCE 200

// TDC7200 configuration
int startpin = 4;
int stoppin = 7;
const byte CLOCKOUT = 11;
byte config1, config2;
tdc7200 chip1;

NewPing hc_sr04(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  pinMode(CLOCKOUT, OUTPUT);
  TCCR1A = bit(COM1A0);
  TCCR1B = bit(WGM12) | bit(CS10);
  OCR1A = 7;

  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV16);

  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);

  digitalWrite(3, LOW);
  delay(5);
  digitalWrite(3, HIGH);
  delay(5);

  Serial.begin(115200);
}

void loop() {
  // Trigger HC-SR04
  long duration = hc_sr04.ping_median();

  chip1.writeReg(CONFIG1, B00000011);
  chip1.writeReg(CONFIG2, B01000100);

  while (digitalRead(TRIGGER_PIN)) {
    break;
  }

  digitalWrite(startpin, HIGH);
  digitalWrite(startpin, LOW);

  delayMicroseconds(100);

  for (int i = 0; i < 4; i++) {
    digitalWrite(stoppin, HIGH);
    digitalWrite(stoppin, LOW);
    delayMicroseconds(100);
  }
  delay(8);
  digitalWrite(stoppin, HIGH);
  digitalWrite(stoppin, LOW);

  chip1.time1 = chip1.readReg24(TIME1);
  chip1.time2 = chip1.readReg24(TIME2);
  chip1.time3 = chip1.readReg24(TIME3);
  chip1.time4 = chip1.readReg24(TIME4);
  chip1.time5 = chip1.readReg24(TIME5);
  chip1.time6 = chip1.readReg24(TIME6);
  chip1.clockcount1 = chip1.readReg24(CLOCK_COUNT1);
  chip1.clockcount2 = chip1.readReg24(CLOCK_COUNT2);
  chip1.clockcount3 = chip1.readReg24(CLOCK_COUNT3);
  chip1.clockcount4 = chip1.readReg24(CLOCK_COUNT4);
  chip1.clockcount5 = chip1.readReg24(CLOCK_COUNT5);
  chip1.cal1 = chip1.readReg24(CALIBRATION1);
  chip1.cal2 = chip1.readReg24(CALIBRATION2);

  chip1.tof1 = chip1.getTOF(chip1.time1, chip1.time2, chip1.clockcount1, chip1.cal1, chip1.cal2);
  chip1.tof2 = chip1.getTOF(chip1.time1, chip1.time3, chip1.clockcount2, chip1.cal1, chip1.cal2);
  chip1.tof3 = chip1.getTOF(chip1.time1, chip1.time4, chip1.clockcount3, chip1.cal1, chip1.cal2);
  chip1.tof4 = chip1.getTOF(chip1.time1, chip1.time5, chip1.clockcount4, chip1.cal1, chip1.cal2);
  chip1.tof5 = chip1.getTOF(chip1.time5, chip1.time6, chip1.clockcount5, chip1.cal1, chip1.cal2);

  Serial.println();
  Serial.print("  ****************");
  Serial.println();
  Serial.print("* TOF1 = ");
  Serial.print(chip1.tof1, 8);
  Serial.println(" *");
  Serial.print("* TOF2 = ");
  Serial.print(chip1.tof2, 8);
  Serial.println(" *");
  Serial.print("* TOF3 = ");
  Serial.print(chip1.tof3, 8);
  Serial.println(" *");
  Serial.print("* TOF4 = ");
  Serial.print(chip1.tof4, 8);
  Serial.println(" *");
  Serial.print("* TOF5 = ");
  Serial.print(chip1.tof5, 8);
  Serial.println(" *");
  Serial.print("  ****************");
  Serial.println();

  if (digitalRead(2)) {
    Serial.println("Interupt");
  }

  if (duration != 0) {
    Serial.print("Distance: ");
    Serial.print(duration / 29.1); // Convert to cm
    Serial.println(" cm");

    // Use HC-SR04 distance as a trigger to stop TDC7200 measurement
    // You can modify the distance threshold as needed
    if (duration / 29.1 >= 10.0) {
      // Stop the TDC7200 measurement
      digitalWrite(stoppin, HIGH);
      digitalWrite(stoppin, LOW);
      delayMicroseconds(100);
      digitalWrite(stoppin, HIGH);
      digitalWrite(stoppin, LOW);

      // Wait for the measurement to complete
      delay(8);

      // Read and process TDC7200 data as before
      // ...

      // Print the TDC7200 results
      // ...

      // Print the HC-SR04 distance
      Serial.print("HC-SR04 Distance: ");
      Serial.print(duration / 29.1); // Convert to cm
      Serial.println(" cm");
    }
  }
}
  • Hello Gaurav,

    Thanks for posting to the sensors forum!

    There are two items I noted as a possibility why you are not obtaining results:

    1. When looking at the HC-SR04 datasheet it indicated that the pins have a logic level of 5V. The TDC7200 uses 3.3V logic so you can provide a max of 3.9V on the digital pins. So there is a possibility you may have damaged these pins.
    2. I am looking over your python code and I didn't see any mention of the INTB pin. Are you checking the INTB status to see if the devices registers are ready to be read?
      1. If the device has not indicated that the registers are ready to be read the data may read 0's or have garbage values.

    Best,

    Isaac

  • I had previously measured TOF values to test the TDC by connecting it to the MSP430 Launchpad.

    I'm using an Arduino and have provided 3.3V and 5V separately to the TDC and HCSR04 respectively. As per your previous input, I've modified the code to include the interrupt and have tried writing an interrupt handler. I have verified the state of the INTB pin to be HIGH(1) using a separate code so the device does seem to be functional. Wiring does not seem to be the issue either as the HCSR04 gives values accurately but the TOF values show up as NaN. I have connected the INT1 pin from J2 of the TDC to pin 2 of the arduino given that is the external interrupt pin.

    The code I have used for the interrupt function incorporated into the main code is given below:

    #include <SPI.h>
    #include "tdc7200.h"
    #include <NewPing.h>

    // HC-SR04 configuration
    #define TRIGGER_PIN 8
    #define ECHO_PIN 9
    #define MAX_DISTANCE 200

    // TDC7200 configuration
    const int INTB_PIN = 2;
    int startpin = 4;
    int stoppin = 7;
    const byte CLOCKOUT = 11;
    byte config1, config2;
    tdc7200 chip1;

    NewPing hc_sr04(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

    // Flag to indicate that a measurement should start
    volatile bool measurementStarted = false;

    void setup() {
      pinMode(INTB_PIN, INPUT); // Set INTB_PIN as input for interrupt
      attachInterrupt(digitalPinToInterrupt(INTB_PIN), interruptHandler, FALLING);

      pinMode(CLOCKOUT, OUTPUT);
      TCCR1A = bit(COM1A0);
      TCCR1B = bit(WGM12) | bit(CS10);
      OCR1A = 7;

      SPI.begin();
      SPI.setBitOrder(MSBFIRST);
      SPI.setDataMode(SPI_MODE0);
      SPI.setClockDivider(SPI_CLOCK_DIV16);

      pinMode(3, OUTPUT);
      digitalWrite(3, LOW);
      pinMode(4, OUTPUT);
      digitalWrite(4, LOW);
      pinMode(7, OUTPUT);
      digitalWrite(7, LOW);

      digitalWrite(3, LOW);
      delay(5);
      digitalWrite(3, HIGH);
      delay(5);

      pinMode(INTB_PIN, INPUT_PULLUP); // Set INTB pin as input with pull-up resistor

      Serial.begin(115200);
    }

    void loop() {
      // Trigger HC-SR04
      long duration = hc_sr04.ping_median();

      chip1.writeReg(CONFIG1, B00000011);
      chip1.writeReg(CONFIG2, B01000100);

      while (digitalRead(TRIGGER_PIN)) {
        break;
      }

      digitalWrite(startpin, HIGH);
      digitalWrite(startpin, LOW);

      delayMicroseconds(100);

      for (int i = 0; i < 4; i++) {
        digitalWrite(stoppin, HIGH);
        digitalWrite(stoppin, LOW);
        delayMicroseconds(100);
      }
      delay(8);
      digitalWrite(stoppin, HIGH);
      digitalWrite(stoppin, LOW);

      // Trigger external interrupt to change the state of INTB_PIN
      triggerTDCMeasurement();

      chip1.time1 = chip1.readReg24(TIME1);
      chip1.time2 = chip1.readReg24(TIME2);
      chip1.time3 = chip1.readReg24(TIME3);
      chip1.time4 = chip1.readReg24(TIME4);
      chip1.time5 = chip1.readReg24(TIME5);
      chip1.time6 = chip1.readReg24(TIME6);
      chip1.clockcount1 = chip1.readReg24(CLOCK_COUNT1);
      chip1.clockcount2 = chip1.readReg24(CLOCK_COUNT2);
      chip1.clockcount3 = chip1.readReg24(CLOCK_COUNT3);
      chip1.clockcount4 = chip1.readReg24(CLOCK_COUNT4);
      chip1.clockcount5 = chip1.readReg24(CLOCK_COUNT5);
      chip1.cal1 = chip1.readReg24(CALIBRATION1);
      chip1.cal2 = chip1.readReg24(CALIBRATION2);

      chip1.tof1 = chip1.getTOF(chip1.time1, chip1.time2, chip1.clockcount1, chip1.cal1, chip1.cal2);
      chip1.tof2 = chip1.getTOF(chip1.time1, chip1.time3, chip1.clockcount2, chip1.cal1, chip1.cal2);
      chip1.tof3 = chip1.getTOF(chip1.time1, chip1.time4, chip1.clockcount3, chip1.cal1, chip1.cal2);
      chip1.tof4 = chip1.getTOF(chip1.time1, chip1.time5, chip1.clockcount4, chip1.cal1, chip1.cal2);
      chip1.tof5 = chip1.getTOF(chip1.time5, chip1.time6, chip1.clockcount5, chip1.cal1, chip1.cal2);

      Serial.println();
      Serial.print("  ****************");
      Serial.println();
      Serial.print("* TOF1 = ");
      Serial.print(chip1.tof1, 8);
      Serial.println(" *");
      Serial.print("* TOF2 = ");
      Serial.print(chip1.tof2, 8);
      Serial.println(" *");
      Serial.print("* TOF3 = ");
      Serial.print(chip1.tof3, 8);
      Serial.println(" *");
      Serial.print("* TOF4 = ");
      Serial.print(chip1.tof4, 8);
      Serial.println(" *");
      Serial.print("* TOF5 = ");
      Serial.print(chip1.tof5, 8);
        Serial.println(" *");
      Serial.print("  ****************");
      Serial.println();

      if (digitalRead(INTB_PIN)) {
        Serial.println("Interrupt");
      }

      if (duration != 0) {
        Serial.print("Distance: ");
        Serial.print(duration / 29.1); // Convert to cm
        Serial.println(" cm");

        // Use HC-SR04 distance as a trigger to stop TDC7200 measurement
        if (duration / 29.1 >= 10.0) {
          // Stop the TDC7200 measurement
          digitalWrite(stoppin, HIGH);
          digitalWrite(stoppin, LOW);
          delayMicroseconds(100);
          digitalWrite(stoppin, HIGH);
          digitalWrite(stoppin, LOW);

          // Trigger external interrupt to change the state of INTB_PIN
          triggerTDCMeasurement();

          // Wait for the measurement to complete
          delay(8);

          // Read and process TDC7200 data as before
          // ...

          // Print the TDC7200 results
          // ...

          // Print the HC-SR04 distance
          Serial.print("HC-SR04 Distance: ");
          Serial.print(duration / 29.1); // Convert to cm
          Serial.println(" cm");
        }
      }
    }

    // Function to trigger TDC7200 measurement using external interrupt
    void triggerTDCMeasurement() {
      // Set the measurementStarted flag to true
      measurementStarted = true;
    }

    // ISR for the external trigger signal
    void interruptHandler() {
      // Check if the measurementStarted flag is true
      if (measurementStarted) {
        // Change the state of the INTB_PIN
        digitalWrite(INTB_PIN, LOW); // Change this to LOW if needed
        delay(100); // Delay to maintain the state change
        digitalWrite(INTB_PIN, HIGH);  // Change this to HIGH if needed

        // Reset the measurementStarted flag
        measurementStarted = false;
      }
    }


  • Hello Gaurav,

    Thanks for the info. Regarding this comment:

    I'm using an Arduino and have provided 3.3V and 5V separately to the TDC and HCSR04 respectively

    I understand you provided each one with their own 3.3V and 5V supply. My understanding is that you have connected the HCSR04 TRIG directly to the TDC7200 START pin and the ECHO pin directly to the STOP pin of the TDC7200. If this is correct, you may be violating the Abs max spec of the TDC7200 since I believe the HCSR04 requires a 5V signal to trigger.

    My recommendation would be to test the TDC7200 with 3.3V signals to ensure the inputs are still operational as they have been damaged due to the 5V signal. If they are not operational  this might explain why they are not reading that an input was received.

    Best,

    Isaac