Image by SparkFun Electronics on Flickr
A new online tool that lets you play around with virtual circuits.
FOR THIS PROJECT YOU WILL NEED
- An Arduino Uno (or compatible)
- A computer with the Arduino IDE installed
- An LED
- A 330 Ohm resistor (Orange-Orange-Brown-Gold)
- A 10K Resistor (Brown-Black-Orange-Gold)
- Male to male jumper wires
- A push button
The Arduino board has been with us for many years and it has become the brand leader in microcontrollers. It may not be as powerful as a Raspberry Pi, in fact the two are very different. But for sheer simplicity the Arduino cannot be beaten, all we need to do is write some code, upload it to the board, and away we go.
But here lies the issue. Learning the Arduino language, a version of C, can be off putting to new users. So when Tinkercad released their Circuits project, we had to have a try.
Tinkercard Circuits is an online tool to create virtual circuits using common electronics and boards such as the Arduino. We can build a circuit using components, then write code for the board using a block editor. We can even simulate our code before we upload it to a real Arduino.
In this project we will learn
- How to create circuits using the Tinkercad Circuits editor
- Write Arduino code with blocks
- How to work with inputs
- Control output devices
- Use comments in our code
- Upload code to an Arduino.
To use Tinkercad, first we need to visit https://tinkercad.com and sign up for an account.
Once we have an account we then need to login and then from the dashboard click on Circuits.
Then we need to click on Create New Circuit to load a blank project and the first task that we have is to make a basic circuit. The Tinkercad Circuits layout is simple. But a little cluttered at times. The main area, a large grey portion of the screen, is where we build our circuit. We get components, such as Arduino, LEDs, buttons etc from the right hand side of the screen. We click on the component and then move the mouse to the centre. We then see the component appear under the pointer.
Let’s start by adding an Arduino Uno to the project. From the components list look for “Arduino Uno R3” and drag that to the centre.
Now let’s add a breadboard in the same way.
And now add a button and LED to the breadboard.
Next we shall connect GND (Ground) from the Arduino to the GND (-) rail of the breadboard using a male to male jumper wire. This means that our circuit now has a common GND connection for all of the components used. To connect components, click on where you would like to start, in this case the GND pin on the Arduino, then click on where you want it to go, the – rail. To shape the wire, double left click on the wire and a node (circle) will appear, you can now move the wire around to form paths around the circuit.
Now we connect the 5V output on the Arduino to the bottom right leg of our button. Again we click on the connections that we wish to make.
Our pushbutton needs a pull down resistor. This is a resistor that is connected to the GND (-) rail of the breadboard, and its job is to ensure that we have a clear distinction between a button press, and not. When the button is not pressed, we need to ensure that our Arduino see 0V, so by pulling the button pin down to GND, we get 0V. When the button is pressed, it connects the button pin to 5V and that triggers our code to work.
Next we connect a 330 Ohm resistor from the short leg (cathode) of the LED to the GND – rail of the breadboard. This means that our LED is connected to GND.
Our button is used as an input to trigger our code, and so we need to connect it to a pin on the Arduino. Pin 2 is connected to the top left leg of the button via a male to male jumper wire. This means that when we press the button, pin 2 receives the 5V signal to trigger the code.
Our LED need to be connected to pin 7 as we shall be controlling this in our code later. In the same way as for our button, connect pin 7 to the anode (long leg) of the LED.
That is all of the connections made in Tinkercad, now we can concentrate on writing the code to control it.
Coding in Tinkercad Circuits is very similar to Scratch. We use blocks to build up the code step by step. The code section can be found by clicking the Code button, found in the top right of the screen.
In the coding section, delete any existing code by dragging it to the trash bin. The first block of code will be to create a comment in our code. Comments are great! We can use them to help others understand our code, and to remind ourselves on how we tackled the project.
Our button can have two states, on or off. But we need a way to store the state so that we can later check it. A variable is a container for our button state. So using the Variables section we create a new variable called “buttonState” and then drag the block “set buttonState to 0” into the coding area.
To store if the button has been pressed, we need to read the pin that it is connected to. This is found in “input” and it is called “read digital pin”, drag this into the 0 of “set buttonState to 0” and change the pin to 2. Now we can read the status of the button!
As we have two states for the button, our LED can also have two states. On or off. To correctly turn the LED on / off depending on if the button has been pressed, we need to use a conditional statement. This is really a way to ask a question, and depending on the result perform an action. In programming this is expressed as If…Else. We can find this block in Control.
So what is the question? Well has the button been pressed? If so then the buttonState variable will store a 1, otherwise a 0. This means on and off. From Math we drag the “1 < 1” block and place it into the corresponding shape in our “if” conditional test. Change the < to a = so that we compare the left value with the right. Now go to Variables and drag the “buttonState” block and place it over the first “1”. So now the question asks “Is the buttonState equal to 1?” which means has it been pressed?
So what happens if the button is pressed? The code inside the “if” section is activated, and now we need to go to Output and find the block “set pin 0 to HIGH”, drag one into the “If” section, and another into the “else” section. The block inside the “if” section will be changed to “set pin 7 to HIGH” as we need the LED to be lit when the button is pressed.
For the other “set pin 0 to HIGH” we need to change this to “set pin 7 to LOW” as the LED should be off if the button is not pressed.
But what if we want the LED to stay lit for a while? Well we need to go into Control and use a “wait 1 secs” block. Drag this into the “if” section, so that it is under “set pin 7 to HIGH”. Change 1 to 10 seconds so that we can see the change.
With the code completed we can now start the simulator. At the top of the screen is “Start Simulation” click on this button and wait for a few seconds. Then click on the button on our breadboard. You should now see the LED light up for 10 seconds, proving that our code and wiring is correct!
Here is a close up to show the LED reacting to the button press.
Building the circuit for real!
Using the circuit diagram, the circuit we created in Tinkercad Circuits. We can now build a real version of the project using our components. Connect everything as per the diagram.
Copying the code to our real Arduino
Our blocks of code can be translated into something that a real Arduino can understand, and so to do this we need to download the code. Just above the area where we wrote our code is a button showing an arrow pointing down. Click on this to download the code to your Downloads folder. Make a note of the file name as we need to find it later!
Now we move away from Tinkercad, and open the Arduino IDE. Click on File >> Open and go to your Downloads folder to locate the file. Open the file and you will see the code that makes our project happen!
Plug in your Arduino, which is connected to the circuit for this project. Then go to Tools >> Boards and make sure that the correct Arduino version is selected. Then go to Tools >> Port and check that the Arduino IDE can see your board.
Then when ready, click on the arrow pointing to the right to upload the code. After a few seconds the Arduino will reboot and you can now press the button on the real breadboard to control the LED!
So what have we learnt?
- We learnt how to create a virtual circuit using Tinkercad Circuits, and how to connect components together to create a logical flow.
- We learnt how to write code for the Arduino using blocks, and how to comment our code to explain the purpose of it.
- We learnt how to use software to write a test to check if a physical, hardware, device has been used.
- We learnt how to control an output device, an LED.
- We learnt how to create variables and how to use them.
Arduino Electronic Circuits in Tinkercad Tips and Tricks
Pushbutton digital input
Pushbutton digital input
whack a mole game
arduino printable relays for kids
Coupons: relays for arduino
RGB LED Color Mixing with Arduino in TinkerCAD
Also In The June 2019 Issue
A Python coding challenge to create exciting labyrinths at the click of a button!
With this new online program, you don’t have to be a coding whiz to create fun mobile apps!
Spend those lazy summer days curled up with great stories about science & tech.
Learn the secret to creating huge programs without getting lost in thousands of lines of code.
Keep kids engaged throughout the summer by transforming school assignments into fun projects.
A wacky story about connectivity, Danish kings, and the need for good dentists.
A simple, fun Python tutorial that’ll get you drawing shapes all over the screen.
Dive into this exciting new language and learn how to manage computer memory without giving yourself a headache!
How Microsoft’s funny little assistant led to the AI we know and love today.
In the future, will computers be able to do everything? Even create an entire universe?
A new online tool that lets you play around with virtual circuits.
Follow this detailed step-by-step tutorial to create dazzling, colourful flowers out of geometric patterns.
The first time AI beat humans at their own game.
Download some paper templates and bring your Minecraft creations into the real world.
Battery history is a critical part of the history of technology. Without stored electricity, there would be no electronics.
Take a step away from the screen and let your imagination run wild with pen & paper.
Mix some math and some code to create dynamic games with realistic physics.
Links from the bottom of all the June 2019 articles, collected in one place for you to print, share, or bookmark.
Interesting stories about science and technology for June 2019.