Snap4arduinio (hc-sr04, and other sensors)


Hi there,
For a project involving children, we’ll be working with several groups remotely.
We’ve opted for MQTT and Arduino to control servos and use various sensors (buttons, accelerometers, HC-SR04s, etc.).
Snap4Arduino seemed like a great option, but there aren’t many blog posts covering sensors.
How can I get new blocks or create my own? (I’ve tried, but the results weren’t very effective…)

If that isn’t possible, we’ll go with Snap! + MicroBlocks communicating via MQTT, but that means having two separate programs.

thx for your help

julien from fablabke


If your main objection is the use of two different programs, the project can be realized using only MicroBlocks. The MB+SNAP pairing is suggested for cases where a control panel type visualization is required, as well as cases where large data storage and interpretation is part if the requirements. SNAP excells in the areas and provides a great platform to realize the functionalities lacking in MB.

However, if only hardware control of components via wired or wireless methods, together with minimal visualization is the main objective, MB can provide the ultimate functionality and performance. The main caveat is that MB support is focused on 32bit microcontrollers. Thus Arduino may not be a good fit.

Check out the MB WIKI for various coding examples and communications techniques that can be implemented.

In case you aren’t familiar with MB, comms support is excellent, covering SERIAL, BLE, WIFI, ESPNOW, MQTT, UDP, WEBSOCKET; as well as various RF radio methods supported by microcontrollers.

IMHO: the biggest pro for developing your project in MB would be ease of switching microcontroller platforms later as needs dictate more advanced functionality and techniques. However, your initial selection of Arduino may be a limiting factor.

Thanks for your replies.

To address your points:

We want to create an activity using Snap! because it features a stage that makes it easy for children to create animations.

Here is an example of the basic project we want to set up:

Two groups of young people in different cities compete in a challenge (like *Mario Kart*, but in real life).

The challenge involves pressing a button as fast as possible.

Scores appear via Snap!, and a corresponding animation plays (for example, two cyclists move forward based on the score).

Two servo motors are also linked to the score, animating two physical cyclist cutouts (real-world movement).

Score data is exchanged between the two cities via MQTT.

All of this can be done using Snap4Arduino.

However, if we want to offer other "challenges" using different sensors, S4A isn't enough.

So, we considered:

either creating our own blocks (though we lack the necessary skills);

or connecting Snap! and MicroBlocks via MQTT to expand the possibilities (though, so far, we haven't managed to get MQTT working with MicroBlocks—we can't seem to receive data).

Another avenue (still to be explored) was found by my colleague, "Magic Fifi":

wiki.microblocks.fun/en/snap/microblocks_snap_messaging

I hope this helps clarify the project.

Snap4Arduino has been phased out in favor of the S4A extension in Snap!, which achieves the same without ever having to leave Snap!. If you need support for HC-04 sensors, you’ll have to use an extended Firmata. I believe @jguille2 can help you better than me with this.

OTOH, some of the documentation in the MicroBlocks wiki is slightly outdated. The best way to get Snap! and MicroBlocks to talk to each other is via the MicroBlocks extension for Snap! (available through the Libraries menu). The last blog post in the Snap! site explains how to use it: A little Snap! game with a physical controller - Snap! Forum