Problems getting the HC-SR04 sensor to work

A catchall for PSoC Mixed-Signal Array (microcontroller) discussions not captured by the other forums.

Moderator: ericb

Problems getting the HC-SR04 sensor to work

Postby BobPleaseHelp » Mon Mar 24, 2014 5:20 am

Hello i am having trouble getting my HC-SR04 sensor to work using the PsoC Designer 5.4. Instead of measuring distance on the LCD the program only seems to print out random numbers. I have hooked it up to an oscilloscope and the sensor seems to be gathering information. Could anyone please take a look at my code and give me pointers if he/she notices any glaring problems with it(Well, there's bound to be something if it doesn't work, right? :oops: ). The processor i use is CY8C27443-24PXI. I have a 16 bit counter and a PWM8 in use. I have probably spent close to 16 hours now banging my head on the wall trying to get this thing to work and getting a bit desperate :cry:

Source Code Below:
-----------------------------------------------------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------
// C main line
//----------------------------------------------------------------------------

#include <m8c.h> // part specific constants and macros
#include "PSoCAPI.h" // PSoC API definitions for all User Modules
#include <stdlib.h>

//65535
#define counterMax 60000
int counterValue, time, distance;

#pragma interrupt_handler echoISR
void echoISR(void){
Counter16_Stop();
counterValue =Counter16_wReadCounter();
time = counterMax - counterValue;
distance = counterValue/58;
Counter16_Start();
PWM8_Start();
}
#pragma interrupt_handler pwmISR
void pwmISR(void){
PWM8_Stop();
}

void main(void){
int i;
char theStr[7] = "0";
char theStr2[7] = "0";
Counter16_WritePeriod(counterMax);
Counter16_Start();
LCD_Start();
M8C_EnableGInt;
M8C_EnableIntMask(INT_MSK0, INT_MSK0_GPIO);
PWM8_Start();

while (1){
itoa(theStr, distance, 10);
LCD_Position(0,5); // Place LCD cursor at row 0, col 5.
LCD_PrString(theStr);
itoa(theStr2, counterValue, 10);
LCD_Position(1,5); // Place LCD cursor at row 0, col 5.
LCD_PrString(theStr2);

}
}

-------------------------------------------------------------------------------------------------------------------------------------------------------

I hope someone out there could help me with this, im new to doing projects on the PsoC and appreciate all the help i can get :mrgreen:
BobPleaseHelp
Newbie
Newbie
 
Posts: 2
Joined: Mon Mar 24, 2014 5:07 am

Re: Problems getting the HC-SR04 sensor to work

Postby cmagrin » Mon Mar 24, 2014 6:20 pm

Hello, I have the same problem with ultrasonic module (HC-SR04). I like modify my code for PIC16F877A, but i don't have success for PSOC.

//my c code - PSOC1
while (1)
{


Delay10msTimes(10);
TRIG_Switch(1);
Delay50uTimes(1);
TRIG_Switch(0);

while(!ECHOSONAR)
{
Timer8_1_EnableInt();
Timer8_1_Start();
}
while(ECHOSONAR)
{
Timer8_1_Stop();
tempo=Timer8_1_bReadTimer();
distancia-tempo*0.012;
}
LCD_Position(1,11);
LCD_PrHexByte(distancia);
}
cmagrin
Newbie
Newbie
 
Posts: 2
Joined: Mon Mar 24, 2014 5:51 pm

Re: Problems getting the HC-SR04 sensor to work

Postby bobmarlowe » Tue Mar 25, 2014 1:09 am

it is always advisable to upload the complete project here, there are a lot of settings besides your code that may go wrong. Additionally the code is difficult to read since you did not preserve the indentation.
To upload a project use Designer -> File -> Archive Project and then upload the resulting .zip-file here, I'll have a look at.

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1490
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problems getting the HC-SR04 sensor to work

Postby cmagrin » Tue Mar 25, 2014 4:40 am

Thanks for help, follows the zip file and HC-SR04 datasheet.
Attachments
Datasheet_HCSR04.pdf
Datasheet HC-SR04
(78.49 KiB) Downloaded 208 times
sonar2.Archive1.zip
Ultrasonic Project
(366.31 KiB) Downloaded 221 times
cmagrin
Newbie
Newbie
 
Posts: 2
Joined: Mon Mar 24, 2014 5:51 pm

Re: Problems getting the HC-SR04 sensor to work

Postby bobmarlowe » Tue Mar 25, 2014 5:50 am

First look at chip: You did not connect RI0[0] to your input-pin, so capture does not work.


Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1490
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problems getting the HC-SR04 sensor to work

Postby danadak » Tue Mar 25, 2014 2:13 pm

1) You are using 32 Khz. If that’s not from a xtal source its accuracy is
15 – 64 Khz, very poor if you want accurate distance measurements.
I would derive timer clock off VC3 chain.

2) Range is 2 cm to 4 M. At 2 cm pulse = 116 uS. At 4M pulse = 23.2 mS,
ratio is then 200, 8 bit timer OK. So a 100 uS clock makes most sense.

3) So configure VC1/2/3 to give you 10 Khz off the 24 Mhz clk, whose accuracy
is 23.4 to 24.6 Mhz, much more accurate.

4) Global properties, set cpu clk to SysClk/1.

5) You want the timer to start off the + going edge of Echo, so use Echo pin and enable its
Interrupt. Start timer in ISR after loading its period with 255. Do not enable Timer interrupt.
You do not need it.

6) You want to read the timer when Echo falls, so either poll echo, or add a buffer, routing Echo
to the buffer, and enable its Interrupt for falling edge. In ISR stop timer, read it, set a flag. In main()
do your computations.

Or use capture and config ISR for timer as capture. In this case in ISR read timer capture, and set
a flag for processing in main(). If you use capture you will need to place an inverting buffer and route pin
to buffer to timer so capture occurs on – edge of Echo. In this case you enable interrupt for timer, but do
not do that in evaluation loop, do it when you enter main(). Make sure you enable global interupts
after you enter main() as well. This will help with that – http://www.cypress.com/?rID=39496

To set up interrupts an additional step needs to be made, modifying boot.tpl in project root directory,
see this - http://www.cypress.com/?id=4&rID=36720

Lastly do not forget to test for timer overflow, given that sensor datasheet has no accuracy specs
shown for max distance tolerance. Also if you want better resolution use a 16 bit timer with a faster
clock, recompute the clock needed for the timer and in your calculations.

Regards, Dana.
Last edited by danadak on Tue Mar 25, 2014 2:27 pm, edited 3 times in total.
Field Application Engineer
KB1RHB Mostly listen :)
Semi Retired
User avatar
danadak
The Big Cheese
The Big Cheese
 
Posts: 2008
Joined: Thu Dec 27, 2007 8:42 am
Location: New Hampshire

Re: Problems getting the HC-SR04 sensor to work

Postby BobPleaseHelp » Tue Mar 25, 2014 2:18 pm

Hello! And sorry for not adding the project i did not come to think of it. Here is the project in it's entirety.
Attachments
ultrav0.01.rar
The project
(259.37 KiB) Downloaded 223 times
BobPleaseHelp
Newbie
Newbie
 
Posts: 2
Joined: Mon Mar 24, 2014 5:07 am


Return to “%s” PSoC1 General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest