How to connect a HCSR04 ultrasonic sensor to a Industruino IND.I/O D21G?

Hello, I am a mechanical engineer with little experience with electronics. I have a 12V power source attached to the Industruino. I have tried quite a few methods to get the sensor to work but I can't seem to get it to return any inputs. I swtiched my code from Indio.digitalWriteMode to pinMode to try and work with the extension ports but that  failed. I also tried various pins (4,5,6,7,8) in both pinMode and digitalWrite. I have attached my full code below. Could someone point me in the correct direction on how to wire it properly and how to figure out what to put in for the digitalWriteMode? As in, what number do I put in for each channel?


#include <Indio.h>

#include <UC1701.h>


const int triggerPin = 1;

const int echoPin = 0;


int backlightIntensity = 5;        // LCD backlight intesity

static UC1701 lcd;


void setup() {

  Indio.analogWriteMode(1, V10_p);  // makes it so I can input any #1-10

  Indio.analogWrite(1, 50, true); //set to 5V


  pinMode(triggerPin, OUTPUT);

  pinMode(echoPin, INPUT);




  analogWrite(26, (map(backlightIntensity, 5, 1, 255, 0))); //convert backlight intesity from a value of 0-5 to a value of 0-255 for PWM.



void loop() {

  // Trigger the sensor

  Indio.digitalWrite(triggerPin, LOW);


  Indio.digitalWrite(triggerPin, HIGH);


  Indio.digitalWrite(triggerPin, LOW);





  // Measure the echo signal duration

  Indio.digitalMode(echoPin, INPUT);

  long duration = pulseIn(echoPin, HIGH);


  // Convert the duration into distance

  float distance = duration * 0.034 / 2; // Speed of sound is approximately 340 m/s


  // Print the distance

  SerialUSB.print("Distance: ");


  SerialUSB.println(" cm");




Jacob Rock
Jacob Rock
| 0 0 0
Asked on 7/3/23, 9:39 AM

Hi, use the expansion port, D5/PWM use as trigger, and use to echo D0 to D3, check the manual or schem.

Remember, this is based on D21G SAM MCU of atmel, and MKR zero is same in functions and work.


Carlos Flores
Carlos Flores
| 0 0 0
Answered on 8/10/23, 6:59 PM

Hi Jacob

The thing is, the I/Os that are controlled as regular Arduino I/Os on the D21G IndIO are on its expansion port (If I remember well, they begin with D9).

If the only sensor your want to connect to your D21G is an SR04 (no 4-20 mA or 0-10V for instance), you would probably be better off with the Proto version or D21G where all I/Os are Arduino-like programmable.

Hope this helps

Didier, DG2R

| 4 1 3
Answered on 7/3/23, 10:48 AM

Hi Didier, this does help. Since I already have this Industruino, it is possible to make it work with the one I have?

Jacob Rock
on 7/3/23, 11:23 AM

Hi Jacob, I would use D6/D7 or D4/D5 on the expansion port (see schematics in the "Products - D21G IndIO" section), just to keep the I2C an SPI ports free. So Triggerpin = 4, Echopin = 5, and connext GND to expansion port GND. Should work.

on 7/3/23, 12:24 PM

Hi Didier, unfortunately, this did not work. I keep receiving 0 as a response. I connected the VCC to the +5V on the expansion board, and did everything else. I am not sure why it is not working. Should I send the code I used?

Jacob Rock
on 7/5/23, 7:20 AM

I did verify that the sensor worked on an Arduino Mega, so I don't believe its a issue with the sensor.

Jacob Rock
on 7/5/23, 7:21 AM

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This community is for professionals and enthusiasts of our products and services.

Read Guidelines

Question tools

68 follower(s)


Asked: 7/3/23, 9:39 AM
Seen: 575 times
Last updated: 8/10/23, 6:59 PM