Help needed for HC-SR04 link up to PICAXE 14M2

murtagh_sean

New Member
Hello all,

I hope someone can give me some much needed advice on setting the HC-SR04 Range Finder with a PICAXE 14M2.

I have spent some time searching the net and also in this forum but cannot find the exact solutions I need thus I have started a thread hopefully to get some help. I would not be anywhere near the same level as many of you on the forum in terms of writing programming code but I will have a fair idea of what I'm told...so go easy please ;)

As part of a school project a pupil is looking to create a circuit that will detect how high he has jumped off the ground on his skateboard. We are in the process of building the circuit. The Input is a HC-SR04 Range Finder and I am open to suggestions for the Output but either LEDs/LCD/7 Seg would be fine.

I will need some help with the coding at a later stage but the first piece of information I would need is what pin method should I set it up as and what do I need to link to do so? I have read that the HC-SR04 is NOT the same as the SRF04 and SRF05.

The overall aim of the circuit is that the HC-SR04 will be mounted to the skateboard, when the pupil goes to make a jump it will detect the highest point he jumped and display this information for say 15 seconds so obviously I would need the HC-SR04 set up to pick up changes fairly quickly.



Thank you in advance for any help received,
Seán
 

hippy

Ex-Staff (retired)
Welcome to the forum.

The best way is arguably to use the HC-SR4 in two-pin mode at a fast enough operating speed for it to be able to be ready for the echo pulse after issuing the trigger pulse -

PULSOUT triggerpin, 1
PULSIN echopin, 1 , wordvar

Alternatively one-pin mode can be used; pin to Trigger, pin to Echo via 1K8 -

HIGH pin
PULSIN pin, 1 , wordvar

For more detail -

http://www.picaxeforum.co.uk/showthread.php?22639-HC-SR04
 

murtagh_sean

New Member
Thank you hippy for your quick response.

I looked over the link and to be honest bits of it lost me. I will go for the two-pin mode as I want the response to be ready for the echo pulse.

Would there be any sample code that you are aware of that is ready to go as a test that I could just ensure that I connect to the right input and output pins to match it for the 14M2?
 

eclectic

Moderator
Ask the student to bench-test this

Code:
#Picaxe14M2
setfreq M32 ; high speed operation

#terminal 38400 ; enable terminal

;see Manual 1, page28

symbol triggerpin = B.1 ; physical leg 12
symbol echopin    = B.2 ; physical leg 11

Symbol range = w0

Do

 PULSOUT triggerpin, 8
 
 PULSIN echopin, 1 , range

 
 ; now convert range to cm (divide by 5.8) or inches (divide by 14.8)
 ; as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
 let range = range * 10 / 58 ; multiply by 10 then divide by 58
 sertxd (#range," ")  ; display range via Sertxd command
 
 Loop
Tested using AXE091/ 14M2 v.6.a

e
 

murtagh_sean

New Member
Thank you eclectic for your help.

I am using PICAXE Programming Editor and have downloaded the code. My Trigger was connected to C.4 and the Echo was connected to C.3 so I changed it to pick up those pins. I am now getting like a humming sound from the HC-SR04. In the Simulation window of the 14M2 C.4 seems to be running fine but it doesn't seem as if the Echo is being picked up. In the Serial Output Buffer window it keeps displaying the following text:
'sertxd output - baud 4800,n,8,1
0

Is there something really obvious that I am doing wrong?

Seán
 

murtagh_sean

New Member
Ah yes re-downloaded it again and the same window has appeared as your image! Thank you eclectic!

In terms of what we are seeing in the box I notice in your picture there are a lot of 57s...does that mean 5.7cm or 57cm because surely it wouldn't be showing it as 57cm with just your hand being waved?

Last bit eclectic if I wanted to pass these readings onto a range of LEDs say every 20cm another LED lights up. So 20cm b.5 goes high, 40cm b.5 & b.4 goes high, 60com b.5 & b.4 & b.3 goes high, 80cm b.5 & b.4 & b.3 & b.2 goes high what would I need to add to the code?
 

eclectic

Moderator
1. I've modified the code to allow for the increased processor speed. (now cm)

2. I've added pauses to make the output more "human-readable".

3. I've added a suggestion for the display.

Pinouts may need modifying.

Code:
#Picaxe14M2

setfreq M32 ; high speed operation
#terminal 38400 ; enable terminal

;see Manual 1, page28

symbol triggerpin = B.1 ; physical leg 12
symbol echopin    = B.2 ; physical leg 11
Symbol range = w0

Do
 PULSOUT triggerpin, 8

 
 PULSIN echopin, 1 , range

 
 ; now convert range to cm (divide by 5.8 * 8 = 46.4) 

 ; as picaxe cannot use 46.4, multiply by 10 then divide by 464 instead
 
 let range = range * 10 / 464 

 sertxd (#range," ")  

 
 pause 200

 
 ; range light

 
If range >=20 then 
  high C.0
  else low C.0
  endif

 
 If range >=40 then 
  high C.1
  else low C.1
  endif

 
 ; and so on
 
 
 Loop
e
 

murtagh_sean

New Member
Thank you eclectic for all your help with this problem. I will download this tomorrow and I'm sure I will be able to modify your code to complete what the project is to do. Will let you know how it all goes!

Again a massive thank you,
Seán
 

beb101

Senior Member
I have an HC-SR04 hooked to a 08m2 with the following program(basically eclectic's code). When I first
ran the program, I saw some non-zero output, but now only zeros.

Code:
#Picaxe08m2
#com 9
#no_data
#terminal 38400 

symbol trigger  = C.4 ; leg 3
symbol echo     = C.2 ; leg 5
Symbol distance = w0
output trigger
input echo

setfreq m32
 do
	pulsout trigger, 8 ; >10 uSec @ 32mHz
	pulsin echo, 1 , distance
   	let distance = distance * 10 / 464 
 	sertxd (#distance," ")  
  	pause  1000

 Loop
My logic analyzer is hooked to the trigger and echo line and produces the
attached screen shots. I have tried doubling the trigger to 20uSec, but with the same
results. The duration of the echo channel is always 0.4128ms. I have tried the one pin
method with no success. My conclusion is that the sensor has failed. Before I toss it out,
a second opinion would be welcome.

Baxter
 

Attachments

hippy

Ex-Staff (retired)
What voltage is it operating at ?

The HC-SR04 can be particular about its operating voltage which should be 5V so may have problems if operated from a battery supply.

It would probably be best to mark the HC-SR04 as suspect but not throw it out yet. You can then re-test it if you get another that does work and see if they both behave the same.
 

SteveT

Senior Member
I've just been playing about with one of these. The code I used was lifted from a previous thread and is simply :-

Code:
' Test circuit for HC-SR04

Symbol Distance = w0
Symbol Trigger = C.2
Symbol Echo =C.1

Do

	setfreq m8
	pulsout trigger,2
	pulsin echo,1,distance
	pause 10
	let distance = distance *5/58
	sertxd ("Range = ",#distance, "Cm", cr, lf)
	pause 200
	
Loop
works a treat for me.

Edit: This is on a 08M2
 
Last edited:

eclectic

Moderator
@Baxter.
re hippy's post #12

I set up your circuit on an AXE091 board.
Used your program (except the #com9)

Regulated 5 volts = works fine.

3 x AA Alkaline (part-used) 4.4v
produced a string of "3 3 3 4 3 4 3"

Changed back to 5.0 v = works fine.

e
 

beb101

Senior Member
Thanks for trying the code and your suggestions . Both the HC-SR04 and the 08m2 have a clean 5.03V supply voltage.
After my post I connected it to a Netduino using an interrupt port to count ticks for the echo line and could not get any meaningful
results. So the one I have has most probably gone belly up.

DealExtreme is selling them for $2.80 in lots of 3. I will order 3 and hopefully will get one that is good.

Baxter
 

ThierryP

New Member
I got my HC-SR04 from ebay and with the help of this forum/thread got it working fairly easily.
Within the technological constraints of the device (hard surfaces, no 'false objects' in angle of view), results looked reproducible and stable, so I added some oversampling & display & higher resolution.
See attached program.
Code:
;Test program for HC-SR04 Ultrasonic range finder
;Averaged range over 4 samples
;Range values displayed on serial LCD/OLED display
;Display resolution can be set to 0.1, 0.2, 0.5 or the usual 1cm
;
;ThierryP, 2 Feb 2013
;  see also: 	http://elecfreaks.com/store/download/HC-SR04.pdf
;  			http://www.picaxeforum.co.uk/showthread.php?23057-Help-needed-for-HC-SR04-link-up-to-PICAXE-14M2/page2&highlight=HC-SR04

#picaxe20M2		;with proper pin reassignment this sould work on any Picaxe M2 
#no_data

setfreq M32			; M2 operating at 5V

symbol triggerpin = B.1 ; HC-SR04, operating at 5V
symbol echopin    = B.2 ; HC-SR04
symbol SerLCD = B.7	; Serial OLED/LCD AXE133 type 16x2
symbol baud = T4800_32	; for serial LCD module (AXE133-like) reprogrammed for 4800 bps and True serial levels
symbol UPuls= 10		; minimum is 10us, 10 gives 12.5us at 32Mhz
symbol BtwnPuls = 100	; wait between samples, at least 10ms required after end of last echo, 100 gives 12.5ms @32MHz 
;symbol MaxPuls  = 14400 ; max pulsin word value according to SR04 spec = 18000us/1.25us = 14400 pulsin-counts @32MHz
symbol MaxPuls  = 13107	; max pulsin not causing word overflow in cm conversion below *) 
				; ie =65535/5, this reduces max range somewhat from 310 to 282cm
symbol Range0 = w0 	; b0,b1	measured and calculated range value
symbol RngSum = w1 	; b2,b3	sum of Range0 values for averaging
symbol OORCnt = b4 	;		Out of Range Count
symbol LoopCnt= b5 	; for loop counter
symbol Resolution = b6	; use 1 for 1cm, 2 for 0.5cm, 5 for 0.2cm, or 10 for 0.1cm resolution 

Resolution = 2		
serout SerLCD, baud, (254,128,"                ")   ;clear LCD/OLED display
pause 500
serout SerLCD, baud, (254,192,"                ")   ;clear LCD/OLED display
pause 500

main:
	OORCnt=0
	RngSum=0
	for LoopCnt=1 to 4							;can't increase this with current word value limits: 4xMaxPuls must be <65536
		pulsOut triggerpin,UPuls : PulsIn echopin,1,Range0	;take a sample
		if Range0>MaxPuls Then						;check if Out of Range
			OORCnt=OORCnt+1
		else 
			RngSum=RngSum+Range0
		endif
		pause BtwnPuls
	next LoopCnt
	;debug
	if OORCnt>1 Then 
 		serout SerLCD, baud, (254,128) 		;start of first line	
 		serout SerLCD, baud, ("out of range    ") ; display OOR message on LCD
 	else
		OORCnt=4-OORCnt 					;calculate number of samples to average
		Range0=RngSum/OORCnt				;calculate average
		Range0=Range0*5/29				;convert to cm, see above MaxPuls *)
		Range0=Range0*Resolution/8			;apply resolution and compensate for Setfreq=M32
		Range0=Range0*10/Resolution			;apply resolution
		bintoascii Range0, b11,b12,b13,b14,b15	;prepare display values
		if b12=48 then					;suppress trailing zero's
			b12=32
			if b13=48 then
				b13=32
			endif
		endif
		serout SerLCD, baud, (254,128) 		;start of first line	
		if Resolution=1 Then
			serout SerLCD, baud, (b12,b13,b14," cm        ")  ; display result on LCD
		else
			serout SerLCD, baud, (b12,b13,b14,".",b15," cm        ")  ; display result on LCD
		endif
	endif
	pause BtwnPuls
	goto main
Thierry
 

germanwolflord

New Member
Hi
I am planning on connecting an HC-SR04 to a picaxe 14m2 and was wondering what the circuit diagram to do so was. could you possibly help by showing me the circuit that you used ?
Thanks Wolf.
 

germanwolflord

New Member
hi
what i acutaly meant was, if it were possible could i be given an image of the circuit diagram that was used as there is nothing i can find that shows how to link the HC-SR04 to the picaxe.
Thanks Wolf.
 

SteveT

Senior Member
hi
what i acutaly meant was, if it were possible could i be given an image of the circuit diagram that was used as there is nothing i can find that shows how to link the HC-SR04 to the picaxe.
Thanks Wolf.
If you went to the link I pointed you to, you would see......

symbol trigger = C.4 ; leg 3
symbol echo = C.2 ; leg 5

Which means the trigger lead goes to pin C.4 (physical leg 3) and the echo lead goes to pin C.2 (physical leg 5). I'm sure you can figure out where the other two leads go :rolleyes:
 

ams

Member
I also spent a lot of problems with the HC-SR04, after the solution was in the pullup on the pin that connects to Trig.
(operation with 3 pins, Trig connected to Echo with R=1K8)

#picaxe 18M2
#no_data
#terminal 4800
'I/O
let dirsB = %10111111 'PORTB
let dirsC = %10001000 'PORTC

'OSCILADOR
setfreq m4

'PULL-UPS 'PortB
pullup %01000000 'attention: B.6 with pullup

main:
pulsout B.6,1
pulsin B.6,1,w0

pause 50
sertxd(#w0,9,CR,LF)
goto main

ams
 
Top