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.

EK-TM4C1294XL: Interfacing HC-SRO4 ultrasound sensor with Tiva c [Stuck]

Part Number: EK-TM4C1294XL
Other Parts Discussed in Thread: ENERGIA

Hello,

I am new to the tiva c connected launch pads and I am stuck now

I am trying to test the HC-SRO4 with the TI EK-TM4C1294XL but I cant even get the distance out of it

I am using energia and this is the code which I am using

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Ultrasonic pins definition
const int echo = 9, Trig = 10;
long duration, cm;

/////////////////////////////////////////////////////////////////////////////////////////
//Time  update variables

unsigned long LastUpdateMicrosecs = 0;		
unsigned long LastUpdateMillisecs = 0;
unsigned long CurrentMicrosecs = 0;
unsigned long MicrosecsSinceLastUpdate = 0;
float SecondsSinceLastUpdate = 0;


void setup()
{
  
  //Init Serial port with 115200 buad rate
  Serial.begin(115200);  
  SetupUltrasonic();  
    
  
  
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Setup UltrasonicsSensor() function
void SetupUltrasonic()
{
 pinMode(Trig, OUTPUT);
 pinMode(echo, INPUT); 
  
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//MAIN LOOP



void loop()
{

    Update_Ultra_Sonic();
    delay(200);
 
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Will update ultrasonic sensors through serial port

void Update_Ultra_Sonic()
{
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  // The echo pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(echo, HIGH);
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
  //Sending through serial port
  Serial.print("Distance=");
  Serial.print("\t");
  Serial.print(cm);
  Serial.print("\n");
  
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Conversion of microsecond to centimeter
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

the circuit is simple I am connecting the sensor to a level shifter in high level 5V and then from the other side which is 3.3V to the launch pads I am pretty sure that the connections are OK but I dont know what I missed

  • Hi,
    Some questions:
    1. The MCU is a 3.3V device. Did you have the trigger output from the MCU first go through a level shifter to the 5V sensor?
    2. What frequency are you running the MCU at? Even though I'm not experienced with how the Energia implements the delayMicroseconds() but it must rely on the MCU's timebase or system clock. The best way to find out is if you are generating a 10us trigger. If you have scope, see if the trigger is 10us. If it is not 10us then it may have something to do with the time base.
    3. What do you see on echo input. What width do you see on echo input? Is it flat or there is something? Make sure it is a 3.3V input to the MCU.
    4. If you see correct width on the echo input then the next thing to investigate is on the pulsein() function.
  • I do appreciate your attention but actually I didnt get what your are saying can you rephrase it please

  • Not sure which part is not clear. Can you use a scope to capture the trigger and echo signals? Measure the trigger signal and see if the high pulse is 10uS. Can you see the echo signal returned from your sensor?