Can you write once run everywhere using the embedded-hal?

Hey guys I have some questions regarding the embedded-hal crate.

I believe there are a lot of crates that uses the embedded-hal crate. So if I wrote code for the Arduino, would it be the same for the Raspberry Pi? For example if I wanted to make an LED flash or spin a motor etc?

Also if I writing code for one of teh Arduino models and I moved to another model of an Arduino, would the exact same code work as well?

1 Like

The idea is you can use the embedded hal to pull the logic behind blinking a LED (e.g. turn pin 5 on, wait a bit, turn it off again) into its own reusable crate, then the end user will give the crate some device-specific objects that implement the timer and GPIO pin traits.

When moving code between Arduinos it might work the same if they've both got the same CPUs with the same registers (e.g. GPIO A might be at address 0x1234_0000 on both). You might also need to switch to a different "peripheral access crate" (the crate providing those device-specific objects) if the two Arduino models differ.

It's the same when going from Arduino to Raspberry Pi - your core logic can all stay the same, but you will probably need to pass in different peripherals and set things up differently.

4 Likes