Introduction: 101 ON ANALOGUE AND DIGITAL CONVERSION, AT LAST (PRACTICAL AND EASY)

MIXING DIGITAL AND ANALOGUE IN MICROCONTROLLERS

TUTORIAL- EXPLORING THE CAPABILITIES OF TINKERCAD


SCOPE AND GOALS OF THE TUTORIAL

This tutorial seeks to motivate people that are beginning to learn about electronic circuits design and about using microcontrollers (MCU). It seeks, as well, to show the interesting possibilities that represent the use of tools for design and simulation (software) like TINKERCAD. This kind of tool eases the design of electronic circuits, helps to test them and validates their performance before the assembly. However, we want to point out that this tutorial is not an exhaustive exposition about this subject. As long as it is possible, and seeking to keep this tutorial light and easy to be understood, we avoid deepening the mathematic concepts of the process. Instead, we try to use simple concepts that are easy to apply.

TO WHOM THIS TUTORIAL IS ADDRESSED

This tutorial is intended for beginners in designing electronic circuits, and for people that have some very basic notions or a minimum of experience in this field and want to improve their skills.

Step 1: ​INTRODUCTION

As you probably already know, microcontrollers (MCU) are, basically, programmable devices that are a part of the digital realm. They receive data (by their input-port) or send data (by their output-port) in a format made up of strings of 1s (ones) or 0s (zeroes). Elemental microcontrollers have I/O ports (input-ports and output-ports) and modules for serial communication. More advanced and performant microcontrollers have PWM (Pulse-Width-Modulation) modules, analogue to digital converters (ADC) and, in some cases, they include digital to analogue converters (DAC), and other type of modules. We can make interesting things when we work together, in a MCU, features from the digital realm and features from the analogue realm. However, for doing this we need to know how to control the corresponding modules of a MCU. This is our goal and this is what we are going to work along this tutorial.

Step 2: ​LET US START DESIGN

Designing and building an electronic circuit can be something simple; however, it could become complicated easily. Fortunately, there exist simulation tools that can help us test each part of a design before building it and know if it work or not. These tools allow us to make the corrections as soon as problems are detected. Identifying the problems quickly save us time and money; this is the real utility of these simulation tools. Let us see what we can make by using the MCU that the Arduino UNO has, and let us use the tools of TINKERCAD as well. As a first part of this exercise, let us suppose we want to read an analogue signal and send its values through a communication port. As a second part of this exercise, let us suppose that we want to generate a periodic analogue signal, a task that a simple MCU, like the one of the Arduino UNO, is not able to do owing to the lack of this kind of modules in the embedded circuits. For the first part of this exercise, it is enough to know that the signal to be read has to have a voltage value between the reference voltage (GND) and the power supply voltage (VCC), i.e. between 0 and 5 Vdc(max). Other details in relation to the maximum frequency, the band width and other factors that normally are considered when we want to sample signals are out of the scope of this tutorial. In this respect, it is enough to know that the maximum frequency component of the signal to be sampled has to be 38.45 kHz (in low resolution – 8 bits) and 7.5 kHz (in full resolution – 10 bits). In relation to the second part of this exercise, we need to make a brief review of the theory that will help us to understand how to solve the lack of the DAC module in the MCU. Normally, one of the solutions that are proposed to provide a digital system with analogue signal generation capabilities consists in adding an external ADC integrated circuit (IC) that generates the analogue signal and that communicates with the MCU by one of its serial or parallel ports. This is an acceptable solution if we can incur in the extra costs of including a dedicated IC in the design of the circuit. See Fig 1.

https://cdn.instructables.com/FND/8L37/JNUL7PX7/FN...

Fig 1 External IC in an analogue to digital conversion

Another economical option is to connect a R-2R ladder network circuit [1] to a parallel output-port using several resistors and an operational amplifier (OpAmp). See Fig 2a. In this respect, the only advantage is the reduction of costs. However, the number of pins of the MCU needed for doing this is an inconvenience. The very few, very needed and well appreciated available pins in the MCU would quickly start to become scarce due to the fact that for each bit of resolution wanted, an output-pin in the MCU must be used. In this type of circuits, it is crucial that the value of the resistors have very high precision to produce accurate analogue conversion. See Fig 2b.

https://cdn.instructables.com/FHA/54N3/JNUL7PX8/FH...

We can consider a third option, more economical and simpler. We are going to use the Pulse-Width-Modulation (PWM) output-pin, a module that the MCU of the Arduino UNO has. Some of the readers may already have used this feature to control the speed of a DC motor or the signal line of a RC-servomotor. A PWM is a periodic signal -it repeats at regular intervals of time. Every period has a part of the time set at a High/On state (high value) and another part set a Low/Off state (low value). The relationship between the time at the High state and the total time that a period lasts is called Duty Cycle. By variating the time that the High/On state lasts, we can variate the Duty Cycle. See Fig 3. Total time = time High + time Low Duty Cycle = (time High / Total time) x 100 % A benefit of using a PWM is that they usually have between 8 and 16 bits of resolution, which gives us the opportunity of generating signal with good resolution. This is much better than lots of DAC embedded in the modules of more powerful MCUs. One downside of implementing this option is that the maximum frequency of the signal that can be generated is lower. If we consider that the percentage that the Duty Cycle represents a value from 0% up to 100%. Then, this value corresponds to the percentage value of the analogue signal. See Fig 4.

https://cdn.instructables.com/FHN/OZGQ/JNUL7PYD/FH...

Therefore, we could create analogue signals by variating the Duty Cycle values in the PWM at regular intervals of time. In other words, from a pin of PWM module we can generate an analogue signal. We just have to filter the high frequency components of the PWM signal that we are not interested in. To get rid of the high frequencies we can make a very basic low-pass filter with a resistor and a capacitor. The output of the filter can be connected to a voltage-follower circuit made up of an OpAmp. This configuration provides the circuit with lower output impedance and increases the capacity of the circuit to deliver more current at the output. See Fig 5.

https://cdn.instructables.com/FAP/5WH7/JNUL7PZJ/FA...

https://cdn.instructables.com/FLG/JNNN/JNUL7PVY/FL...

Step 3: ​APPLICATION

Now, let us see what we have to do with respect to the software for dealing with both parts of the exercise. The first part of the exercise is simple; we just have to read the value of the analogue signal at input of the analogue pin and place the data in the serial port, and that is all. The second part is a bit more elaborate. For generating a periodic signal, we need a mathematical function that allows us to calculate values at regular intervals. These values will be the values of the Duty Cycle at the PWM output pin. Alternatively, we can create a list of values -a look-up table- with values that have been previously calculated and read them sequentially and set the corresponding data at regular intervals in the output pin of the PWM. Then, the process of reading the list of values and setting these data in the Output PWM pin is repeated continuously. This is faster to do, but consumes more memory. A caveat is that we have to set the data of the look-up table in the flash memory. Putting the data of the list in the RAM memory could occupy a lot of space, and we could find ourselves without having enough space in this memory for the application. Another simple solution is to implement the software in a way that the values of the signals that are read at the analogue input pin can be sent through the serial port and set the Duty Cycle in the PWM. In this way, we can easily sample an analogue signals and generate an analogue signal in the MCU without the complexities of using either a look-up table or a mathematical function approaches.

Step 4: HOW WE CAN USE TINKERCAD

Before handling a protoboard and an Arduino UNO, let us make a circuit using TINKERCAD that allow us to test if the circuit works as we think it will. We will use TINKERCAD focusing on simulation and on how to do this simulation. We will not engage in the details of how to make circuits using TINKERCAD. They have a site that provides the tutorials for such a task. We connect the Arduino UNO to a 5Vdc power supply. At the input of the analogue pin, we connect a signal generator with an output between 0Vdc and 5Vdc. Take note that signals with negative voltage or with more than 5Vdc should not be connected at the input of the analogue pin. We connect this pin to an oscilloscope, so that we can see the input analogue signal. At the output of the PWM pin, we connect a resistor and a capacitor in a low-pass filter configuration [2]. At the output of this configuration, we connect the OpAmp that works as voltage-follower. The OpAmp (LM741) is connected to a dual power supply (+5Vdc, -5Vdc). To see the output analogue signal, we can connect an oscilloscope to the output of the voltage-follower. See video at the end.

https://www.tinkercad.com/things/3N79Zo1wKPA

Step 5: LET US RECAPITULATE

With this exercise, we have touched several topics:

  • There exist several approaches to make a conversion from/to analogue to digital.
  • Signals of the analogue realm can be converted into the digital realm, and be processed in this digital realm and converted into an analogue signal again.
  • Simulation tools ease electronic circuit designs and improve performance and reliability of them. For instance, in this exercise, TINKERCAD probed to be very usefull in these aspects.

Step 6: Notes

[1] https://en.wikipedia.org/wiki/Resistor_ladder

[2] A low-pass filter has a cut-off frequency, a maximum limit frequency, up to where any higher frequency component in a filtered signal is attenuated. The formula below is used for calculating the value of the components of the filter and the cut-off frequency.

Fc = 1 / (2 x π x RC)

Where:

Fc= value of frequency (Hz)

R= value of resistor (Ω)

C= value of capacitor (F)

We use the formula to calculate the values of the resistor and for Fc = 300 Hz, R ≅ 5100 Ω, C = 0.1 µF

Electronics Tips & Tricks Challenge

Participated in the
Electronics Tips & Tricks Challenge