Tinkercad Circuits Code Blocks - Tiffany Tseng
Tinkercad Circuits Code Blocks - Tiffany Tseng
Ti any Tseng
Sep 26, 2017 · 6 min read
We* designed code blocks to make learning how to program with Arduino as easy as
possible. With that in mind, our main goals were to,
2. Focus on the desired interaction (what you want to sense and control), while
scaffolding all the necessary setup required to do so
3. Bridge the worlds of visual programming and text-based programming to make
everything created in Tinkercad Circuits extensible to real, physical hardware using
the Arduino IDE
Here, I’ll provide an in-depth explanation of how code blocks work while describing
how our design philosophy wove into the creation of this feature.
*We = The Tinkercad Circuits Team (Joshua Brooks, Lead Software Developer, &
Myself, Lead UX)
Control — Control structures like adding delays, repeating, and if else statements
These categories make it easier to see all of the basic functions you may want to use in
your program. Rather than having to look up a reference for all the functions available
for controlling a speaker, for example, the main options are visible in the Output
category. We also draw from all the benefits of visual programming, with blocks that
only snap together if they “fit” and construct working code.
When you drag out any programmable element into the Tinkercad Circuits editor and
click on the Code Editor button, you’ll automatically see code blocks side-by-side with
the text editor. Let’s dig into an example below for controlling a servo motor with a
potentiometer.
All Arduino programs have two main functions: a setup() and a loop(). When you drag
out any block into the scripting area, the code gets added to the loop() function, and
we dynamically create any lines of code necessary within the setup().
For example, when the first block to rotate the servo is added (rotate servo on pin 0 to 0
degrees), several things happen automatically:
This all happened just by adding one block to our code — pretty powerful!
Similarly, when the read analog pin A0 block is added (to control the position of the
servo using a potentiometer), check out how we add additional information to the
setup() function to specify the pinMode (pinMode(A0, INPUT)).
Because we generate the text-based code side-by-side with the code blocks (Goal #3),
you can always see how your blocks translate to real Arduino syntax. We believe this
has incredible value for learning how to program with Arduino because with every
new block you use, you see exactly how it affects the code. In the example above, we
effectively turned 3 blocks into ~16 lines of code. For other sensors, there is even
greater encapsulation. For example, if you drag out the read ultrasonic distance sensor
block, it carries with it ~24 lines of code!
The read ultrasonic distance sensor block translates to 24 lines of text-based code
The translation between visual blocks and text syntax also affords us some
opportunities to tweak the semantics of Arduino functions to make them easier to
understand. We wanted to map as closely to sentence-structure as possible, so rather
than create a block called, for example, Serial.read(), we created the block read from
serial.
Another example of this is with our wait block, which is the equivalent to an Arduino
delay. Delay is usually specified in milliseconds, which we felt may be difficult to think
in when you’re beginning to program. Instead, we default to a unit of seconds, but in
the process, we automatically add comments to the text editor that explain the
translation between 1 second and 1000 ms:
Ultimately, users can download the code as an Arduino sketch from the Circuits editor,
open it in the Arduino desktop IDE, and upload it directly to a device when they’re
ready.
Try it Out!
To get started with code blocks, head over to your dashboard (tinkercad.com when you
are logged in), click on Circuits, and click the Create Circuit button. When you have
the Circuits editor open, drag out an Arduino and click the Code Editor button. There,
you’ll automatically see the code blocks and default code to turn the built-in LED on
and off — click the Start Simulation button to test it out!
Getting to Code Blocks in Tinkercad
Many of our starters also include code blocks. To test out these pre-made circuits, click
on the Components Button, select the Starters tab, and choose something under the
Arduino section to start tinkering (I recommend the Servo starter!).
If you’re looking for some ideas for projects to build, here are some additional
examples to get you well on your way to coding with code blocks:
Upcoming Features
Our preview release includes blocks for playing speakers, controlling servos, reading
ultrasonic distance sensors, and more. We’ll be adding even more blocks in the near
future.
We’re also planning on doing a bit of an editor refresh to optimize screen real-estate, as
shown below:
Any questions or suggestions? Please leave a comment below!
Acknowledgments
Thanks to the Scratch Team, especially Andrew Sliwinski and Paul Kaplan, for helping
us get started with Scratch Blocks, and thanks to the larger Tinkercad team for their
feedback and bug bashing!