We get requests every now and then to ask if tinyLiDAR will work on their particular computing platform. Although tinyLiDAR was designed as a simple to use LiDAR sensor for the Arduino UNO, there is nothing stopping it from being used on other platforms like the Raspberry Pi (as shown in the previous instructable here). That is, if the platform has an I2C bus and can support the clock stretching feature of the I2C spec. So - what if your board doesn't even support I2C? - never mind the clock stretching stuff...
Well that would be a challenging scenario but in reality it actually does exist for the ultra popular visual programming language called "Scratch".
Google it if you haven't heard of it before but in a nutshell, it's a great first language for anyone to get their mind into the programming realm. Scratch was created by the MIT Media Lab and has been around for over 16 years now. It's the goto language for teaching kids to code around the world. Anyone can start using it for free - as it normally runs on your desktop in a web browser. Check it out here if you like.
YES! With a new feature called "Ultrasonic Emulation Mode" in tinyLiDAR f/w version 1.3.9
Step 1: Scratch What?
There are many flavors of Scratch in the wild now. Robotics enthusiasts tend to use the GPIO focused versions like ScratchGPIO or other modded versions like ScratchX which can be made to support any 'experimental hardware'. All of these are great for advanced users but the the mainstream versions which are installed by default on the pi will be our focus for this instructable as they have rather limited hardware options.
The pi's Raspbian Stretch Desktop comes with two versions of Scratch pre-installed. Namely, "Scratch" and "Scratch 2". We will be using the first one aka "Scratch 1.4 (NuScratch)" and will be using it "offline" so that we can use the GPIO server feature.
You can download the official pi desktop image here.
For whatever reason, the Scratch creators decided to support only a few of the most commonly available sensors from large companies such as Lego etc. Interestingly though, they also decided to add support for the HC-SR04. This is, of course, the ubiquitous ultrasonic distance sensor that simply outputs a single pulse width that's proportional to the measured distance.
The measurement accuracy can vary a bit depending on air temperature, humidity and target material as mentioned here, here and here. But in general, just about any platform can measure the pulse width output of this device.
Step 2: New Feature
Outputting accurate microsecond-scale pulses is not a problem for us on tinyLiDAR as we have spare high resolution hardware timers inside of the on-board 32bit micro. tinyLiDAR also always auto calibrates for temperature as it powers up so no further adjustments are necessary for the operating environment.
Let's Do it!
Okay - we could so we did just add a new feature to tinyLiDAR (as of firmware 1.3.9) called the "Ultrasonic Emulation Mode". You can access it by using the "u" command from the updated tinyLiDAR GUI Terminal.
Using it will change the settings in non-volatile memory so it will make tinyLiDAR look just like a generic ultrasonic sensor even after you unpower it. You can change it back to normal I2C mode by pressing the reset button and issuing the "az" command. Further details are in the user manual.
To make life even simpler, we are making the tinyLiDAR sensor available preset to this new Ultrasonic Emulation Mode from our website. Just order the "-u" version.
Look Ma, No Soldering
No soldering and also no breadboard is needed as the included "Grove to Female 4pin" cables will plug directly into the Raspberry pi header pins. The trigger pin is the Yellow wire and the echo pin is the White wire. Black and Red are for power of course. Refer to the main picture above for details.
Btw, we went one step further and made the Yellow pin behave like the PING))) sensor which uses a single wire for both the trigger and echo signals.
Because of this, you can now take measurements with tinyLiDAR using the default "PING" ultrasonic sketch that ships with every Arduino IDE without any code changes! You can try it without the delay as well.
Of course, you can set parameters like high accuracy, long distance etc of your LiDAR measurements before selecting the "u" command and it will then take those measurements every time it sees the trigger pin drop as shown in the diagram above.
Danger, Will Robinson!
Note the SR04 ultrasonic sensor needs some resistors to prevent the +5v supply from damaging your pi. But since tinyLiDAR runs natively from +3.3v, there's no need for any resistors to interface with the pi :)
Step 3: Coding It
So then, what exactly is the code we need for tinyLiDAR to work in Scratch?
Glad you asked!
It's only a matter of dragging a few simple broadcast blocks as shown in the pictures above.
To enable the GPIO pins we can issue the "broadcast gpioserveron"
Then to configure the trigger pin we issue the "broadcast config16out"
Next we can configure the echo pin by "broadcast config26in" and then start the measurements by "broadcast ultrasonictrigger16echo26". This will cause measurements to be taken continuously at about a 140ms cadence.
You can read the measured data by using the sense block "ultrasonic distance sensor value".
Well that's it for now, thanks for reading and be sure to check out the fun little Scratch demo program (shared here) we made called "tinyLiDAR_catch_me" and ...Scratch On! ;)