Translation missing: en.general.accessibility.skip_to_content
Understanding a Basic Arduino Sketch

Understanding a Basic Arduino Sketch

Functions

There are some similarities in the code between all Arduino sketches.

In every Arduino sketch, you'll usually see a Void setup( ) and a Void loop( ) these are functions that ran everytime the board is powered on or reset. The setup function is run once and usually used to declare the libraries in use, initialise any variables and set the pin modes for any I/O pins the sketch uses.

 

The code inside the loop function will be looped through until power is removed from the board or the reset button is pressed. This is where you'll usually want to write all your code, it's ideal if you're constantly polling a sensor for data information or updating a display.

Comments in code

Commenting on code is extremely important, it's the easiest way to express your thinking and explain your code to anyone else that might view it. There are two ways to leave a comment in code, we'll explain them below.

Using this block comment allows you to quickly remove parts of code so they won't be ran in the program. It's handy for troubleshooting or running parts of code in isolation.

The other type is a single line comment which does exactly as you'd expect let you leave a comment on or at the end of a single line.

Variable Declarations

Variables are used for storing a piece of data, each variable has it's own name and data type. There is a wide range of different variable types which all have their specific uses. We're going to quickly detail how to declare a couple of the most popular ones. 

Integers & Floats: These are the best way to store number data. Integers are used for whole numbers and floats are designed for use with numbers that have decimal points with up to 7 digits of precision. 

 String: This data type is a sequence of characters. It can be used to store names and words but also things like dates and number even though they have their own data type.

 

 

There are many other parts of an Arduino Sketch but there are the ones we thought were most important for beginners! For a full list of them and details on what they do/how to use them see here. If you've just written your first program and got some error messages or your having some issues with your board then check out our Fixing Common Arduino Issues blog post! 

Previous article Comparing the BBC Micro:Bit V1 and V2, what is different?