Please select To the mobile version | Continue to access the desktop computer version

OrangePi

 Login            
 Register            
Search
Hot search: orangepi
View: 1082|Reply: 3

HC-SR04 ultrasonic sensor 2G-IOT

[Copy link]

2

Threads

4

Posts

26

Credits

Novice

Rank: 1

Credits
26
Post time 2018-6-1 15:24:57 | Show all posts |Read mode
Using an OPi 2G-IOT,running Ubuntu 2G-IOT GSM OS.
Struggling to get any form of reading on this sensor, I followed this : https://www.piprojects.xyz/ultrasonic-distance-sensor/
and changed the the Trigger pin to Pin 12, but when I run the program , all I get is a distance of 0 cm.
I used a 1Kohm and 2Kohm resistor as mentioned in the thread.

Has any one got this sensor to work with the 2G-IOT ?


10

Threads

220

Posts

2000

Credits

Gold member

Rank: 6Rank: 6

Credits
2000
Post time 2018-6-5 18:58:23 | Show all posts
  1. '''
  2. Pin#7:GPIO(56, "in/out")
  3. Pin#16:GPIO(101,"in/out")
  4. Pin#18:GPIO(121,"in/out")
  5. Pin#19:GPIO(4, "in/out")
  6. Pin#21:GPIO(3, "in/out")
  7. Pin#23:GPIO(2, "in/out")
  8. Pin#24:GPIO(5,"in/out")
  9. Pin#26:GPIO(6,"in/out")
  10. Pin#27:GPIO(1, "in/out")
  11. Pin#28:GPIO(0,"in/out")
  12. Pin#29:GPIO(122, "in/out")
  13. Pin#31:GPIO(123, "in/out")
  14. Pin#33:GPIO(124, "in/out")
  15. Pin#35:GPIO(125, "in/out")
  16. Pin#37:GPIO(126, "in/out")
  17. '''

  18. from periphery import GPIO
  19. import time
  20. import datetime


  21. def getCM():
  22.   #Send trig pulse
  23.   gpio_trig.write(True)
  24.   time.sleep(20/1000000.0)
  25.   gpio_trig.write(False)
  26. #  time.sleep(10/1000000.0)

  27.   #Wait for echo start
  28.   while(gpio_echo.read() == 0):
  29.     pass

  30.   #Wait for echo end
  31.   now = datetime.datetime.now()
  32.   startTime = now.microsecond
  33.   while(gpio_echo.read() == 1):
  34.     pass

  35.   now = datetime.datetime.now()
  36.   travelTime = now.microsecond- startTime;
  37.   distance = travelTime / 58;
  38.   return distance

  39. gpio_echo = GPIO(125, "in")
  40. gpio_trig = GPIO(126, "out")

  41. gpio_trig.write(False)
  42. time.sleep(0.03)

  43. while(1):

  44.   dist = getCM()
  45.   print dist
  46.   time.sleep(1.0)
Copy the Code


orangepi@OrangePi:~/test$ sudo python ./hc-sr04.py
12
14
16
18
19
21
21
23
23
23




2

Threads

4

Posts

26

Credits

Novice

Rank: 1

Credits
26
 Author| Post time 2018-6-5 21:52:24 | Show all posts
Hi nopnop2002, Thanks for your input.
I assume youv'e installed periphery python ? I currently have WiringOpi installed.
Do you think I should remove it. ?

10

Threads

220

Posts

2000

Credits

Gold member

Rank: 6Rank: 6

Credits
2000
Post time 2018-6-5 22:20:56 | Show all posts
nix666 replied at 2018-6-5 21:52
Hi nopnop2002, Thanks for your input.
I assume youv'e installed periphery python ? I currently have ...



No
You don't have to remove WiringOP.

You have to log in before you can reply Login | Register

Points Rules

Archiver|Mobile edition|Darkroom|OrangePi En ( 粤ICP备14086627号-2

2019-11-15 08:30 GMT+8 , Processed in 0.029196 second(s), 22 queries .

Powered by Discuz! X3.2

© 2014-2015 orangepibbs en.

Quick Reply To Top Return to the list