Tachometer Sample
This sample demonstrates reading vehicle speed using tachometer inputs.

To run the sample:

    2.
    Copy the sample code to a folder on the Raspberry Pi.
    3.
    Open terminal, and then go to the folder you copied the sample.
    4.
    Run the sample with the following command:
1
python3 Tachometer.py
Copied!

Sample Code

Tachometer.py
1
#!/usr/bin/env python3
2
3
#This sample demonstrates reading vehicle speed using tachometer inputs.
4
#Install RC Driver HAT library with "pip3 install turta-rcdriverhat"
5
6
from time import sleep
7
from turta_rcdriverhat import Turta_RCDriver
8
9
#Initialize
10
rc = Turta_RCDriver.RCDriver()
11
12
try:
13
#Activate fan at 50% speed
14
rc.set_fan(50)
15
16
while True:
17
#Option one: Read both tach data in one shot
18
#Useful when you need all the readings within a minimal time
19
tach_all = rc.read_tachs()
20
21
#Print the readings
22
if tach_all is not None: #Check if data is received
23
print("Tach L....: " + str(tach_all[0]) + " rpm")
24
print("Tach R....: " + str(tach_all[1]) + " rpm")
25
26
#Wait
27
sleep(1.0)
28
29
#Option two: Read tachometer data one by one
30
#Useful when you need only one reading
31
tach_r = rc.read_tach(Turta_RCDriver.TACH_IN.RIGHT)
32
33
#Print the reading
34
if tach_r is not None: #Check if data is received
35
print("Tach R....: " + str(tach_r) + " rpm")
36
37
#Wait
38
sleep(1.0)
39
40
#Option three: Calculate speed as KMH
41
#Parameters: Ticks per revolution, Diamater in cm, MPH Output (Empty for KM/H)
42
speed_all = rc.read_tachs(1, 10)
43
44
#Print the readings
45
if speed_all is not None: #Check if data is received
46
print("Speed L...: " + str(speed_all[0]) + " km/h")
47
print("Speed R...: " + str(speed_all[1]) + " km/h")
48
49
#Wait
50
sleep(1.0)
51
52
#Option four: Calculate speed as MPH
53
#Parameters: Ticks per revolution, Diamater in cm, MPH Output (Empty for KM/H)
54
speed_all = rc.read_tachs(1, 10, True)
55
56
#Print the readings
57
if speed_all is not None: #Check if data is received
58
print("Speed L...: " + str(speed_all[0]) + " mph")
59
print("Speed R...: " + str(speed_all[1]) + " mph")
60
61
#Wait
62
sleep(1.0)
63
64
#Exit on CTRL+C
65
except KeyboardInterrupt:
66
print('Bye.')
Copied!

Result

When you run the sample, it reads RPM from the microcontroller. If there is valid data, it calculates speed accordingly. The example output should be like this:
1
Tach L....: 0 rpm
2
Tach R....: 0 rpm
3
Tach R....: 0 rpm
4
Speed L...: 0 km/h
5
Speed R...: 0 km/h
6
Speed L...: 0 mph
7
Speed R...: 0 mph
Copied!
To stop the Python script, just press CTRL + C.
Last modified 2yr ago
Copy link