Copy the sample code to a folder on the Raspberry Pi.
Open terminal, and then go to the folder you copied the sample.
Run the sample with the following command:
python3 Tachometer.py
Sample Code
Tachometer.py
#!/usr/bin/env python3#This sample demonstrates reading vehicle speed using tachometer inputs.#Install RC Driver HAT library with "pip3 install turta-rcdriverhat"from time import sleepfrom turta_rcdriverhat import Turta_RCDriver#Initializerc = Turta_RCDriver.RCDriver()try:#Activate fan at 50% speed rc.set_fan(50)whileTrue:#Option one: Read both tach data in one shot#Useful when you need all the readings within a minimal time tach_all = rc.read_tachs()#Print the readingsif tach_all isnotNone:#Check if data is receivedprint("Tach L....: "+str(tach_all[0]) +" rpm")print("Tach R....: "+str(tach_all[1]) +" rpm")#Waitsleep(1.0)#Option two: Read tachometer data one by one#Useful when you need only one reading tach_r = rc.read_tach(Turta_RCDriver.TACH_IN.RIGHT)#Print the readingif tach_r isnotNone:#Check if data is receivedprint("Tach R....: "+str(tach_r) +" rpm")#Waitsleep(1.0)#Option three: Calculate speed as KMH#Parameters: Ticks per revolution, Diamater in cm, MPH Output (Empty for KM/H) speed_all = rc.read_tachs(1, 10)#Print the readingsif speed_all isnotNone:#Check if data is receivedprint("Speed L...: "+str(speed_all[0]) +" km/h")print("Speed R...: "+str(speed_all[1]) +" km/h")#Waitsleep(1.0)#Option four: Calculate speed as MPH#Parameters: Ticks per revolution, Diamater in cm, MPH Output (Empty for KM/H) speed_all = rc.read_tachs(1, 10, True)#Print the readingsif speed_all isnotNone:#Check if data is receivedprint("Speed L...: "+str(speed_all[0]) +" mph")print("Speed R...: "+str(speed_all[1]) +" mph")#Waitsleep(1.0)#Exit on CTRL+CexceptKeyboardInterrupt:print('Bye.')
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: