Linux Format

Model and simulate your circuit projects

He’s no fake, but Mats Tage Axelsson shows you around the intricacie­s of simulating your Arduino-based projects for better, error-free results.

- Mats Tage Axelsson is keen to show you how using the Linux operating system can benefit your work and home life.

He’s no fake, but Mats Tage Axelsson shows you around the intricacie­s of simulating your Arduino-based projects for better, error-free results.

You may have considered creating a project using either the Arduino controller range of boards or any of its compatible systems. If you have, but were put off by having to buy new hardware just to try it out, then why not simulate your idea before splashing out the cash? All the software you need is on Linux and you can see what the different boards are capable of beforehand.

In this tutorial we’re going to learn what you can do with virtual boards and the right software. You can create all the components and connect them to your board, program the controller and see how it handles it. You have several CLI options including simavr, simulavr and a few others, too.

There are many different packages available for designing the circuit. The main software for writing the sketches (what the code is referred to as) is the Arduino IDE. A graphical front-end is Simulide, which also can simulate ordinary circuits. Adding components is straightfo­rward. You can measure voltages and display waves on an oscillosco­pe in the applicatio­n.

We’ll be looking at how to set up the software in this tutorial, step by step. To choose the correct board you’ll need to have a vision for what your project needs to do. You’ll use an Arduino type board for your projects, and there are many copies available. The Arduino platform is open source.

Start small

The Arduino Uno board is small and cheap. It makes sense to start learning with this model until you’ve become more experience­d. We recommend choosing that one first and move on to larger projects later.

The standard way to compile for the Arduino is to write a sketch in the Arduino IDE, which is a Java applicatio­n. It enables you to handle all the tasks necessary for getting your project to a board. However, you may want to do this using other solutions. You can also rely on the compiler extensions from that package. The extensions are for the gcc suite of programs. You can do this by installing arduino-mk, which depends on arduino-core. These packages are there for people who use the command line for everything.

For successful simulation you’ll need a few more packages. Here, too, you have options. A good one is Simulide, a package that’s easy to set up and start creating your design. Compiling and upload to the simulator involves just a couple of clicks. You’re also able to use the Arduino core package for compiling.

When you’re planning a new project, it’s good to test your ideas many times. You could either run cables and power your Arduino, or you can simulate your circuit. If you’re uncertain about which components to use, you can buy one of each, or stick to a simulation.

Some uses are also available for more scientific cases. Here, you can use Octave to visualise and analyse your designs. For the music lover there’s also a connection to Pure Data. In this tutorial, you’ll create an Arduino circuit that flashes a light, and try to compile and run a simulation with this circuit. You’ll then have the knowledge to choose the method that best suits your way of working and your requiremen­ts.

Board to bits

Once you have a board, you can program it using the Arduino IDE. But what can you do when you haven’t decided which board to use? You simulate the board! Either way, you need to start the programmin­g as well as the connection of other components. Running the wires works as with other simulators using SPICE.

For several of the tools, the Arduino core function compiles your sketches. The simplest is to choose a device and make a sketch that flashes the light on pin 13. Most, if not all Arduino boards have a built-in LED that correspond to pin 13. It’s the “Hello World” of Arduino boards. It’s also great for making sure that your simulator works before adding more components. More components leads to greater complexity. const int ledpin = 13; // Pin connected to LED void setup()

{ pinmode(ledpin, OUTPUT); // Set LED pin as output

} void loop()

{ digitalwri­te(ledpin, LOW); // Turn on LED

delay(2000); digitalwri­te(ledpin, HIGH); // Turn on LED delay(2000);

}

As you can see in the code, there are two standard parts: setup and loop. As you can probably guess, the setup establishe­s values for the machine. Here you can use pinmode to decide what pins should do what. You can also set pins that need to stay on all the time, such as the power light.

When you want to program using C, you need a few more tools. Or rather, you need to go about it another way. The Arduino official IDE uses the same tools behind the scenes. The big difference is that you write a C program and you need to know the libraries yourself. You also have control over all aspects of the code, although to what extent depends on your skill levels.

Gather your tools

The tools you need are gcc with the addition of avr-gcc for handling the processor itself. The avr-libc libraries have useful support for many functions. Finally, you’ll want the avr-dude package to transfer it to the device. Note that when you’re simulating you don’t need the last package because the emulators run the code in an adapted environmen­t. It can also help to install arduinomk. This is a Makefile created to help you program the Arduino from the command line.

The C code works the same way, although you can be more flexible. The simplest version will include the libraries you need, define any fixed values and then go to the main part. The program sets values for pins at the top and then starts a while loop where the code goes: #include

#include

#define BLINK_DELAY_MS 1000 int main (void)

{

/* set pin 5 of PORTB for output*/

DDRB |= _BV(DDB5); while(1) {

/* set pin 5 high to turn led on */

PORTB |= _BV(PORTB5); _DELAY_MS(BLINK_DELAY_MS); /* set pin 5 low to turn led off */ PORTB &= ~_BV(PORTB5); _DELAY_MS(BLINK_DELAY_MS); }

}

The values of _BV and how the _delay_ms works is in the source code. You can find the source of the avr – you should have that on your system. With this code, you need to compile the code to a hex file. If you call the file ledblink.c, your commands will be as follows:

$ avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o ledblink.o ledblink.c $ avr-gcc -mmcu=atmega328p ledblink.o -o ledblink $ avr-gcc -O ihex -R .eeprom ledblink ledblink.hex

You can then run the code with simulavr or simavr. However, if you like the Code::blocks.org IDE, you can do it all in that environmen­t. Included in that IDE is compiling, debugging and simulation, which makes it useful for carrying out initial work on your project.

If you’re happier with a graphical interface, Simulide

works well. You can write the sketch with any editor, but the Arduino IDE will create the standard base file for you. You can also use the IDE to handle libraries to support your coding. There are plugins for most editors and IDES.

Let’s start with the code for flashing the D13 on an Arduino board. Using the Simulide makes this a simple task. The interface is sparse, so you need to look around for the components. There’s a list on the left side, listing all the components you can use, including some of the Arduino boards. Pick one and place it on the middle panel. Save the file – it will have a ‘simu’ file extension.

Next, add the code in the right-hand column and save the file. Here are some of the weaknesses with

Simulide. It has no project function, so you need to keep track of your code by having it in one directory. You also have no way of checking your code for syntax and other mistakes. You need to use your regular editor or IDE for that, or use the Arduino IDE.

Once the file and the circuit is ready, you compile and load the code to the board. There are two buttons on the interface to do this. There’s also a console below the code, so you can see the compiling process. It will end with a message saying SUCCESS! Compilatio­n Ok . Next, send the file to the simulated board and turn it on with the red power button. You’ll now see the little light flash. It may not look very impressive, but you’ve just done your first simulation!

If you’re curious about what’s going on, move to the build directory that Simulide creates under .local/ share/simulide in your home directory. There you can see what files you need and what compiler the system uses. You can always do this by hand, if you prefer. You would probably do this for educationa­l purposes, or because you’re already coding something in C or even Python.

For more complex projects, you can also draw circuits in the Simulide applicatio­n. It has many components available in the build, adding a new component can be a bit tricky. The already available components comes with decent default values. The idea is that you pick a resistor and change the value when you use it. This is the common way to do it for resistors, but you can also tweak the other components.

Simple switching

The next step is to add a few components to the circuit and introduce the code to handle the state of a switch. All you need are two resistors, one LED and a switch.

Plus the Arduino of course. The schematic (see bottom

left)is very simple: you connect the LED negative through a resistor to ground. The positive side goes to pin 13 via a resistor. Add a resistor from ground to pin 2 and one side of the switch. The other side of the switch, you connect to 5V.

All these components are already included in Simulide, so you can create this quickly. The code goes in the right-hand column, as shown here: const int buttonpin = 2; // Pin connected to pushbutton const int ledpin = 13; // Pin connected to LED int buttonstat­e = 0; // Give pushbutton a value void setup()

{ pinmode(ledpin, OUTPUT); // Set LED pin as output pinmode(buttonpin, INPUT); // Set pushbutton pin as input

} void loop() { buttonstat­e = digitalrea­d(buttonpin); //

Read input from pin 2 if(buttonstat­e == HIGH)

{ // If pushbutton is pressed, set as HIGH digitalwri­te(ledpin, HIGH); // Turn on LED

} else

{ digitalwri­te(ledpin, LOW); // Otherwise, turn off LED }

}

As you can see, the code starts with setting names for each pin. In the setup section we set the button pin as input and the LED pin as output. In the loop we then check the state of the pin and set the output that way. Such a simple function can be set without the Arduino, but we can now set a value high with the input and start entire scripts or set many values with the one button.

Once you’ve created more complex programs in the Arduino, you may need to debug your code. The most obvious step to do is to follow the serial port communicat­ions. To open a console that shows this, just right-click and choose to open it up. The console appears at the bottom of the screen. You can also send in data using the console.

Add an oscillator

Simulide has both fixed voltage and wave generator sources. You can try this by adding an oscillosco­pe component and measure the result. If you have a project where the Arduino needs to handle wave functions and any similar things, then you have the necessary features at your fingertips.

To add more components, you can use sub-circuits.

The job of doing this requires a few files. These are subcircuit and package files and you need to edit the arduinos.xml file, too. You can use only boards where the designers have written the circuit in the program. If you don’t have the matching avr then you have to contribute C code to the project to simulate it. Of course, it’s simulated by external software. You need the AVR libc libraries for the boards. The whole tool chain contains avr-gcc, avr-binutils and avr-libc.

With that said, Simulide uses simavr to simulate the Arduino and gpsim for the PIC (see LXF266) circuits. If a device is missing, check for compatible binaries that support it. You can always add components with the GUI itself. That is only obvious for logic gates and amplifiers.

If you decide to embark on that journey, use the Microchips package repository and all their documentat­ion. The endeavour is worthwhile if you want to learn all the nuts and bolts of Arduinos main chip; the AVR of many models. If you just want to get a project running and build some neat gadget in the garage, we recommend staying with the existing tool and using hardware that’s already supported.

Expanding your toolset

There are other tools that are worth mentioning, although they’re not for simulating your hardware. To start with, you can collect data from an Arduino for example. You can use the octave-arduino package to both collect data and control the Arduino.

The pd-pduino package helps you with music-related issues. Programmin­g audio to the Arduino is a complex task. If you have this package and the matching Arduino firmware you can program it using Pd objects.

Once you have finished the simplest projects, you should check out esptool. This is useful for ESP8266 and ESP32 chips that are used for Wi-fi communicat­ion on a budget.

You may run into instances of the binutils-xtensalx10­6 libraries. These are there for developing code for the ESP8266 chips. Ordinary users probably won’t need them because they would code on top of the platform. It’s good to be aware of them, though.

When you start creating IOT applicatio­ns, you’ll run into the MQTT protocol. This is a protocol created specifical­ly for communicat­ing small amounts of data. It also aims to create very lightweigh­t software clients for the devices. On Linux, you’ll find the binary in your

distributi­ons repositori­es, usually called mosquitto.

Playing with Arduino to create small-scale useful gadgets is handy when you want to learn more about microcontr­ollers and electronic­s in general. Even though the hardware is cheap, it can be useful to create the circuit on the computer and simulate it before you get started. This way, you can also change existing projects without stopping the currently active project.

If your system becomes complicate­d, it’s also easier to use a simulator to rearrange hardware and software, instead of having to rebuild over and over again. With the packages available in Linux, you can program with any language you wish to use (almost!). The standard code is simple although potentiall­y limited, but there are also C-libraries, several Python libraries and other packages available.

Because the devices are small and built to be independen­t, they have gathered a lot of interest from the IOT industry. This means that you have many options, both in terms of hardware and software. The component stores are full of interestin­g shields and accessorie­s. There are many tools available that you can build your own project from – explore those options!

 ??  ??
 ??  ?? PICSIMLAB looks less sleek, but shows promise because it can do debugging and takes a modular approach to adding new boards. It also supports a range of chips.
PICSIMLAB looks less sleek, but shows promise because it can do debugging and takes a modular approach to adding new boards. It also supports a range of chips.
 ??  ?? The Simulide program enables you to wire up your project and run the code. All components show up and they behave exactly how they would on your real desk..
The Simulide program enables you to wire up your project and run the code. All components show up and they behave exactly how they would on your real desk..
 ??  ?? In most cases, need the Arduino IDE installed even when you don’t use it. One use of it is to manage libraries for shields and other accessorie­s.
In most cases, need the Arduino IDE installed even when you don’t use it. One use of it is to manage libraries for shields and other accessorie­s.
 ??  ?? With some effort, you can also use Netbeans for Arduino developmen­t. Use the Arduino plugin and set your libraries in a tool collection.
With some effort, you can also use Netbeans for Arduino developmen­t. Use the Arduino plugin and set your libraries in a tool collection.
 ??  ?? The Eclipse C/C++ IDE also has powerful functions for Arduino coding. It even includes debuggers and gives you the opportunit­y to use the native language. Direct communicat­ion is also available.
The Eclipse C/C++ IDE also has powerful functions for Arduino coding. It even includes debuggers and gives you the opportunit­y to use the native language. Direct communicat­ion is also available.

Newspapers in English

Newspapers from Australia