Arduino Compilation
Arduino Compilation
ARDUINO COMPILATION
ALL ARTICLES FROM ELEKTOR ● 233 PAGES
N ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ●
ARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ●
EARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ●
GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ●
HARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ●
Arduino Software
Development
with Atmel Studio
By Wolfram Pioch
(Germany)
Debugging applications on the Arduino Due
3
46 | December 2014 | www.elektor-magazine.com
Arduino compatibility
The procedure described here assumes you have
Atmel Studio 6.2 (or later) installed under Win-
dows. To make the Atmel IDE compatible with
Figure 3.
Arduino, you also have to install a free add-in
The Micro Explorer window
called Visual Micro [2], which is an Arduino IDE
of Visual Micro.
for Microsoft’s Visual Studio and Atmel Studio.
Visual Micro is directly available in Atmel Studio tion Explorer window in Atmel Studio. There the
after it has been installed. Arduino references are shown as links on the
Reference tab. A click on one of the entries takes
The USB-based Arduino debugger available from you to the corresponding website in the config-
[2], which is not free, is not necessary because ured browser. The Examples tab holds a list of
debugging with the Atmel ICE is significantly more examples (including library examples) from the
convenient. The latter tool is available in vari- Arduino IDE, grouped by topic.
ous webstores at prices in the 100 dollar range
(see the review in the October 2014 issue of Sketches in Atmel Studio
Elektor [3]). The expenditure is worthwhile if A major benefit of the Atmel Studio IDE is the
you use Atmel microcontrollers more than just code completion function, which you now have
occasionally. available for Arduino sketches as well. For this
With this debugger you can set real breakpoints you have to enable Visual Assist X via VAssistX
without recompiling the program, observe vari- Enable/Disable. With this enabled, all possi-
ables in the Watch window, and examine or ble completion options are listed each time you
Figure 4.
change memory contents. What’s more, you can type a character in the Code window. For exam-
The Atmel Studio editor
inspect the numerous I/O registers and alter them ple, if you type “S” at the start of a new line in
helps you avoid typos
with a mouse click. a sketch, the terms Serial, Server, SPI and so on
by suggesting function
After you install Visual Micro, a new toolbar are suggested. You can then select the appro- names and options for
appears in Atmel Studio. On this toolbar you priate term directly, with no risk of typos. Even autocompletion.
can select the current Arduino version (1.5.x), better, after you type a dot, for example after the
the board (Arduino Due) and the programming term “Serial”, the existing attributes and methods
interface (programming port) as illustrated in of this class are listed for selection (Figure 4).
Figure 1. After this you have to configure the
appropriate virtual serial interface, which you At this point you could create or open a sketch
can find by looking in the Device Manager win- in the Arduino IDE in the usual manner and then
dow when the Arduino Due is connected to the import it into Atmel Studio. However, you now
PC with a USB cable. As in the Arduino IDE, the
serial monitor button is located to the right of
these settings. Adapter for SAM ICE +3V3
If the settings do not appear automatically, for This adapter is necessary for using
JTAG
example because the Arduino IDE software is the SAM ICE debugger/programmer SAM-ICE
2 1
not located in the default directory, you have to with the Arduino Due. 4 3
+3V3
window shown in Figure 2. There you have to It connects the 10-pin JTAG 10 9 2 1
12 11 JTAG_TCK 4 3
enter the correct folder location manually, since pinheader with 50 mil (0.05”; 14 13 JTAG_TDO 6 5
16 15 JTAG_RESET 8 7
no selection dialog is available. 1.27 mm) pin pitch on the Due board 18 17 10 9
20 19
Clicking the question mark on the menu bar in to the 20-pin JTAG connector on the
Figure 1 opens the Micro Explorer window (Fig- SAM ICE device. 130392 - 66
4
www.elektor-magazine.com | December 2014 | 47
5
48 | December 2014 | www.elektor-magazine.com
Arduino IDE, a copy of this file is also located in The actual structure of the downloaded Arduino
the debug folder for the sketch. It can be seen program can be seen in the Solution Explorer win-
in the Solution Explorer window of the code gen- dow, where the main.ccp file is selected and the
eration instance of Atmel Studio under “Output cursor is positioned on the first line of this routine
Files” (Figure 8).
6
www.elektor-magazine.com | December 2014 | 49
SpeedTest
Only the following two instructions are executed
in the main loop of the example sketch (see the
SpeedTest listing):
digitalWrite(TP1,1); //On
digitalWrite(TP1,0); //Off
Figure 14.
Microcontroller PIO register. With an oscilloscope, you can see a pulse wave-
form on this pin while the program is running,
with a pulsewidth of 2.35 µs and a period of
6.6 µs (Figure 13), which corresponds to a fre-
quency of about 150 kHz. That is not very high,
and you could hardly be blamed for thinking that
there’s no reason to use an ARM controller clocked
at 84 MHz for this when a simple 8-bit AVR micro-
controller can do it better.
7
50 | December 2014 | www.elektor-magazine.com
Listing 1. Speed-Test.
//*******************************
//Speed-Test with Arduino Due
//*******************************
void setup()
{
/* add setup code here */
// set the digital pin as output:
pinMode(TP1, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TP1,1); //On
digitalWrite(TP1,0); //Aus
}
8
www.elektor-magazine.com | December 2014 | 51
#else
digitalWrite(TP1,1);
digitalWrite(TP1,0);
#endif
}
9
52 | December 2014 | www.elektor-magazine.com
C Modules
Software for Elektor Extension Shield,
Relay Board and more
By Jens Nickel (Elektor Germany)
10
56 | November 2014 | www.elektor-magazine.com
11
www.elektor-magazine.com | November 2014 | 57
RELAIS
ing to the Arduino-Uno board has the name
ARDUINO BoardEFL_ArduinoUnoCore.
#0
RS485
#2 #2 #3 #3
• We can now use several extension boards in
our project, since each of the corresponding
code files has a different name, for example:
#1
ExtensionEFL_Arduino_
SHIELD
ElektorExtensionShield.c/.h
140328 - 17 ExtensionEFL_ECC_RS-485.c/.h
ExtensionEFL_EEC_Relay8.c/.h
12
58 | November 2014 | www.elektor-magazine.com
Short code
The application itself can be viewed in Listing 1. CONN#1
ADC#0
13
www.elektor-magazine.com | November 2014 | 59
BlockEFL files
Up till now BoardEFL and ExtensionEFL files have still included the so-called Low Level/Block Functions, such as for example
the Function
COMMON
independent of which particular route the bytes take to reach
the Display. This Block Function must be defined once in the BOARD BLOCK BLOCK BLOCK
DISPLAY IO UART
project if a Display is provided on any of the boards. HARDWARE
LAYER
Logically we therefore provide a dedicated file pair in the CONTROLLER
Hardware folder for the Low Level/Block Functions required
by the Display. If a Display is used anywhere in a project, HARDWARE
you need to integrate BlockEFL_Display.h/.c in addition. A 140328 - 14
Figure 5.
You can display the EFL Tables using a Terminal program.
The Blocks are set out centrally.
14
60 | November 2014 | www.elektor-magazine.com
15
www.elektor-magazine.com | November 2014 | 61
Virtualization
Port expanders and external A-to-D converters are two The concept of ‘Virtualization’ is now broadened to analog
sample applications for chips that extend the capabilities of inputs. In our case the external ADC is located on the
a Controller. Frequently these chips are addressed via an EEC/Gnublin ADC Expansion board, so the code in file
I2C Bus or some other serial interface. ExtensionEFL_EEC_ADCModule16bit.c must take over
the activation of its ADC. When the board is initialized with
A modular prototyping library should, as far as possible, the Function ExtensionEFL_EEC_ADCModule16bit_Init(…)
be based on the hardware used. For someone developing the external ADC is integrated into the EFL Tables like
an application (or a Library that activates Peripherals) an internal ADC (in our case with the Block number #1),
it should be immaterial how the wiring tracks run on where, however, it is assigned to the virtual Port 0x40. In
the board or to which Controller pins the Peripheral is addition the I2C interface is made ready. Furthermore the
connected. Ideally the developer should also not need to Function Virtual_ADC_GetValue(…) is notified to the
know whether an input or output is connected to a genuine Controller code.
Controller pin or merely to a Port expansion chip. We
can solve that problem by assigning to the Controller in Using the Function ADCSimple_GetRawValue(uint8
addition to its Ports 0, 1, … etc. some extra ‘virtual’ Ports ADCBlockNumber, uint8 ADCPosition) users now have
that begin with the number 0x40 = 64, so as to be able to access to an ADC pin in a specific ADC Block, independent
differentiate these from ‘real’ Ports. of the connection to the Controller. The Function directly
If for example you wish to activate a relay connected to a calls up the Controller Function ADC_GetValue(…). The
Port expansion device, then access to the relay from the Controller refers to the EFL Tables and on account of the
RelayEFL Library will be forwarded perfectly normally to high Port number recognizes that an internal ADC is not
the Function SwitchDigitalOutput(…), located in the file intended but instead it should call up the Function Virtual_
BlockEFL_IO.c. This Function refers in the EFL Tables which ADC_GetValue(…), located in the code file of the Expansion
Controller Port and pin the relay belongs and calls up the board. This results in giving access to the external ADC over
Function IO_SetPinLevel(…) in the Controller file. In the I2C.
Tables a Port 0x50 is recorded for the relay and normally
this would be the end of it, as the Controller is unaware As well as the Function ADC_GetValue(…) our application
of any Port 0x50. However, for such situations when the also virtualizes the Function ADC_GetParameter(…), with
relay board is initialized, a special Function of the relay which the resolution and voltage range of an ADC can
board code is passed to the Controller code that is called be read off. In this way ADC values can be calculated in
up in cases like this. This same Function then sends the millivolts.
corresponding I2C commands to set the output pins of the
Port expansion unit located on the board.
DISP#0
ADC#0
Figure 7.
CONN#1
Internal and external ADCs can be addressed using the
SHIELD 140328 - 13 same Functions, with the Block numbers #0 and #1 used
for differentiation.
16
62 | November 2014 | www.elektor-magazine.com
17
www.elektor-magazine.com | November 2014 | 63
It had always fascinated me what I might hear—or ten in Europe where the AC supply frequency is
rather see on an oscilloscope—if I could connect 50 Hz but exactly the same methods will work
a pick-up coil, with a couple of hundred turns on in territories where the line frequency is 60 Hz.
it, to an extremely sensitive amplifier. A dozen Please read ‘60’ wherever you see ‘50’ from now
or so years ago I decided to turn this supposition on, if you live in a 60 Hz country. Ed.]
into fact using modern electronics. Eventually, after I submitted my received data to
The first circuit I constructed for this purpose FFT-versus-Time analysis, it became very clear to
differed from the version presented here only me that that this ‘wriggling about’ on the screen
by having a cruder filter and a somewhat old- was the result of recurring signals of typical struc-
er-fashioned method of analog to digital con- tures, which could be resolved only if they could
version. To my surprise there appeared on the be compressed over prolonged periods of time.
monitor screen more than the power frequency They were also audible if played back at higher
hum that I was expecting but unfortunately the speed, sometimes reminiscent of animal sounds
confused serrations of the complex time signals or teletype transmissions on the short waves. In
did not allow me to draw any conclusions from any case, all this was sufficiently interesting to
about their composition. [this article was writ- keep me occupied with it ever since. Readers who
18
8 | October 2014 | www.elektor-magazine.com
are interested will find further detailed informa- be connected to other recording devices, all the
tion at the blog vlf.it [3], which is a platform for time keeping in mind that signals below 16 Hz
enthusiasts involved with receiving and experi- will be attenuated heavily by PC sound cards.
menting in the ELF and VLF bands. I have pub- The circuit is made up from a combination of a
lished a number of articles there on this theme highly sensitive voltage amplifier and a steep
along with many screen shots. (36 dB per octave) Sallen Key low-pass filter with
Among other things, we need to understand that a cut-off frequency of approximately 21 Hz. The
supply transformers in residential areas radiate receiver has the task of amplifying extremely
extremely weak magnetic waves between around weak magnetic waves in the frequency range
0.3 Hz and 25 Hz. These are up to 1,000 times from 21 Hz down to (almost) 0 Hz and filtering
weaker than the interference fields produced by out line hum interference in the process. Figure 1
the 50 Hz AC supply. To receive the desired fre- shows the schematic for this receiver, which is
quencies without interference, we need to filter made up from the functional groups that follow.
out the 50 Hz (60 Hz) supply hum as early as
possible ahead of the main amplifier in order to Linearizer and preamp
avoid over-driving the receiver. The extremely weak (in the microvolt region) AC
signals of interest here are picked up with a coil
The circuit and once processed and optimized in a combi-
The receiver described here operates in conjunc- nation of preamplifier and low-pass filter (IC1),
tion with the ADC module described in a separate they are directed to the Sallen Key low-pass filter
article, an Arduino Uno and some free—that goes that follows. This simple upstream low-pass filter
without saying—recorder software for the PC. This (a pre-filter so to speak) is necessary specifically
combination makes it possible to detect, display for attenuating any 50 Hz line frequency interfer- Figure 1.
and log weak alternating currents and/or alter- ence in relation to the wanted signal to prevent Schematic of the ELF
nating magnetic fields at frequencies down to less overloading that might generate a square-wave Receiver (without Data
than 1 Hertz. The receiver output can additionally signal between the maximum output voltages Logger).
R7
56k K2(S1)
1 2
R6
C1 3 4
120k TP1
5 6
R5 IC3 = TL074
10n 390k
R2 C6
10M R4 C4
9
470k C2 1u TP2
K1 6 8
IC1
8
6 2 1 5
OP07 10k R8 R9 IC3A 18k 220k
1
3 6 3
Coil OP07 22k 180k C7 TP3
3 C5
C3 22n
47n
68n
IC4
IFX25001TS V50 +5V
S2 L1 L2 +12V R16 P1
1 3
47k
100u 1 6 100u
+VIN +VOUT C22 C20 C18 1M
C8 C9 R15
13 K3
2
2 4 0Ω6
-VIN -VOUT 10u 100n 47u 900mA
63V L3 25V
100u -12V
+12V +12V +12V +5V
R17
C10 C12 C14
1k5
7 7 4
100n 100n 100n
IC1 IC2 IC3 D1
C11 C13 C15
4 4 11
19
www.elektor-magazine.com | October 2014 | 9
of the op-amp. This could occur were the coil The stage built around IC2 is a well-known ‘stan-
to be placed close to a power cable in which a dard’ circuit involving an inverting amplifier. The
heavy current was flowing. The circuitry associ- gain factor can be varied by selecting one of sev-
ated with IC1 has a second function: the char- eral feedback resistors. This feature is absolutely
acteristics of the coil at the input of the circuit necessary since completely different intensities
mean that low frequencies are attenuated appre- of the received signal may arise, according to
ciably, so that the amplitude of received signals the position of the receiver. R4 is not a built-in
in the region of zero Hz is weakened increasingly. part of the selector switch, ensuring that there is
We can compensate or ‘linearize’ this to a large always some degree of negative feedback, even
extent using the effect of capacitor C1 in paral- when the switch settings are open-circuit. This
lel with R2. Figure 2 shows the amplification at has the advantage that at the moment of swi-
the output of IC1. tchover, when the switch contact ‘hangs in mid-
air’ for a very brief timespan, no interference
pulses appear on the receiver output.
The gain or amplification of the inverting ampli-
Audio Precision
+24 fier arises from the quotient negative feedback
+23
resistance divided by the upstream resistor:
+22
+21
+20
V = Rg/Rv
d +19
B
r +18
+15
-40
50 Hz signal relative to the desired signal makes
d
B
-50 this filter extremely necessary, to prevent over-
r -60
A -70
loads. To learn more about Sallen Key Filters you
-80
can find the desired background information in
-90
-120
Figure 3. -130
stated, 21.5 Hz, which is far enough removed
10 20 50 100 200 500 1k
Filter IC3 achieves a slope of Hz 140035 - 55
-10
-20
-30
anteed. Figure 3 shows how steep the flanks of
-40
the resulting filter are (measured at the output
-50
d
-60 of IC3C).
B -70
r
-80
A
-90
-100
High-pass and final stage
-110 At high levels of gain (according to the setting
-120
-150
quite small offset voltages could nevertheless
6 10 20 50 100 200 500 1k 2k
reduces interfering noise by Hz 140035 - 56 be sufficiently large to shift the output signal by
up to 40 dB. several volts into the positive or negative regions
20
10 | October 2014 | www.elektor-magazine.com
21
www.elektor-magazine.com | October 2014 | 11
Installation and
operation of the recorder
Now connect the ELF receiver to the 16-bit Data-
logger module described in the September 2014
edition of Elektor [13]. The ADC samples the
signal with a resolution of 15 bits and a sampling
rate of around 112 Hz. The article also explains
how the ADC module can be connected to an
Arduino Uno, which accepts the digitized data
using a simple program (Sketch) and relays this
to the PC and the recording software.
The recorder software is written in the Processing
programming language [12], which resembles
C. Curly brackets are used for code blocks; each
instruction must be closed off with a semicolon.
The programming environment is very simple:
just open the Editor and write the source text.
Then click on the Start button and you’re rolling.
22
12 | October 2014 | www.elektor-magazine.com
What is ELF?
ELF signals are a mysterious and, to some
degree, myth-ridden subject that amounts in
reality to nothing more than electromagnet-
ic waves of extremely low frequency (hence
ELF) from 3 Hz to 30 Hz. Because commercial
radio transmissions do not exploit such low
frequencies, it is naturally fascinating to inves-
tigate what is going on in this profound realm.
Wow! This signal occurred on one single occasion
over a night in September 2013. Duration around
In residential areas many of the signals de-
one hour. Recording made with electrodes. Frequency
tectable with the receiver described here
range: 0 to 20 Hz.
clearly take the form of magnetic waves ra-
diated by supply transformers at the local
substation. The sprawling network of metallic
conductors (ground connections, water and
gas pipes, etc.) evidently behave like a vast
underground antenna that gathers up the
weakest low-frequency alternating currents
flowing in the ground, wherever they may
arise from, and transports them to a common
connection point at the local substation. Here
A square-wave signal of 1.6 Hz, which arises in
(this is merely an assumption) these currents various locations across all Europe at irregular times.
are radiated as magnetic fields by the ground- Typical characteristics: phases of activity and intervals
ed Petersen Coil (used for ground/earth leak- changing regularly.
age compensation).
23
www.elektor-magazine.com | October 2014 | 13
Operation
Operation of the software recorder in the Win-
Figure 8.
dows-style window (Figure 9) is virtually self-ex-
Power section and filter/
planatory. The test results are shown in three
amplifier should be placed
windows on the left-hand side.
as far apart as possible from
one another. Inductance L4
is fitted in between them Time signal
(here on the underside of An iteration takes five seconds. After the program
the PCB). starts a signal is always visible here, even if it is
not being recorded—and after recording stops.
Software installation
To get a Processing program to run on your com- FFT vs. Time
puter, you need to download the necessary soft- Every x seconds (x depending on the value set
ware from the Internet onto your machine. when downsampling) a new line is plotted—even
Go to the Processing website [7] and follow the if no recoding is being made—and after record-
instructions given there. The data downloaded ing stops.
can go into any folder you choose on the hard
disk. Within this data is also a file with the name Supervisory signal
processing.exe. Run this program if you want to After each iteration of 5 seconds, the highest
write Processing software of your own. Numerous amplitude of this time segment is indicated in
impressive sample programs not only showcase the upper window.
the powerful capability of this language but also
indicate how you can make the best use of it. The parameters for measurement and display
The Recorder program written in Processing can are set on the right-hand side of the recorder:
be downloaded from the Elektor website [8] into
any folder of your choice. Recording time
Length of the recording.
Important: The Processing program must be
located in a sub-folder bearing the same name Downsampling
as the program itself—but without the ‘.pde’ suf- Zoom in the Y direction in order to see the lower
fix. Also all resources required by the program frequencies better. Relates only to the FFT dis-
(such as .wav files or associated graphics) must played and not to the recording.
be kept in this sub-folder. After double-clicking
on the recorder file (Recorder_.....pde) the Pro- FFT brightness
cessing editor window opens automatically and Renders the FFT displayed brighter or darker.
the program code is implemented. Relates only to the FFT displayed and not to the
In the following line you need to replace ‘COM3’ recording.
and enter the COM interface of the PC allocated
by Arduino (see Device Manager in Windows): FFT scrolling
Pages forwards and backwards through the
serport = new Serial(this, “COM3”, 115200); analysis data displayed. Valid only for the data
recorded during the current recording phase still
Then save the program code with File Save. held in RAM. FFT data is not stored on the hard
After a (single) click on the arrow at top-left in disk.
the Editor window, the program begins. The Editor
window with the source code remains during this Mouse position
process on the screen (in the background). Unfor- Mouse position coordinates and number of but-
tunately (and not for want of searching count- tons clicked. Very important if you wish to work
less different sources) I have not managed to on the program yourself.
find a working .exe file for the program. Further
information about Processing can be found in the Recording
Editor itself (Help Reference) and on countless Left-hand button
other Internet pages. Normal method of starting a recording of a dura-
24
14 | October 2014 | www.elektor-magazine.com
Web Links
[1] Online ELF blog: www.vlf.it
[2] https://en.wikipedia.org/wiki/Sallen%E2%80%93Key_topology
[3] Coiled wire: http://www.jameco.com/1/1/379-30pe-awg-plain-enamel-magnet-wire-1-4-lb-825-ft.html [USA],
http://www.scientificwire.com/acatalog/Solderable_Enamelled_Copper_Wire.html Ref: SX0250s-D200 [UK].
Alternatively just look on eBay.
[4] Input transformer: http://www.jensen-transformers.com/ln_in.html
[5] http://blackboard.serverpool.org/
[6] www.elektor-labs.com/project/arduino-16-bit-low-frequency-datalogger-130485-i-140035-i.13703.html
[7] http://processing.org/
[8] www.elektor-magazine.com/140035
[9] https://groups.yahoo.com/neo/groups/VLF_Group/info
[10] http://naturalradiolab.com/
[11] http://en.wikipedia.org/wiki/Schumann_resonances
[12] http://en.wikipedia.org/wiki/Processing_(programming_language)
[13] www.elektor-magazine.com/130485
25
www.elektor-magazine.com | October 2014 | 15
This project is a good example of where our Hunting for ELF signals
web-based project platform Elektor.Labs [1] has At Elektor.Labs you can read more details of the
provided the spark for new ideas. Kurt Diedrich, original project [2]: The ELF signal picked up by
an author well known to us, has recently been the large wire loop is first amplified, filtered and
busy experimenting with an Arduino Uno to then given a DC offset of 2.5 V so that the signal
expand his knowledge of microcontroller firm- swings between 0 and 5 V and does not go nega-
ware. It didn’t take too much encouragement tive. The Arduino Uno uses an ATmega328 which
to persuade Kurt to share his experiences with already has a built-in A/D converter but with only
a wider audience via Elektor.Labs. One of the 10-bit resolution. Kurt Diedrich did some research
designs he came up with is a data logger where and ended up ordering a small PCB from Ada-
an Arduino Uno measures a signal level and fruit which has on-board an ADS1115 (from TI).
sends it to a PC [2]. Kurt’s previous experience
with PC programming and graphical user inter- This particular A/D converter has four input chan-
face design could now be put to good use: using nels and can measure signals with 16-bit reso-
the programming language called Processing lution [3]. An I2C bus is used for control and to
he designed a Tool that not only represents the pass information between the chip and microcon-
sampled signal but can also performs spectral troller. It wasn’t long before Kurt was studying
analysis. The complete setup is ideally suited the Arduino code examples and software libraries
for processing the reception of low-frequency supplied by Adafruit. The routines he needed to
electromagnetic signals to be discussed in a implement are not complex; after a little tin-
follow up article describing an ELF Receiver kering with the code examples the data logger
(ELF = extremely low frequency). firmware was finished. An endless loop in the
26
40 | September 2014 | www.elektor-magazine.com
The concept
The prototype worked really well but the fin- USB
ished set up looked a bit untidy with all those
flying leads between the ADC module and Arduino
board. Labs set about making improvements; the 130485 - 12
main decision was to not make the design into a
dedicated ADS1115 Arduino shield but instead
opt for a more flexible approach that would allow
the board to easily interface to a number of dif-
ferent systems [4]. C1
+3V3
47p
R2 ALERT/RDY POWER
100k
The ADS1115 chip should derive its power from D1 D2
R1
the attached controller board and be controlled 4
100k
REF2912 1
via an I2C bus. A good choice for the interface +3V3 AIDBZT R3 IC2
3 1V022
120k R8 R9 R10
would be the 14-way pin header used by the EEC/ 1 5
IC3 2 0V511 OPA377
C4
560R
C3 C2 R4 R5 T1
nected via a length of flatcable to the Elektor-Li-
270k
120k
AIN0 7 8 K3
VDD
AIN1 4 1 EEC/Gnublin
AIN0 ADDR
Voltages 5
AIN1 ALERT/RDY
2 1 2
6 9 3 4
The ADC board circuit diagram is shown in Fig- 7
AIN2 SDA
10 5 6
K2 AIN3 SCL
ure 2. The Gnublin/EEC connector used by the R6 R7 7 8
GND
AIN2 9 10
board specifies a 3.3 V supply voltage but the ADC
2k2
2k2
AIN3 ADS1115 11 12
3
chip can operate with a supply range between IDGST JP2 JP3 13 14
2.0 and 5.5 V.
+3V3
130485 - 11
27
www.elektor-magazine.com | September | 41
VDD
ADS1115 Comparator
Voltage
Reference ALERT/RDY
AIN0
AIN1 ADDR
AIN2 16-Bit 2
MUX PGA IC SCL
AIN3 ADC Interface
SDA
Figure 3. Oscillator
The data sheet [3] gives details of how the chip diodes on the inputs which provide some degree
can be controlled and configured using I2C com- of protection but the data sheet recommends fit-
mands. It can be configured to measure: ting external zener diodes and series resistors to
give better protection.
• The voltage level on AIN0, AIN1, AIN2
and AIN3 relative to ground (i.e. four ‘sin- Jumper JP1 can be fitted to provide a 1.022-V
gle-ended’ inputs). offset. The voltage level is made up of the 1.25-V
• The voltage level on AIN0 relative to AIN1 reference voltage level produced by IC3 and the
and the voltage level on AIN2 relative to standard op-amp IC2. When the offset is applied
AIN3 (two differential inputs). the input voltage range is ±2.048 V. An AC input
• The voltage level on AIN0 relative to AIN3 signal swinging between the values of around
and from AIN1 relative to AIN3 (two differ- –1 V and +1 V will be translated into a voltage
ential inputs with a common node). at the ADC input AIN0 swinging between 0 to 2 V
with 15-bit resolution. In the software be aware
The chip outputs a digital value in the range that higher input voltages produce lower output
from –32768 to 32767; negative values indicate values (inversion).
a negative differential voltage (negative values
are expressed in twos-complement format). In Another possibility is to add the offset to AIN0 and
single-ended mode the voltage will always be use the tip described above. With the measured
positive so that the output values will be in the voltage applied to AIN1 or AIN3, the correspond-
range 0 to 32767 which corresponds to a 15-bit ing differential mode configured and a full-scale
resolution. Using a workaround suggested by Ton range of ±1.024 V set. This method makes it
Giesberts in our lab and described in more detail possible to measure a voltage in the range from
on the .Labs Website [8] it allows measurement 0 to 2 V with a 16 bit resolution.
of negative-going signals while producing a 16-bit
value: Apply a fixed DC voltage, say 1 V on input Timing
AIN3. Now an input voltage of 0 to 1 V on the The ADC operates using the Delta-Sigma princi-
inputs AIN0 and AIN1 produces a negative out- ple, which gives good resolution and accuracy.
put value when the corresponding differential This method of A-D conversion however is not the
mode is selected. fastest. A rate of up to 860 samples/s is possible.
The sample rate is configurable and has a default
In addition to an internal voltage reference the value of 128 samples/s. Like many other ADCs
ADC also has a built-in programmable amplifier the ADS1115 can work in single-shot or contin-
which allows you to setup the full scale voltage uous mode. In single-shot mode it is necessary
range that the ADC will measure. In our case, with to issue a command to the chip to tell it to mea-
a 3.3 V operating voltage, the ranges ±2.048 V, sure the input voltage. Once the measurement
±1.024 V, ±512 mV and ±256 mV are of interest. is complete and a valid digital value is available
With this board it is important to ensure the the chip will set a bit in one of its internal regis-
measured input voltage does not go above 3.6 V ters. The external microcontroller monitors the
or below –0.3 V. The chip uses integrated ESD state of this bit and reads the new value from an
28
42 | September 2014 | www.elektor-magazine.com
internal register when it becomes available. It can the role of a slave. Using just write operations you
then go on to request a new measurement. In can, for example set up an internal configuration
continuous mode the chip continually measures registers or tell the chip to start a measurement
the input voltage and produces digital values process. In order to read out a digital value we
without the need for any external intervention. need to both write and read over the I2C bus.
In all our programs including the ELF receiver To be precise you need to write twice (which
application we use single-shot mode in a contin- includes the slave address) and then read once.
uous loop. After each new reading (and before The chip’s slave address can be set-up externally
issuing the next sample command) the value is so it allows more than one ADS1115 chip to be
processed in the microcontroller (e.g. to show connected to the same two bus wires (provided
on the display or send over the serial interface). you use different addresses for each chip). The
Using the default sample rate of 128 samples/s slave address is defined by how pin 1 is con-
produces a data rate of about 100 to 120 Hz nected. There are four possible slave addresses
which should be sufficient for the majority of available; pin 1 can be linked to either pin 3, 8,
data logger applications. 9 or 10 using a jumper on the header pins of JP4
to define the address.
The comparator
The Conversion-Ready-Pin (IC1 pin 2) can be The two I2C signals are connected from the
used to indicate when an A-D conversion has chip to pins 5 and 6 of the EEC/Gnublin connec-
been completed. The ADS1115 can also be con- tor. Jumpers JP2 and JP3 allow you to use the
figured to act as a comparator (Figure 3) and on-board pull-up resistors. The beauty of the
in this mode the pin acts as an alert output. I2C bus is that you can safely interface a slave
Now using values stored in the Low Threshold device operating at 3.3 V with a controller such
and High Threshold registers you can allow a as an Arduino Uno running at 5 V.
sort-of hysteresis or apply a voltage compari-
son window to the measured value so that an The software library
alert occurs only if the measured value is outside When it comes to the control software you know
these stored values. that even if you opt for the default settings of
In our module this output is made visible via the ADC you will still need to spend some time
transistor T1 and a yellow LED. In addition this studying its data sheet. Controlling a microcon-
signal is routed to pin 11 of the Gnublin/EEC con- troller’s I2C interface can also sometimes be a
nector K3 where it is available for use by either bit tricky. Don’t worry; we have simple solutions
the Linux- or Xmega-board but has no connection that should get around these potential problems.
on the Elektor extension shield. The technical boss at the Elektor Labs Clemens
Valens has written a C library that avoids the need
I2C to define which registers you need to address in
As we already mentioned communication with the the ADC and instead provides high level func-
ADC occurs over an I2C interface where it plays tions to control the chip. As a bonus he has also
Web Links
[1] www.elektor-labs.com
[2] www.elektor-labs.com/project/arduino-16-bit-low-frequency-datalogger-130485-i-140035-i.13703.html
[3] www.ti.com/lit/ds/symlink/ads1115.pdf
[4] www.elektor-magazine.com/130485
[5] www.elektor-magazine.com/130214
[6] www.elektor-magazine.com/120126
[7] www.elektor-magazine.com/140009
[8] www.elektor-labs.com/contribution/from-the-lab-using-differential-mode.14034.html
[9] www.elektor-magazine.com/120668
29
www.elektor-magazine.com | September | 43
ADS1x1x_start_conversion(&my_adc);
and
are available.
void soft_i2c_scl_write(uint8_t value) // write high or low state to the SCL pin
void soft_i2c_sda_write(uint8_t value) // write high or low state to the SDA pin
void soft_i2c_sda_mode(uint8_t value) // configure SDA pin as O/P (value=1) or I/P (value=0)
Five further functions bind the ADC library (from you will find more demo programs and interesting
Clemens) with the I2C software library, this code applications which make use of the Embedded
can just be dropped 1:1 into the project. Firmware Library [9].
30
44 | September 2014 | www.elektor-magazine.com
Our Arduino sketch performs the same software It is probably easiest to upload the sketch using
function as Kurt Diedrich’s original project: An the Arduino bootloader, that way you only need
endless loop repeatedly tells the ADC chip to a USB cable between the Arduino and PC. Once
measure and digitize the voltage at input AIN0 in the firmware has been flashed the measured val-
single-ended and single-shot mode. The measure- ues can be seen on the serial monitor display
ment range extends from –2.048 V to 2.048 V, in the Arduino development environment or via
while in Single-ended mode only 0 to 2048 mV a simple terminal emulator program (data rate
can be measured and with only 15-bit resolu- set to 115,200 Baud and the corresponding COM
tion. The decimal output values (0 to 32767) port selected).
after conversion are sent out as ASCII characters To achieve the highest measurement accuracy
over the serial interface, followed by a <CR> and it is better to power the Arduino from a bench
<LF> character. power supply rather than from a USB port. It
will provide much better common-mode noise
For testing we used an Arduino Uno fitted with performance.
the Elektor extension shield described in the
last issue [7] connected to the ADC board via a Some spooky signals
14-way ribbon cable. The I2C address of the ADC If you like you can now try out the recorder soft-
board is configured as ‘ground’ i.e. the jumper ware that Kurt Diedrich developed for a his ELF
is placed in the first position of JP4 nearest the reception experiments. Now when you turn the
Gnublin/EEC connector. Initially we will not use pot the values are recorded over time in soft-
any offset correction (JP1 jumper fitted nearest ware. You can also switch in the 1022 mV offset
the terminal blocks). and see how the measured values change. Volt-
Now to generate the voltage to measure we con- age levels in the range of approximately –1 V to
nect a flying lead from the Arduino pin A3 (you +1 V can be measured with 15-bit resolution,
can connect it at the shield box header connec- this set up can be put to good use investigating
tor as shown in Figure 4) to the AIN0 input the presence of ELF signals in your location. If
of the ADC board. The voltage on A3 can now this sounds interesting have a look at the ‘ELF
be adjusted by twiddling the pot on the shield; Receiver’ project described in this issue you may
this gives us a rudimentary test set-up to check be in for some surprises!
operation of the ADC board. The voltage at A3 (130485)
can be turned up to 5 V but it’s important that
the input voltage level does not exceed 3.6 V so
before connection ensure that the pot is turned
all the way down so that the voltage is at 0 V
then slowly increase it.
31
www.elektor-magazine.com | September | 45
Arduino is a Tool
By Clemens Valens Barring you just woke up from a 10-year long coma you will have heard about
(Elektor.Labs) Arduino. Everybody is talking about Arduino and all & sundry are developing
programs (“sketches”) and extension boards (“shields”) for Arduino. We have
filled scores of pages in Elektor about, or on using Arduino. Here is another one.
to the Arduino
board to make
the serial port
MIDI compatible
(the Data signal
has to be inverted)
and connected it to the
device-under-test (DUT).
This article however is slightly different because We c o u l d h ave d o n e
it is not about an Arduino project but about without the transistor if we
using Arduino as a tool. I use Arduino quite had used a software serial
often because it is so easy to program and use. port on the Arduino, but it would have meant
I sometimes even use it as a calculator because more testing.
it is quicker to write an Arduino program (called
a ‘sketch’) than a PC application. With the tester ready all we had to do was power
up everything, lean back and watch everything
To give you an example, the other day at Elektor. work just fine. This to me shows the real power
Labs we had to test our prototype of the MIDI of Arduino: an easy-to-use multi-purpose tool
Channel Analyzer [1]. Hedwig our secretary had that can help you out in many prototyping and
brought a nice keyboard with MIDI capabilities test situations.
from home, but, for some reason, it wouldn’t allow So, if you are still wondering what all this Arduino
us to test the analyzer properly. We had run into fuzz is about, try to remember this article the
the chicken-and-egg paradox where we needed next time you can’t figure out how to test your
a MIDI analyzer to figure out the MIDI behavior almost-completed-but-not-working yet super-
of this keyboard to test the MIDI analyzer. So duper ultra gadget.
we called Arduino to the rescue. It took me less (140059)
than five minutes to write a sketch that sent a
Note-On and Note-Off message every second on
Web Link
a different, incrementing, MIDI channel. Elektor.
Labs veteran Ton Giesberts added a transistor [1] www.elektor-labs.com/node/3380
32
82 | July & August 2014 | www.elektor-magazine.com
By Jens Nickel Like it or not, very low-cost, mass-produced are fed out to two rows of socket headers which
(Elektor Germany) microcontroller boards have changed the electron- provide the basis for the shield concept. If you
ics landscape. If you want to or have to develop equip another PCB with a matching set of pin
a working project or demo that does not need headers, you can plug it directly into the micro-
very much computing power, you can simply controller board. A shield can hold a wide variety
grab an Arduino Uno – and there’s bound to be of peripheral devices, including displays, sensors,
one lying around somewhere, just waiting to be or interfaces such as Bluetooth or WLAN. Thanks
used. Beginners in particular benefit from the to the widespread use of the small Arduino Uno
free Arduino development environment, and all board, there are now hundreds of commercially
you need to download programs is a USB cable. available shields.
If you prefer to program in Basic, you can also
work with Bascom and the boot loader. Complex Getting started
programs in C or C++ are also possible with the Many of our readers are interested in learning
free AVR Studio environment and a small pro- how to work with microcontrollers and program
grammer, such as the AVR ISP mk2. in Bascom and/or C. For them, the Arduino Uno
is a natural choice. To become truly familiar with
Intelligent shields programming a microcontroller, you need to come
There aren’t many peripheral devices on the to grips with the key interfaces of the IC yourself
Arduino Uno; a single LED is all you get from at least once. The best way to do this, of course,
the board designers. You can also use the USB is to work with peripheral devices that use these
interface to access the microcontroller on the interfaces. Unfortunately, there is hardly any
Uno board (an Atmel ATmega328P) over its UART shield available that provides all the necessary
port. However, most of the microcontroller pins peripherals in a small, low-cost package, so we
33
66 | July & August 2014 | www.elektor-magazine.com
Socket headers Table 1. Arduino pins, microcontroller pins and shield functions
The socket headers of the Arduino Uno are dupli-
Arduino Pin ATmega328 Function
cated on the shield. There is an Arduino standard
SCL PC5 EEC-SCL (*)
for the signals available on these headers, which
must be adhered to regardless of the microcon- SDA PC4 EEC-SDA (*)
troller fitted on the Arduino board concerned. IO13/SCK PB5 ISP-SCK
The individual sockets are also called “Arduino IO12/MISO PB4 ISP-MISO
pins”. The Arduino pins and the corresponding IO11/PWM/MOSI PB3 ISP-MOSI
pins of the ATmega328 microcontroller are listed IO10/PWM PB2 LED2
in Table 1.
IO9/PWM PB1 ECC-GPIOB
IO8 PB0 ECC-GPIOA
Due to the display and the buttons, it is not
possible to plug another shield piggy-back onto IO7 PD7 LCD-D7
the Elektor shield. However, all of the Arduino IO6 PD6 LCD-D6
pins remain accessible when the Elektor shield IO5 PD5 LCD-D5
is plugged in, and in most cases their operation IO4 PD4 LCD-D4
is not affected by the connected shield, as you IO3 PD3 LCD-E
can see from the circuit diagram in Figure 2.
IO2 PD2 LCD-RS
Among other things, this applies to the analog
IO1/TX PD1 ECC-TX
inputs AD0 and AD1, the serial interface pins
RX and TX, and the digital IO pins IO8 and IO9. IO0/RX PD0 ECC-RX
Additional I/O pins can be freed up by pulling AD0 PC0 S1
the jumpers (JP1 and JP2) and unplugging the AD1 PC1 S2
display module. AD2 PC2 LED1
AD3 PC3 P1
LEDs, buttons and potentiometer
AD4 PC4 EEC-SDA
Two supply voltages are always generated on
AD5 PC5 EEC-SCL
Arduino boards: 5 V and 3.3 V. They are available
(*) Connected to AD4 and AD5 on the Arduino Uno
on a socket header and can be used by shields
34
www.elektor-magazine.com | July & August 2014 | 67
R7
RESET 5
SCK
MISO 1
1k
GPIOA which can easily be configured in software as
R4
a digital output. LED1 is connected to AD2 and
10
2
4
6
8
3
ECC
ICSP
K1
K7
can also be used as a photosensor, as described
1
3
5
7
9
6
4
2
+5V
+5V
in the Microcontroller BootCamp article in this
MOSI
TX
100R
issue. According to the Arduino standard, pin
R6
GPIOB
1k
IO10 must be able to output a PWM signal. This
R5
R/W
RS
VL
D0
D1
D2
D3
D4
D5
D6
D7
LCD_E
LCD_D4
LCD_D5
LCD_D6
LCD_D7
K4
SCL 10
SCL AD1 to still be used as analog inputs, we omitted
P2
10k
SDA
+5V
9
SDA
K3
8
8
7
AREF pull-up resistors and debounce circuitry. However,
7 GND
IOREF SCK 6
it’s very easy to connect internal pull-up resis-
3V3
6 RESET 1k IO13
RESET MISO 5
R3
LED3
3V3 +5V
5 IO12
3V3
5V
4
MOSI 4
3
IO11 tors in the ATmega328, both in C and in Bascom.
3 1k IO10
GND GPIOB 2
The potentiometer is connected to analog input
R1
JP1
LED2
2 IO9
GND GPIOA 1
1 IO8
VIN
1k
LED
AD3. If you configure the A/D converter of the
R2
JP2
LED1
LCD_D7 8 IO7
LCD_D6 7 IO6 is common practice, the setting range is 1 to
S1
LCD_D5 6
K5
SDA IO5
AD0
1 330R LCD_D4 5 IO4 1023 (10 bits).
R8
2 LCD_E 4
AD1 IO3
LED LCD_RS
14
12
10
3 3 IO2
8
6
4
2
AD2
4 TX 2
EEC
AD3 IO1
K2
Display
S2
5 RX 1
AD4 IO0
140009 - 11
6
13
11
9
7
5
3
1
AD5
The alphanumeric display module from Electronic
+5V
SCL
10k
P1
330R
3V3
RS485
PORT-
EXPANDER
UART connector
K1 is a fully wired Embedded Communication
Connector (ECC), which has been described in
WIFI
detail in a previous article [4]. In accordance
(PLANNED) T-SENSOR
with the specification, the UART signals TX and
SHIELD
RX as well as two digital I/O lines are available
ECC EEC/
GNUBLIN ADC here, with a logic high level of 5 V. There is also
BLUETOOTH (PLANNED)
(PLANNED)
5 V constantly available on one pin for powering
140009 - 12
connected peripheral devices. In this case the
35
68 | July & August 2014 | www.elektor-magazine.com
Component List
Resistors K1 = 10-pin boxheader, 0.1” pitch
R1,R2,R3,R4,R5 = 1kΩ K2 = 14-pin boxheader, 0.1” pitch
R6,R7 = 100Ω K3,K4,K5,K6 = Arduino Shield stacking
R8,R9 = 330Ω header, Adafruit ID 85
P1 = 10kΩ trimpot with adjuster K7 = 6-pin (2x3) pinheader, 0.1” pitch
P2 = 10kΩ trimpot, SMD, Vishay JP1,JP2 = 2-pin pinheader with jumper, 0.1”
TS53YJ103MR10 pitch
LCD1 = LCD 2x8 characters with back-
Semiconductors ground lighting, Electronic Assembly
LED1,LED2 = LED, low-current, red (SMD DIPS082-HNLED
0805) 2 pcs. 7-way precision socket strip for LCD1,
LED3 = LED, low-current, green (SMD 0805) TE Connectivity 1814655-7
PCB # 140009-1 [1]
Miscellaneous or
Assembled board # 140009-91 [1]
S1,S2 = pushbutton
peripheral device is a small module that gives already been developed in the Elektor labs. The
user projects access to various interfaces to the latter allows the Arduino board to transmit and
outside world (see Figure 3). An RS485 module receive data wirelessly at an ISM frequency like
[4] and a wireless communication module have 433 MHz [5].
Advertisement
100uH
toroid YOU DESIGN IT – WE MACHINE IT
♦ ♦
♦♦••••
••••••
••••
From one piece and at a fair price! Simply
download our free Front Panel Designer at
www.schaeffer-ag.de, design your front
panel and order it directly.
36
www.elektor-magazine.com | July & August 2014 | 69
Voltages
Due to the countless shields now boards and automatically adjust which would make the shield more
available, the layout and pin to the operating voltage of the complicated and more costly.
assignments of the Arduino Uno socket microcontroller. That is why the However, we have not yet rejected
headers have become a sort of quasi- Arduino socket headers always have this plan, and there are already
standard for extension connectors. not only 5 V and 3.3 V supply voltage several prototypes in the labs. We
More and more microcontroller boards, pins, but also an IOREF pin that is decided to design a less complex
even with 32-bit processors, are being connected to the supply rail for the shield without level converters for the
equipped with this interface. However, microcontroller. Microcontroller BootCamp series from
some of these microcontrollers require Burkhard Kainka and relatively small
an operating voltage of 3.3 V instead We originally planned to develop projects. It is nevertheless possible
of 5 V as on the Arduino Uno board, a shield suitable for all types to connect 3.3 V peripheral devices
and their inputs and outputs are of microcontroller boards, and to the partially wired EEC connector,
therefore compatible with 3.3 V signal furthermore equipped with ECC and and the shield can even be used on a
levels. EEC extension connectors compatible 3.3 V microcontroller board with some
with both 5 V and 3.3 V peripheral restrictions.
Ideally, a shield can be plugged onto devices. That would of course compel
both 5-V and 3.3-V microcontroller the use of several level converters,
All signal pins of the ECC (TX, RX, GPIOA and a master device operating at 5 V, such as the
GPIOB) are connected to the corresponding Ardu- ATmega328 in this case, can easily be connected
ino pins through resistors, based on a suggestion to a slave device operating at 3.3 V, such as a
from Burkhard Kainka. This provides a bit of pro- Gnublin module. Both lines are held at the high
tection in the development environment, so that level by pull-up resistors, which in this case are
the Uno microcontroller will not die immediately located on the extension module, and pulled to
if something a bit higher than 5 V is inadvertently the active low level by the master or the slave.
connected to a pin. Resistors R8 and R9 attenuate reflections on rel-
atively long lines and provide some protection
EEC/Gnublin against noise pulses.
Extension modules compliant with the Gnublin/ The Gnublin modules available in the Elektor Shop
EEC standard can be connected to K2, which is include a board with eight relays, a port extender
configured as an Embedded Extension Connector with sixteen I/O lines, a temperature sensor and
(EEC). This connector is not fully wired because other devices [6].
the standard specifies a 3.3 V level and we omit-
ted a level converter to keep the cost down (see Programming
the “Voltages” inset). Most of the Gnublin mod- The Elektor shield provides everything you need
ules developed by the company Embedded Proj- for experimenting with or learning about the
ects, which are also available in the Elektor Shop, essential function of the ATmega microcontrol-
are controlled using only the I²C lines SDA and ler or another 5 V microcontroller on some other
SCL. One of the advantages of the I²C bus is that Arduino board: digital outputs and inputs, analog
inputs, a parallel interface (four bits), a serial
UART interface and I²C. The only thing that’s
Web Links missing is SPI. The pins for this are fed out to
[1] www.elektor-magazine.com/140009 connector K7, a 2x3 pin header. You can use
this connector to program the ATmega328P on
[2] www.lcd-module.de/eng/pdf/doma/dips082e.pdf
the Arduino Uno board, with the aid of a simple
[3] www.datasheetcatalog.com/datasheets_pdf/S/T/7/0/ST7066.shtml programmer such as the AVR ISP mk2. That will
[4] Elektor March 2014, www.elektor-magazine.com/130155 overwrite the Arduino boot loader, but it can be
[5] Elektor May 2014, www.elektor-magazine.com/130023 downloaded again using the Arduino IDE.
(140009-I)
[6] Elektor Gnublin series http://www.elektor.com/development/gnublin
37
70 | July & August 2014 | www.elektor-magazine.com
Attention Bridgeport lathe and milling machine Without even scratching the surface of elemen-
users; get in touch with an electronics geek and tary metalworking and finishing, electronicz peo-
have him build this circuit for you. ple should know that the turning speed is crucial
Attention Arduino users: build this circuit for a for anything that cuts, drills, mills, finishes or
nearby Bridgeport lathe owner and get access polishes metal, wood, glass, and recently, hard
to his prize machinery. plastics, Teflon® and rubber too. In the case
of a round steel bar being milled to end size,
In this project a reflective IRED / phototransistor the turning speed used to do the first couple of
device is used as a sensor to produce a retrofit rough passes using toolbit ‘A’ at angle x may
readout of the number of revolutions of a spin- differ considerably from that applied much later
dle (or “shaft”) on a lathe, a milling machine or for an ultra-smooth surface finish using toolbit
a similar piece of equipment in the metalwork- ‘B’ at angle y. In both cases, the shaft speeds
ing toolshop. have to be set and verified by the operator using
38
14 | June 2014 | www.elektor-magazine.com
whatever gears or reduction drives are available black adhesive tape or similar is secured around
on the machine. the lathe’s shaft, leaving a narrow gap (of about
To the metalworker, the turning speed his prized 3 mm). Whenever the light emitted by the LED
machine is running at equals the supply volts and reflects off the shaft at the position of the gap,
amps to the electronics engineer. Get the turning it illuminates the internal phototransistor, caus-
speed (≡voltage) wrong and you risk destroying ing the SENS+ line to drop logic Low, and High
your precious stock material (≡64-bit ARM MPU), again when the LED light is not reflected. Hence
object being created (≡Linux dev system) and/ the speed of the rising edges of SENS+ signal
or $50K Bridgeport lathe (≡128-bit LeCroy logic indicates the shaft speed. Note that due to reduc-
analyzer). ≡You don’t want that to happen. tion gears being present in the lathe or milling
Where we write spindle or shaft in the remainder machine the shaft speed is not the motor speed.
of this article of course we mean chuck, 3-claw The Arduino Micro can be reset to force it to start
Figure 1. Schematic of
head, 4-claw head, boring head, end mill, polish executing its firmware from scratch by press-
the precision rev counter
pad, drill, tap, etcetera, since all of these tools are ing S1.
for CNC lathes and milling
fitted eventually to the powered shaft or spindle machines. All measurement
of the lathe or milling machine. Finally, the function of LED2 on Arduino Micro and control functions are
line D7 is free to assign by the programmer—like handled by an Arduino Micro
How it works over rev or call keith [4]. plug-on board.
As with most applications of embedded technol-
ogy the schematic in Figure 1 does not reveal
a lot about the workings or aim of the circuit. In
IC1
fact, you could be looking at NASA’s latest alliga- D1 78L05Z +5V
K1.1
tor counter for use at Cape Canaveral. One of the
reasons for the happy scarcity of components in 1N4148 R1
BT1
the schematic is the use of an Arduino Micro board
390R
C2 C1
9V
in position MOD(ule)1, resulting in all manner of
100n 100n LED1
things being controlled and decided by software
rather than discrete parts. The Arduino Micro is K1.2
first programmed with the project firmware of
+5V
course via its micro USB connector.
+5V R2
MOD1 +5V
alkaline battery (or a 7 V to 9 V power adapter)
R3 R4
enters the circuit on K1, and gets reduced to 1 1
100R
S1 MOSI SCK
22k
On indicator. LED+
1
RESET
3
TX VIN
3
2 4 ICSP 4
The Adafruit OLED (organic light emitting diode) LED– RX GND
3 5 5
SENS+ RST RST
display module [1] is connected straight onto 4 C3 6 6
SENS– GND +5V
8-pin connector SV1. The OLED module with its 7
D2 NC
7
10n
monochrome, 128 x 64 graphic display gets its SENSOR 8
D3 NC
8
SV1 9 9
5-volts supply voltage from the same regulator D4 A5
8 10 10
D5 A4
as the Arduino Micro. Other OLED modules may 7 11 11
+5V D6 A3
be used but be sure you match their pinout to 6 12
D7 A2
12
5 OLED_CS 13 13
the board. D8 A1
4 OLED_RESET 14 14
The only input device to the circuit, a reflective D9 ARDUINO A0
3 OLED_DC 15 MICRO 15
D10 REF
IRED / phototransistor (“reflective optical sen- 2 OLED_CLK 16 16
D11 3V3
sor” device) like the one at Yourduino [2], is 1 OLED_MOSI 17
D12 D13
17
R5
connected on K2. The LED inside the device is
OLED
390R
39
www.elektor-magazine.com | June 2014 | 15
er
.e l e k
K1.2 K1.1
v i c e.c
D1
Resistors
ww
R4 K2 R1,R5 = 390Ω 5%, 0.25W
Module 1 R3
om
R2 = 10kΩ 5%, 0.25W w
R2 R3 = 100Ω 5%, 0.25W
MOSI SCK C2
IC1
SS MISO
C1
R4 = 22kΩ 5%, 0.25W
TX VIN
RX GND
RST RST Capacitors
C3
GND +5V C1,C2 = 100nF 10%, 100V, 5mm pitch
D2 Arduino C3 = 10nF 10%, 100V, 5mm pitch
D3 Micro S1
D4 A5
D5 A4 Semiconductors
D6 A3 IC1 = 78L05Z
R5
D7 A2 LED1 = LED, yellow, 3mm
D8 A1 LED2 = LED, 3mm, color t.b.d., optional, see text
D9 A0
D10 AREF
D11 3.3V Miscellaneous
D12 D13 MOD1 = Arduino Micro, Farnell/Newark # 2285194
S1 = pushbutton, PCB mount, 6x6x9.5mm
SV1
SV1 = 8-way pinheader receptacle
8 1LED2 K2 = 4-way pinheader receptacle
R1 LED1 K1 = 9V battery connector clip + wires
Reflective Optical Sensor, like TCRT5000
Monochrome 0.96” 128x64 OLED graphic display, Adafruit #
Figure 2. Printed circuit board layout
326 (UG-2864HSWEG01)
designed by Elektor Labs for the project. 34-pin DIP socket for Arduino Micro (DIY from SIL pinheader
receptacles)
PCB # 130470-1
Software
Listing 1. Excerpt from Firmware.ino
The program “Firmware.ino” is recommended
//to be done when when sensor is interrupted: educational reading if you want to understand
void interrupt_rpm_time() all the ins and outs of the control program, and
{ how it got developed and tweaked. The program
// current_interrupt_time = micros();
is richly commended by the author.
// if (micros() - previous_interrupt_time > (1/fMax))
{
Arduino users will know how to transfer the file
current_interrupt_time = (micros() - previous_interrupt_time);
to their Arduino Micro board, starting from file
previous_interrupt_time = micros();
130470-1.zip, which can be downloaded free of
// Serial.print("+");
charge at [3].
// Serial.println(current_interrupt_time);
// digitalWrite(ledPin, HIGH);
} The Arduino program waits for a rising edge at
} its INT1 input and uses five rising edges to com-
pute the average time between them. Using some
math the rpm (revolutions per minute) is calcu-
void calc_run_Time() lated and sent to the OLED display.
{ A snippet of the program code is printed in List-
//calculating running time ing 1. Here we see how the interrupt produced
unsigned long elapsed; by the sensor gets handled, and the Tachometer’s
unsigned long over; internal clock is ticking to display the “up” time.
elapsed=millis();
h=int(elapsed/3600000); Build it, use it
over=elapsed%3600000; The printed circuit board designed by Elektor Labs
m=int(over/60000); has through-hole parts only and its component
over=over%60000;
overlay is given in Figure 2. Let the electronics
s=int(over/1000);
people handle the programming, board stuffing,
ms=over%1000;
soldering and wiring, and the mech people do
}
the mounting of the board in a case, and the
40
16 | June 2014 | www.elektor-magazine.com
Figure 3. The Arduino Micro board secured on to the Figure 4. The Lathe Tacho board has boards fitted at
solder side of the tachometer board (early prototype). either side: an Arduino Micro at the solder side, and
a .96” OLED display at the front. Note the use of SIL
pin strip receptacles to ensure the correct mounting
distance.
Over to you
The project being open in terms of hardware and Web Links
software, there should be nothing in your way to
[1] OLED display: www.adafruit.com/product/326
do custom adaptations like getting that k right. It
will be insightful for sure. The hardware for exam- [2] Reflective Infrared Sensor:
ple may be adapted to support different types http://yourduino.com/sunshop2/index.php?l=product_detail&p=217
of sensor types like Hall effect. Other possible [3] Arduino Micro firmware: www.elektor-magazine.com/130470
applications include a step counter, a rev counter [4] Keith Fenner’s Turn Wright Machine Works on Youtube:
for model planes, a PWM percentage meter, or a www.youtube.com/user/KEF791
simple event driven animation on the display. It
41
www.elektor-magazine.com | June 2014 | 17
3D Pad:
Touchless Gesture
Control interface
point to make the point
42
8 | May 2014 | www.elektor-magazine.com
43
www.elektor-magazine.com | May 2014 | 9
R9 IC6.A
1
3
2 & IC7.C
8
10
Figure 1c.
EN_TOP = ‘1’ 9 &
IC7.D
Detail of the switched ‘1’ 12
11
electrode oscillator circuit 13 &
from Figure 3. Here, the
logic circuit is shown ‘1’
configured with the top
electrode (EN_TOP = 1) 130505 - 15
connected to the oscillator.
multiplexing. The software’s job, at each step the logical AND and NAND operators (IC6, IC7,
in the scanning, is to analyze the oscillator fre- and IC8). You’ll also see that the switching and
quency with each of the electrodes in turn. receiving electrode (EN_X) selection signals come
Figure 1c shows the electrode oscillator in the from the Arduino Uno board (top left). The nom-
state it is at the moment the top receiving elec- inal frequency of our oscillator, in the absence
trode is in circuit. The control signal EN_TOP is of hand influence, is set by R9–R14 at around
high, all the other control signals EN_X are low. 1.6–1.8 MHz.
The three ports IC6A, IC7C, and IC7D now form
just a simple inverter. IC6D is also configured Now that we have an oscillator whose frequency
as an inverter. varies by a few hundred ppm according to the
So this circuit is equivalent to that of the “easily proximity of a hand, all that remains for us to
influenced oscillator” in Figure 1b. With a bit of do is to derive – if possible using a simple solu-
concentration, once you have familiarized your- tion―from that a signal that can be used for a
self with the circuit, you’ll be able to find it in final application. To do this, we need a frequency
the full circuit diagram in Figure 3, by spotting comparator comprising:
external
12 VDC linear 9V 3D Arduino Shield
power supply regulator
préscaler
12V top
phase/frequency
Aalog comparator 9V 5V
Input
buffer
right
center
left
injection
Arduino UNO
SPI reference
DAC SPI VCO
bottom
reference
VCO
USB 5V 9V
serial Port
Figure 2.
130508 - 14
The functions of each of the data to host
three boards.
44
10 | May 2014 | www.elektor-magazine.com
IC5 TP1
VCO_INH 4 5 ENOSC_SENSE 2
2 3 MODE GND
LED1 3 6 SYNC_SCOPE
3 2 13 8
LED2 2 7
4 TX +9V
1 8
5 RX
K4 K5
* IC6.D
47k
82k
22k
13
11
& 12 IC6, IC8 = HEF4011BT
K2 IC7 = HEF4081BT
IC8.A IC9 = 74HC50
MOD2 1 ELEC_BOT 1
3
130508 - 2 2 EN_BOT 2 & IC7.A
1
3 3
IC8.B 2 & IC9.C
4 ELEC_RIGHT 5
4 IC7.B 7 6
EN_RIGHT 6 & 5 1
4
IC8.C
6 &
IC9.D
ELEC_CENTER_BR ELEC_REFB 8
10 9 10
EN_REFB 9 & 1
82k
22k
OSC_SENSE1 2 3 11 12
1 1
13
11
& 12 IC9.F
OSC_SENSE2 4 5 14 15
1 1
IC9.B
+5V
SYNC_SCOPE
VCO_INH
+5V
TP4
C3
C6
R1
CPF_OUT
100n 10k
100n
TP5 16
14 R3 T1
TP7 VDD C1
VDD 12 3 13
Q1 CIN PC2
1M
SCPF_CLEAR
IC2 11 4 1
OSC_SENSE1 1 Q2 VCOUT PP 2n2
CLK 9 5 2
Q3 INH IC1 PC1 2N7002
6 14 6
Q4 SIGIN CX
74HC24 Q5
5
DEM
10 C2
ENOSC_SENSE 2
RST 4 15 +5V
Q6 ZEN
3 12 HEF4046 7 220p
Q7 R2 CX C7
VSS 11 BT 9
R1 VCOIN
7
VSS 100n
4
OSC_SENSE2
R4
120k
8
R7
TP8 VDD
Figure 3.
Full circuit diagram. Top
10k
R5 R2
IC3 1 DAC_CS
R6 SYNC
+5V
470k
6 3 SPI_DATA
10k DOUT DIN
2 SPI_CLK
C5 C4
R8
DAC8311
SCLK
and below it, the electrode
plane. Everything else is
18k
GND
4u7 100n 5
10V
on the Arduino shield. The
130508 - 11 supply voltage comes from
the Arduino Uno board.
45
www.elektor-magazine.com | May 2014 | 11
Figure 4.
The scanning of each
electrode generates a
sawtooth whose amplitude
is an indication of the
capacitance detection.
46
12 | May 2014 | www.elektor-magazine.com
47
www.elektor-magazine.com | May 2014 | 13
Software
The software (sketch) to be downloaded into the
Arduino UNO consists of a file named _3Dpad_sen-
sor.ino and a library ElektorLabs3DPad, to be
installed into the Arduino IDE in the usual way.
This program sends the data to the terminal via
48
14 | May 2014 | www.elektor-magazine.com
the USB port, which is used in COM port mode right), rotating movements with turns counting
(fig. 7). The data sequence contains the four and detection of the rotation direction, and push-
electrode measurement values: ing a virtual button (Push).
And for testing, with the _3Dpad_test.ino file
>IN/OUT EL_Gauche EL_Droite loaded instead of _3Dpad_sensor.ino, we have
EL_Haut EL_Bas < commands that allow us to activate the oscilla-
tors continuously, by choosing the electrode to
IN/OUT indicates when the hand is perceived be used. This lets us check the frequencies and
as being (IN) the detection field or not (OUT). adjust the resistors R9–R14 if necessary. There
The program on the UNO board has two main is also a command to make the VCO work with
functions: on the one hand, the scanning and these set-points: 0, Max and ½ (i.e., at the DAC
sequential measurement of the electrodes, on the output, 0 = 0 V, ½ = 2.5 V and Max = 5 V).
other, the regulation, i.e. maintaining the operat- As a bonus, I am offering a very simple but fairly
ing point, taking the two reference electrodes into comprehensive application program, for PC under
account. The principle is the same as for Touch Windows (XP and 7): this displays the 3D co-or-
detectors, e.g. the ones from Atmel. The level is dinates in the form of a cursor whose position on
regulated outside the detection window, which the screen reflects X and Y, while the Z axis is
makes it possible to overcome slow variations represented by the diameter of the cursor; it also
(mainly caused by temperature changes); then displays the words “Air Swipes” when it detects
when the detection is active, this regulation is a sideways sweep of the hand along one of the
usually blocked. In our case, this implies princi- four axes, or the word “Push” when you make
pally that the VCO set-points for each electrode the movement of pushing a button (Z axis); and
measurement are continuously being corrected lastly, it indicates the rotation direction of the
[8]. I can’t go into a detailed description of the hand or finger it detects and the number of turns
software here – it would take a whole article at (very handy for carrying out settings).
least as long again as this one. However, here It will be easy for you to draw inspiration from
are the successive states of the 3D-Pad: this program in Visual Basic 6 for your own
applications.
Self-calibration: (when first brought into ser-
vice or on command via a serial link) the sys- Set-up
tem seeks the operating point for each electrode, Your Arduino UNO board is operational and you
then memorizes the set-points into EEPROM on have installed the software into the Arduino IDE.
the Arduino. You’ll need a voltmeter, an oscilloscope, and a
frequency meter (the latter is unnecessary if you
Setup: each time it is powered up, or on com- can measure frequencies with the ’scope). Once
mand via a serial link, the system quickly (less the components have been correctly soldered,
than one second) seeks the operating point, start- I recommend the usual checks: orientation of
ing from the set-points read from the EEPROM. the integrated circuits and polarized components,
There are also automatic configuration conditions, examination of the soldering using a magnifier
e.g. in the event of saturation of the electrodes. (especially for the DAC IC3).
Run: the normal operating state, which always The tension mounts: Plug together all the
follows on from a successful configuration. boards to form the stack of PCBs (Figure 6)
then plug the 12 V DC supply into the Arduino
The software’s other tasks are: UNO external power jack. It’s a good idea to
check the power rail voltages: 12 V on K3-1, 9 V
Interpolating the co-ordinates: from the mea- on TP1, and 5 V on TP2.
surement values of the top, bottom, left and
right electrodes, it calculates 3D co-ordinates: All OK? Then upload and run _3Dpad_test.ino.
X, Y, and Z. Open the connection with the terminal set to
115,200 baud; a menu of the commands avail-
Gesture recognition: it recognizes swipes in all able appears.
four directions (upwards, downwards, left, and Let’s check the oscillators are working:
49
www.elektor-magazine.com | May 2014 | 15
VCO: enter the command to activate the VCO via the serial link, the corresponding command
oscillator with a set-point ½: you should see (e.g. “T” = top electrode, or “B” = bottom one)
2.5 V on TP8 and a nice square wave on TP5 at and check the oscillation signal on TP6. The fre-
a frequency of 100–110 kHz (depending on com- quency should likewise be close to 100–110 kHz.
ponent tolerances). The other commands are documented in the soft-
ware source code.
Electrodes: make sure you clear everything
away from around the electrode plane. For each If these checks are satisfactory, load the normal
of the six electrodes, use the terminal to send, program _3Dpad_sensor.ino in place of the test
Component List
3D-Pad Arduino shield
Resistors
SMD 0805, 0.125 W)
r p c bs
R1, R6, R7 = 10kΩ to
er
.e l e k
R2, R8 = 18kΩ
v i c e.c
R3 = 1MΩ
R9,R12 = 47kΩ
ww
om
R4 = 120kΩ w
R5 = 470kΩ
R10,R13 = 82kΩ
R11,R14 = 22kΩ
R15,R16 = 470Ω
Capacitors
default: SMD 0805
C1 = 2.2nF
C2 = 220pF
C3,C4,C6,C7,C8,C9,C10,C11,C15,C16,C17,C18 = 100nF
C5 = 4.7µF 16V (pitch 2mm)
C12 = 100µF 16V (pitch 3.5mm)
C13, C14 = not fitted
Semiconductors
IC1 = HEF4046BT
IC2 = CD74HC4024M
IC3 = DAC8311 ou AD5641AKSZ
IC4 = CD4504BM
IC5 = 78L09 (SOT-89)
IC6, IC8 = HEF4011BT
IC7 = HEF4081BT
IC9 = CD74HC4050M
T1 = 2N7002
Miscellaneous
K5, K6 = 8-pin pinheader*
K3, K4 = 6-pin pinheader *
K1, K2 = 4-pin pinheader *
K7, K8 = 2-pin pinheader
Electrode Plane
Semiconductors
LED1,LED2 = LED, green, SMD, Kingbright type KPT-2012SGC
Miscellaneous
K1,K2 = 4-pin pinheader*
K3, K4 = 2-pin pinheader*
PCB # 130508-21
* 0.1’’ pitch
50
16 | May 2014 | www.elektor-magazine.com
program. A good way of checking the 3D-Pad in scrolling on the terminal [9]. You can launch a
operation consists in connecting a ’scope probe to reconfiguration at any time, either with the com-
TP8 (DAC output) and another to TP4. The sync mand “R” from the terminal, or by saturating the
is taken from TP8. At first switch-on, or follow- electrodes by bring your hand flat within a few
ing the self-calibration command (send “A” from millimeters of the electrode plane.
the terminal), the voltage level on TP8 will fall
gently: this is the search for the set-points for I’ll be there to help you if you need help getting
each electrode. After a few moments, this trace it going or information for a specific application,
should be stabilized (end of self-calibration) and or if you have any suggestions for developing the
your oscilloscope screen should look like the one project. Don’t hesitate to contact me on Twitter
shown in Figure 4b. Bring your hand closer to the [10], I’ll be delighted to answer you personally.
electrode plane: you should see the sawtooth on (130508)
TP4 change and also the measurement values
Web Links
[1] games
Fruit Ninja: http://fruitninja.com/
Despicable Me - Minion Rush: https://play.google.com/
[2] CC BY-NC-SA 4.0 http://creativecommons.org/licenses/by-nc-sa/4.0/
[3] www.embedded.com/design/prototyping-and-development/4008781/
Getting-in-touch-with-capacitance-sensor-algorithms
[4] injection locking: http://en.wikipedia.org/wiki/Injection_locking
[5] www.elektor.com/arduino
[6] assembled, tested UNO board from the Elektor e-shop
www.elektor.com/arduino
[7] www.elektorPCBservice.com
[8] www.rtcmagazine.com/articles/view/101626
[9] video of the sawtooth signal on TP4 http://youtu.be/rYdyR49qFzU
demo video: http://youtu.be/11QGUxXFYq8
[10] @junowhynot + #3DpadElektor: https://twitter.com/junowhynot
51
www.elektor-magazine.com | May 2014 | 17
Microcontroller
BootCamp (1)
Arduino and Bascom
By Burkhard Kainka
(Germany)
You might ask why you should use a microcon- For comparison: the NE555 timer IC
troller when it’s possible to do so much with First of all there’s the output. When you look
ordinary analog electronics. At first glance, this at the internal block diagram of the NE555 on
looks like a good question. New microcontrollers the data sheet (Figure 1), you can see that it
with more features, higher performance, higher has a push-pull output stage that can actively
clock rates and even more memory are appear- switch high and low. Microcontrollers have exactly
ing all the time, but the first demo program for the same kind of outputs. They are called ports,
every one of them invariably makes a LED blink. where the name “port” can stand for a set of
This leads to the justified criticism that you could outputs or for pins that can be configured either
get the same result by simply taking an NE555 as inputs or as outputs. The circuitry connected
timer IC and adding a couple of resistors and a to the output for a LED blinker application is also
capacitor. That’s absolutely right, and the com- the same: a LED with a series resistor, connected
parison is better than you might think because either to ground (GND) or to the supply voltage
a lot of the elements of an NE555 timer IC can (Vcc). The NE555 also has a second output driven
also be found in a microcontroller. by an open-collector transistor, which can only
52
30 | April 2014 | www.elektor-magazine.com
switch something to ground. Many microcontrol- the other hand, an analog electronics solution is
lers can also emulate this function. Actually the a better and more economical choice for quite a
only difference is that microcontrollers usually few simple tasks.
have several outputs but the NE555 has only one,
since the push-pull output and the open-collector Anyone with a bit of experience in microcon-
output are not independent. troller development can also mention another
advantage of microcontrollers: once the circuit
Next we have the inputs of the NE555, which is complete, you don’t need to touch the solder-
consist two inputs to a comparator that controls ing iron again. From that point on, all you do is
an internal flip-flop. A typical application for this write and test code. Changes to device functions
is an astable multivibrator, which hobbyists often can be implemented and tested very quickly.
call a blinker circuit. You can put all this together Microcontrollers are general-purpose and versatile
in almost no time. You just plug the numbers computation workhorses. The learning curve is
into a couple of formulas to determine the right worth the effort, since you ultimately save time.
component values, and then the NE555 does Your first exposure to a microcontroller data
exactly what you want. Determining the compo- sheet may put you off, since it can easily amount
nent values for an NE555 circuit and program- to 300 pages or more. Fortunately, there are
ming a microcontroller are actually comparable
tasks. There’s another thing that’s very similar:
both devices (NE555 and microcontroller) have
VCC
a Reset input that you can use to set everything
RESET
back to the starting point. And with both devices 4 8
VCC
the Reset input is usually high in the quiescent
state and must be actively pulled to ground.
5k
DIS-
THRESHOLD 6 INHIBIT/ 7
Figure 2 shows a simple square-wave genera- R
RESET CHARGE
CONTROL 5
tor in the form typically used as an LED blinker. VOLTAGE
DIS
controller are summarized in Table 1. You could
3
OUT
formulate the result of a fair comparison as fol- 9V 6 NE555
THR
lows: The microcontroller has a bit more of 2 TR
1k
CV GND
everything and is therefore generally the better
5 1
choice for complex tasks. For example, a single LED
53
www.elektor-magazine.com | April 2014 | 31
54
32 | April 2014 | www.elektor-magazine.com
1k
4 11
D2 D5
on a display, and much more. However, perhaps 5
D3 D4
6
55
www.elektor-magazine.com | April 2014 | 33
56
34 | April 2014 | www.elektor-magazine.com
for them will be a frequent topic in this series on doesn’t work in practice, and likewise a micro-
microcontroller programming. controller with nothing in its program memory
cannot program itself. However, this is possible if
You can type in the program in Listing 1 yourself a boot loader is already present in memory, and
or download it from the Elektor page. The file the Arduino comes with a built-in boot loader.
UNO_LED1.bas is located in the zip folder [3]. The development environment on the PC also has
Compiling the program is very easy: simply click to be able to download program code using the
Program/Compile, click the corresponding icon Arduino boot loader. Fortunately, Bascom devel-
on the toolbar (a black IC), or press the F7 key. oper Mark Alberts already guessed that some-
If there is any sort of error in the source text, one would want to program an Arduino board in
an error message will be displayed. If there are Bascom at some point in time, so this capabil-
no problems, a pop-up window shows what per- ity is already incorporated. After you configure
centage of the memory is occupied. In this case the right settings, everything is quite easy. Here
it is less than 1%, which is rounded down to 0%. we describe how this works with the demo ver-
What matters is the resulting hex file UNO_LED1. sion, since there is a small difference with the
hex or the binary file UNO_LED1.bin, which are full version.
two different file formats with the same con-
tent. They contain the executable code for the
microcontroller. Now you have to load this code
into the microcontroller’s flash memory. There
are many ways to do this, and for now we only
describe the simplest way. Other options will be
described in subsequent instalments.
57
www.elektor-magazine.com | April 2014 | 35
58
36 | April 2014 | www.elektor-magazine.com
Microcontroller
BootCamp (2)
Digital inputs
By Burkhard Kainka
(Germany)
When a microcontroller has to do more than just blindly follow a predefined pro-
cess, it needs additional information while the program is running. Inputs enable
microcontrollers to detect external events and respond accordingly.
Microcontrollers, including the ATmega328 on be used to measure voltages, and digital inputs,
the Arduino board, have many different types which can only detect whether a signal level is
of input. They include analog inputs, which can high or low. This can be used to read the state of
a button or switch, among other things.
Digital inputs
IN Each port pin of the ATmega328 can be configu-
red as an input or as an output, as mentioned in
+5V
LED the previous instalment of this series [1]. Here
100k
B5
B4
B3
B2
B1
GND
AREF
AVCC
D5
D6
D7
B0
X1
X2
59
34 | May 2014 | www.elektor-magazine.com
log inputs, so the corresponding terminals on the diodes only start conducting at –0.6 V and +5.6 V
Uno board are labelled A0 to A5. Port pin PC5 is (assuming a supply voltage of 5 V).
connected to A5 at the edge of the board diago-
nally opposite the USB connector, which makes Reading input states
it easy to find. That’s actually why we chose this The program UNO_Input1.Bas in Listing 1
particular port pin for our first program. Figure 2 (the code can be downloaded from the Elektor
shows all the pins of the ATmega328 with their website [2]) reads the state of the signal on
official pin names (PC6, PD0, etc.), the special port pin PC5 and outputs it to the LED on port
functions of individual pins (which we will discuss pin PB5. First all pins of port C (PC0 to PC5)
in due course), and the abbreviated Arduino pin are initialized as digital inputs. In the interest of
names (at the outside edges). readability, the instruction Config Portc = Input
After a restart or reset, every pin of port C is is used for this purpose. Actually you could omit
configured as a high-impedance CMOS input with this instruction, since as previously mentioned
the same properties as the familiar 4000 family port C is automatically configured with all pins
of CMOS ICs. Accordingly, it’s not a bad idea to as inputs after power-up.
use 100 kΩ resistors here for protection against ATmega microcontrollers have output registers that
excessive voltages and static discharges. determine the states of the output port pins. When
you program the software to write a particular
Protection diodes value to an output register, the corresponding port
All modern CMOS devices have a pair of protection pins are set to 0 or 1 according to the content of
diodes on each input, with the one connected to the register. There is a separate input register that
ground and the other to Vcc. Their job is to limit holds the actual states of the pins. This register
excessive voltages. The first CMOS ICs in the
4000 family, which came on the market a long
time ago, did not have input protection diodes.
Premature IC failures due to improper handling ATmega328p
were therefore fairly common at that time. Static
RESET (PCINT14/RESET) PC6 1 28 PC5 (ADC5/SCL/PCINT13) A5
discharges from people to grounded objects at 0 (PCINT16/RXD) PD0 2 27 PC4 (ADC4/SDA/PCINT12) A4
voltage levels from 100 to 1000 V happen all the 1 (PCINT17/TXD) PD1 3 26 PC3 (ADC3/PCINT11) A3
time, but MOSFET gates don’t like them at all 2 (PCINT18/INT0) PD2 4 25 PC2 (ADC2/PCINT10) A2
and may break down at voltages as low as 50 V. 3 (PCINT19/OC2B/INT1) PD3 5 24 PC1 (ADC1/PCINT9) A1
Now that protection diodes are integrated in all 4 (PCINT20/XCK/T0) PD4 6 23 PC0 (ADC0/PCINT8) A0
5V VCC 7 22 GND GND
ICs, failure due to static discharge breakdown
GND GND 8 21 AREF AREF
has become rare. You can detect these protection
(PCINT6/XTAL1/TOSC1) PB6 9 20 AVCC 5V
diodes with an ordinary ohmmeter. This is often
(PCINT7/XTAL2/TOSC2) PB7 10 19 PB5 (SCK/PCINT5) 13
very useful because it is an easy way to check 5 (PCINT21/OC0B/T1) PD5 11 18 PB4 (MISO/PCINT4) 12
the connections to the pins of an IC in a circuit 6 (PCINT22/OC0A/AIN0) PD6 12 17 PB3 (MOSI/OC2A/PCINT3) 11
to see whether or not they are okay. 7 (PCINT23/AIN1) PD7 13 16 PB2 (SS/OC1B/PCINT2) 10
The protection diodes are what make it safe to 8 (PCINT0/CLKO/ICP1) PB0 14 15 PB1 (OC1A/PCINT1) 9
touch an open CMOS input with your finger through 130568-12
a 100 kΩ resistor, despite the fact that your body
may have static charges and carry induced AC
voltages up to 100 V under no-load conditions. Figure 2.
The human body is a sort of node with numerous The ATmega328 pinout and
capacitive links to all sorts of cables and power the corresponding Arduino
outlets. As audio designers and troubleshooters names.
know, a finger is an excellent source of a hum Rpu
signal. The protection diodes (Figure 3) limit the
Logic
high-amplitude sinusoidal signal to a low-amplitude PXN
Cpin
square wave (5 V peak to peak). You can see See Figure
'General Digital I/O'
this for yourself by measuring the signal with an for Details
oscilloscope. Actually the amplitude of the square Figure 3.
130568-13
wave is 6.2 V peak to peak, since the protection Input protection diodes.
60
www.elektor-magazine.com | May 2014 | 35
can be read by the software to determine whether the state of a single port pin.
the pins are high or low. Each register holds one In the case of port B, the output register (write
byte (eight bits) with a value range from 0 to 255 register) is called PORTB and the input register
(see the Bits and Bytes inset). Each bit shows (read register) is called PINB. The software
sees these registers as memory locations that
can be read and written. However, in hardware
terms they are physical circuits with external
Listing 1. Copying an input to an output.
connections. There is another register for each
'-------------------------------- port called the Data Direction Register, which
'UNO_Input1.BAS allows the port pins to be configured as inputs
'------------------------------- or as outputs. For port B, this register is called
$regfile = "m328pdef.d 'ATmega328p DDRB. There is no obvious way to write data to
$crystal = 16000000 '16 MHz this register in a Bascom program, since this
operation is performed indirectly by the Config
instruction. For example, Config Portb = Output
'-------------------------------
sets all bits of DDRB to 1, which causes all pins
of port B to act as outputs.
Config Portb = Output
If you run the program and connect the input to
Config Portc = Input
ground, the LED will be off (dark). If you connect
the input to +5 V, the LED will light up. As you
Do can see, the input state is copied to the output.
Portb.5 = Pinc.5 'Copy Input to Output Now try something different: first make sure that
Waitms 19 '19 ms you are standing or sitting or standing completely
Loop insulated from any conductive objects, and then
touch the input with your finger. Remarkably
61
36 | May 2014 | www.elektor-magazine.com
For comparison (and for those of you who have forgotten Now for a question: what is the largest number that can
your first-grade math): our usual number system with be represented by a byte? If you said ‘255’, you’re right.
base 10 comes from the more or less random biological Once you know this, the world of numbers is an open book
circumstance that we have a total of ten fingers and thumbs. for you and it is no longer a mystery why you can replace
The weights of the individual digits are 1. 10, 100, 1000 and the instruction Portb = Output in Bascom with the simple
so on. instruction Ddrb = 255. This says that all bits in the data
The eight bits of a byte can be divided into two sets of direction register for port B (DDRB) are set to 1 and all pins
four bits. Each set of four bits can then be expressed from PB0 to PB7 are outputs.
enough, you will see that the LED blinks. At first value where the pin is guaranteed to be read as
this may seem very strange, but when you think high is 0.7 Vcc (corresponding to 3.5 V) and the
about it the reason becomes clear: a stroboscope maximum value where the pin is guaranteed to
effect. Your finger couples a signal into the input – read as low is 0.3 Vcc (corresponding to 1.5 V).
the well-known 50-Hz AC hum signal with a period That sounds a bit like a disclaimer intended to
of 20 ms, or 16.6 ms for 60 Hz. By contrast, prevent unjustified complaints about supposedly
the wait time in the program is 19 ms. The 5% incorrect operation: if you connect a signal
difference between the two times yields an output between 1.5 and 3.5 V to an input, whatever
signal with a frequency of 2.5 Hz (50 Hz x 0.05). happens is your responsibility. But what actually
If you live in an area where the AC line frequency happens in that case? The only way to find out is
is 60 Hz, the output frequency—and thus the to simply do it and observe the results.
blinking rate—is correspondingly higher. Go ahead, try it! Use an external power supply for
this so that Vcc is exactly 5 V, since the voltage
What makes a high level high? available from a USB port is usually only 4.5 V or
Like all digital circuits, microcontroller output pins so. Connect a potentiometer as shown in Figure 4
have only two states, which are variously called and vary the input voltage over the range of 0
on and off, high and low, 1 and 0, or whatever. to 5 V. Here’s what you should measure with the
Furthermore, microcontrollers can only detect microcontroller: If you gradually raise the voltage
these two signal states on their input pins. from 0 V, the output state changes from low to
However, in practice any voltage from 0 V (ground) high at 2.7 V and the yellow LED lights up. If you
to the supply voltage (Vcc) can be applied to an then slowly reduce the voltage, the output state
input pin. You might think that anything above changes back to low at 2.2 V. You can repeat
2.5 V is a high level and anything below 2.5 V is this process as often as you like. The switching
a low level. However, the ATMega328 data sheet points are 0.5 V apart, and no change occurs in
states somewhat circumspectly that the minimum the range between 2.2 and 2.7 V. This means
62
www.elektor-magazine.com | May 2014 | 37
1k
10k it means that the inputs act like Schmitt triggers.
As you may know, the purpose of Schmitt triggers
28 27 26 25 24 23 22 21 20 19 18 17 16 15 is to convert analog signals with variable voltage
levels into digital signals.
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
GND
AREF
AVCC
ATmega328p Switching back and forth
GND
VCC
RES
D0 You can also automate the above process and
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 dispense with the potentiometer. You already
have the necessary hardware in the form of the
16MHz microcontroller, and the idea is to have it measure
100n
its own switching points. In the circuit in Figure 5,
22p 22p the capacitor connected to the input pin is charged
130568-14
Figure 4. by the output pin through a series resistor. The
Measuring the input voltage. output should go to 0 when a 1 is read at the input,
and it should go to 1 when a 0 is read at the input.
This circuit is effectively a sort of on/off controller
Figure 5. with hysteresis. You need to change the program
A control loop. slightly for this purpose, as shown in Listing 2.
Scope 10k...1M The Not function inverts a digital state, in the same
V way as an inverter gate in hardware logic. The
1k
100n...100u
LED wait time is not necessary because the program
+5V
should respond immediately when a new state
is detected. In this case, “immediately” means
within a microsecond or so.
28 27 26 25 24 23 22 21 20 19 18 17 16 15
The potentiometer of the previous circuit is
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
GND
AREF
AVCC
D5
D6
D7
B0
X1
X2
63
38 | May 2014 | www.elektor-magazine.com
with a frequency of approximately 500 kHz. This An interesting aspect in this regard is that your
means that the circuit responds to changes in the finger acts like a high (logic 1) signal. This means
input state roughly once per microsecond. that if you touch the input pin when nothing else
There’s only one strange thing about this circuit: is connected to it, the LED will blink at a steady
with a symmetrical 500-kHz square-wave signal, rate. This results from the Wait instruction in the
the yellow LED on the Arduino board should be lit If block. When the program sees a 0 level on PC5,
at half its normal brightness, but it remains dark. it immediately jumps to End If and then back to
This reason for this can only be determined by the start of the loop. This means that a 0 level
examining the circuit diagram of the Arduino Uno causes the If condition to be tested repeatedly in
in more detail. The built-in LED on the Uno board rapid succession. If you apply a 50-Hz signal to the
is connected to PB5 through a buffer consisting input by touching it with your finger, after at most
of one half of an LM358 dual opamp. This is 10 ms a 1 level is detected at the input. Then the
unusual on a digital circuit board, and it comes
from the fact that the other half of the LM358 is
used as a comparator for the supply voltage. If Listing 2. Inverting the input signal.
you connect an external power supply, the Uno '--------------------------------
board automatically disconnects the USB supply 'UNO_Input2.BAS
voltage—very user-friendly and reliable. The other
'--------------------------------
opamp is redundant and actually shouldn’t be
$regfile = "m328pdef.dat" 'ATmega328p
used, but the designer took advantage of its high
$crystal = 16000000 '16 MHz
input impedance to use it as buffer that allows port
pin PB5 (Arduino pin 13) to also be used as an
input. However, a 500-kHz signal is simply outside '-------------------------------
the frequency range of an ordinary opamp, so
the LED on the board remains dark. If you want Config Portb = Output
to actually see something, you have to connect Config Portc = Input
your own LED to the board as shown in Figure 1.
Do
Branching Portb.5 = Not Pinc.5 'Inverted Input to Output
Microcontrollers can make decisions by following Loop
program branches. In practical terms, this means
that parts of a program are either executed or not
executed, depending on specific conditions. The
program structure used for this in Bascom is the If Listing 3. An If block.
block, which takes the form If <condition> Then '--------------------------------
<instructions> End If. The instructions inside the
'UNO_Input3.BAS
If block are executed if the condition is fulfilled,
'--------------------------------
and otherwise these instructions are not executed
$regfile = "m328pdef.dat" 'ATmega328p
and the program jumps directly to End If.
In the program in Listing 3 you can also see $crystal = 16000000 '16 MHz
another new instruction called Toggle, which '-------------------------------
means “switch to the opposite state” and
causes the output to change state each time
the instruction is executed. The blinker runs as Config Portb = Output
long as the signal level on port pin PC5 is logic 1, Config Portc = Input
which means 5 V or at least something higher
than 2.7 V. When the input pin is connected to Do
ground, which corresponds to logic 0, the LED If Pinc.5 = 1 Then
remains in its current state – either constantly
Toggle Portb.5
lit or constantly dark.
Waitms 250
The circuit is the same as in Figure 1. Here as well,
End If
you can test the circuit by connecting the input to
ground or +5 V, or by touching it with your finger. Loop
64
www.elektor-magazine.com | May 2014 | 39
case the LED will not blink – but you can’t say in
+5V advance whether or not the LED will blink. If you
hold your hand close to the input and move your
LED feet, the static charge on your body can cause
10k the port to change states as a result of capacitive
1k
coupling through the air. An outside observer who
doesn’t know all the details might start looking for
28 27 26 25 24 23 22 21 20 19 18 17 16 15
a pesky intermittent contact, and perhaps start to
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
GND
AREF
AVCC
doubt the reliability of microcontrollers in general.
ATmega328p Many experienced designers have learned about
this the hard way by spending hours looking for a
GND
VCC
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 suspected software bug or hardware error, when
the actual problem was an open input.
16MHz
Reading switch states
100n
22p 22p
with a pull-up resistor
130568-17
Figure 7. From the above, we can conclude that open inputs
A pull-up resistor. cause problems. Suppose you want to read the
state of a switch. This means that the input must
LED changes state and the program waits 250 ms have a defined state when the switch is open. The
before testing the If condition again. usual way to achieve this is to use a pull-up resistor,
This aspect of the program also shows you what which is a resistor connected to Vcc to pull the input
you shouldn’t do. Open inputs are bad news and high when it is open. If the microcontroller sees
cause nasty problems, especially if you overlook a low level at the input, it knows that the switch
them. In the case of this simple program, it’s connected to the input is closed.
totally unpredictable what will actually happen The pull-up resistor in Figure 7 is shown connected
when nothing is connected to the input. The input by dashed lines, which means that the resistor
may be at the high level, in which case the will is optional. This is because the microcontroller
LED blink, or it may be at a low level, in which has built-in pull-up resistors; all you have to do
is connect them. Many microcontrollers have a
separate register for this purpose. However, AVR
Listing 4. Polling a port with a pull-up resistor.
microcontrollers manage to handle everything
'-------------------------------- with a total of three registers: PORTC, PINC and
'UNO_Input4.BAS DDRC. In this case you set all bits of DDRC to
'-------------------------------- 0, which configures all pins of port C as inputs.
$regfile = "m328pdef.dat" 'ATmega328p You also set all bits of PORTC to 1, as though you
$crystal = 16000000 '16 MHz
wanted to set all the pins to high outputs. What
actually happens in this case is that each port pin
'-------------------------------
is connected to Vcc by an internal pull-up resistor
(marked “Rpu” in Figure 3) with a resistance of
Config Portb = Output
approximately 30 kΩ. This causes the input pins
Config Portc = Input to have a high signal level and a relatively low
Portc.5 = 1 'Pullup input impedance, instead of a high impedance. As
a result, all bits in PINB will be read as 1 unless
Do they the corresponding pin is connected to ground
If Pinc.5 = 1 Then by an external switch.
Toggle Portb.5 In Listing 4 you only need the instruction Portc.5
Waitms 250 = 1, which connects the internal pull-up resistor
Else to pin PC5. The other inputs of port C are left
Portb.5 = 0
in the high-impedance state without a pull-up
resistor. That doesn’t matter here because they
End If
are not used in the program. With this change,
Loop
the program response is unambiguous. An open
65
40 | May 2014 | www.elektor-magazine.com
input is always read as a high level (logic 1), and and 50 kΩ. To check this, connect an ammeter to
the LED blinks. Another change is necessary to the input pin instead of the switch, or in parallel
ensure that there is no ambiguity when the switch with the switch. In our case, we measured
is closed. This consists of adding an Else section 140 µA. This corresponds to a resistance of
to the If block. The instructions between Then and 35.7 kΩ (5 V ÷ 0.14 mA), which is exactly in
Else define what has to be done when the switch is the middle of the range specified by Atmel.
open, and the instructions between Else and End (130568-I)
If define what has to be done when the switch is
Web Links
closed. In this case, the LED should be off then.
By the way, you might want to measure the actual [1] www.elektor-magazine.com/120574
value of the internal pull-up resistor. The data [2] www.elektor-magazine.com/130568
sheet says that it is somewhere between 20 kΩ
Latch-up
You can use the input protection diodes of If your body has a static charge and you
the port pins intentionally in your circuits touch an IC input, the current pulse may
to limit input voltages higher than Vcc. To be large enough to trigger latch-up. Many
30k
take an RS232 interface as an example: modern ICs can withstand static discharges
the signal voltage on the output line of a IN up to 15 kV, based on a human body model
PC serial port is often –12 V and +12 V, with a contact resistance of 1 kΩ. In that
100n
or in some cases somewhat less. This is a case the ignition threshold is about 15 A,
complete mismatch to CMOS inputs with 5 V and you can certainly feel static discharges
signal levels. The quick and dirty solution of that magnitude. However, no IC can
is to connect the signal line directly to the survive contact with a 12 V power lead
30k
input through a protective series resistor lying loose on the bench, especially if the
(value 10 kΩ or 100 kΩ) as illustrated in IN 1u power supply has a hefty output capacitor.
Figure 1. Here the protection diodes take If the power lead touches an IC pin, the
100n
care of the rest. microcontroller is guaranteed to be dead.
However, there’s one thing you have to Have a look at the circuit diagram in the
watch out for: the current through the above figure. Do you see anything to be
protection diodes should never exceed a worried about? To all appearances, the
30k
few milliamperes. This is because every only thing the capacitor on the input does
10k
CMOS IC has a parasitic thyristor that can IN
is to debounce the switch contacts. This
be triggered unintentionally. It basically is common practice to ensure that the
100n
consists of an NPN transistor and a PNP microcontroller only sees a single level
transistor, which are a sort of undesirable 130568-18 change when the switch contacts bounce
side effect of the CMOS structure. This back and forth a few times. What the circuit
thyristor can be triggered by the current diagram doesn’t show is the length of the
through the protection diodes if it rises above a certain wires and their inductance. If you imagine an inductor in the
value, which might be 100 mA or as high as 1 A —but you circuit as shown in the middle diagram, you have something
never know the precise value. The worst thing about this is that looks a lot like Marconi’s spark-gap transmitter. When
that even very short current spikes can trigger the thyristor. the switch is closed, there is an excited resonant circuit that
After the device enters the latch-up state, what happens oscillates at 500 kHz, assuming a capacitance of 100 nF
next depends on how much current the power supply can and an inductance of 1 µH There’s also enough energy
deliver. The latched CMOS IC draws as much current as it available, since the capacitor has previously been charged
can and gets blistering hot. So if you get your fingers get through the pull-up resistor. The first peak of the oscillation
burned, switch off the power right away and wait a few waveform hits the protection diode with full force, and under
minutes. There’s still a small chance that the IC will have unfavorable conditions it can trigger that nasty thyristor.
survived, but in most cases it’s game over. If you measure The circuit diagram at the bottom shows how you can
the resistance between the Vcc and ground pins, you’ll only prevent this. All you need is a current limiting resistor that
read a few ohms. soaks up the excess energy.
66
www.elektor-magazine.com | May 2014 | 41
Microcontroller
BootCamp (3)
Serial interface and A/D converter
This instalment focuses on the analog
to digital converter, which is effectively
a voltmeter built into the microcontrol-
ler. However, it does not have a pointer
or a display, so you have to do a bit
of work to be able to read the mea-
surements. The A/D converter simply
supplies data. In order to be displayed
somewhere, this data has to be trans-
mitted. That’s where the serial interface
comes into play.
By Burkhard Kainka Although most PCs nowadays have dispensed with Instead of this, the Arduino board has a serial
(Germany) the traditional serial interface, it is still widely to USB converter that sends the data over USB.
used in the microcontroller world. This interface This means that you need a driver for a virtual
was originally developed to transfer serial data serial port (such as COM2) on the PC. For pro-
over a conductor. The individual bits obediently grams running on the PC, that makes the data
travel down the wire one after the other. The bit transported over USB look like it entered the PC
timing is precisely defined so that the receiver through a conventional serial port. This data can
can make sense of the serial bit stream. A com- therefore be displayed using a standard terminal
monly used data rate is 9600 bits per second, emulator program.
which is also called 9600 baud. In addition to the We recommend that you use the terminal emu-
eight data bits of each byte, there is a start bit lator program integrated into Bascom. You can
and one or two stop bits. Each data bit is pres- open it under Tools > Terminal Emulator or with
ent on the wire for an interval of approximately the key combination Ctrl-T, but you have to con-
100 µs (at 9600 baud). figure the right settings to make it work prop-
erly. In particular this means selecting the right
Print output COM port (the same one you configured for the
Every ATmega microcontroller has a serial inter- boot loader) and setting the right baud rate (in
face (UART) with RXD (PD0) and TXD (PD1) lines. this case 9600). For the rest you can use the
These signals are TTL compatible, which means default settings: 8 data bits, 1 stop bit and no
that the signal level is 5 V in the quiescent state parity (Figure 1).
and 0 V in the active state. By contrast, a COM Now you have to persuade the Arduino Uno to
port on a PC operates with RS232 signal levels. transmit something. That’s easy in Bascom, thanks
They are -12 V in the quiescent state and +12 V to the Print command. See Listing 1 for an exam-
in the active state. For this reason, a periph- ple of how to use the Print command (note that all
eral interface IC such as the MAX232 is often code files can be downloaded at [1]). This com-
necessary to invert and adapt the signal levels. mand can be used to send output to a printer,
67
30 | June 2014 | www.elektor-magazine.com
which explains its name. However, to avoid wast- be a problem – namely when you have inadver-
ing paper it’s better to send text and data to the tently chosen a number type that is too small
monitor. The result is shown in Figure 2. and the overflow is not detected.
There are two Print commands in the program. There’s another detail that should be noted here:
The second one sends a message in quotation why does the display always go to a new line after
marks: “Uno”. But first a number is sent – more “Uno”, but not after the number? The answer is
particularly, the number N. If it’s been a while
since you had anything to do with math, you may
still have a vague memory that unknown quan-
tities in algebra are often designated as x. You
could do the same in Bascom, but in situations
where numbers are simply incremented (1, 2, 3
and so on), the symbols N, M, I and J are com-
monly used in source code. However, you are
free to choose your own symbols for variables
of this sort. The only thing that really matters is
that you first tell Bascom what type of number
each variable represents. This is because Bas-
com has to know how many bytes of memory the
Figure 1.
number needs and how computations should be Terminal emulator settings.
performed with the number in the code. For this
reason, the variables must be declared (dimen-
Listing 1. Print output
sioned) before they are used. That’s the purpose
of the following instruction: Dim N as Byte. This ‘------------------------------------
means that N is a number of type Byte, which ‘UNO_Print.BAS
clearly indicates that this number consists of eight ‘------------------------------------
bits (recall the previous instalment) and has a $regfile = “m328pdef.dat”
number range from 0 to 255.
$crystal =
$baud = 9600
Assignments
Now you can perform calculations with the num-
Dim N As Byte
ber variable N. The variable is assigned the value
0 at the start of program execution. Although
you might think this is only logical, it is actually Do
a particular feature of Basic. In other languages Print N;
you have to write ‘N = 0’, but in Bascom this Print “ Uno”
is done automatically. The value of N is subse- Waitms 200
quently incremented by 1 each time the loop is N = N + 1
executed. My old math teacher would have boxed Loop
my ears if I wrote ‘N = N + 1’, because it is not
valid mathematical equation. However, in many
programming languages the equal sign is used
as an assignment operator. The intention here
is that the variable N is assigned a new value by
adding 1 to the existing value.
You can see the results of this ongoing incre-
menting in the terminal window: the value of N
increases by 1 after each output. You can also
see something else: the value stops rising after it
reaches 255, since we defined N as a single-byte
number. This means that 255 + 1 is not 256, but Figure 2.
instead 0. This is called number overflow. Here Print output transmitted
this is not a problem, but in other places it can over a serial line.
68
www.elektor-magazine.com | June 2014 | 31
10k
V are sent to the serial to USB converter on the
Uno board, can easily be seen with an oscillo-
scope. Simply touch the scope probe to the TX
pin of the microcontroller, which is routed to the
28 27 26 25 24 23 22 21 20 19 18 17 16 15
socket header at the top right on the Arduino
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
board. Then you can see the data stream. The
ATmega328p RX pin is next to the TX pin, and if you type some
characters on the terminal you can see them
GND
VCC
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
on the RX line. This shows that the PC can also
1 2 3 4 5 6 7 8 9 10 11 12 13 14
send data to the microcontroller. At this point
the received data does not do anything because
16MHz
the program ignores it.
Config Adc = Single , Prescaler = 64 , Reference = Avcc '5 V Config Adc = Single , Prescaler = 64 ,
Reference = Avcc
Do
D = Getadc(0) This initializes the ADC, which means it enables
Print D the ADC with specific attributes. Here Single mode
Waitms 200 means that only one measurement is made for
Loop
each request. The other option is Free (short
for free-running mode), in which measurements
69
32 | June 2014 | www.elektor-magazine.com
70
www.elektor-magazine.com | June 2014 | 33
71
34 | June 2014 | www.elektor-magazine.com
External programmer
When you want to use a microcontroller, you usually need can always restore everything to the original state if you do
some sort of programming device. This is not necessary something really wrong, such as accidentally deleting the
with the Arduino because it boot loader.
has a built-in boot loader,
as described in the first part You can also use the
of this series. However, you programmer to read out the
do not have a boot loader content of the flash memory,
when you buy a new AVR for example in order to make
microcontroller and fit it on a backup copy (including the
your own PCB, so you have boot loader) that you can
to use a programmer. One use to restore the Arduino
low-cost option is the Atmel if necessary. A backup copy
ISP mkII. It can be used (UnoBoot.hex) [1] is also
with the free AVR Studio 6 available in the Elektor
development environment. However, there are lots of other archive in case you need it.
programmers that can do the same job. In order to load your own program into the microcontroller,
you first have to load the hex file. In this case you must
A six-pin ISP connector is normally tick the option ‘Erase chip before
used for in-system programming programming’ to ensure that the
(ISP) of AVR microcontrollers. boot loader is deleted. This means
Many microcontroller boards, you have to decide whether or not
including the Arduino, have a you want to use the boot loader.
suitable 6-pin header for this. The You can also opt to use another
SPI interface, which consists of boot loader, such as the MCS boot
serial data streams (MOSI to the loader. We’ll describe how that
microcontroller and MISO back to works in one of the upcoming
the programmer) and a clock signal instalments.
(SCK), is often used to transmit the
programming data. This interface You can also view the
will be described in more detail in a microcontroller fuse settings with
later instalment. The pinout of the Bascom or in Atmel Studio 6.
ATmega328 is: By the way, the term ‘fuse’
comes from the early days of
1 MISO, PB4 microcontroller technology when
2 Vcc there was program memory in
3 SCK, PB5 which links were literally burnt
4 MOSI, PB3 through to program the memory
5 Reset once and for all. The modern
6 GND fuses in ATmega microcontrollers
are actually flash memory cells
The Arduino Uno board has two ISP that must be configured to define
ports for external programmers. specific basic settings. Among other
The one close to the USB connector things, they allow you to switch
is best ignored, since it is used to back and forth between the internal
program the USB to serial converter clock and an external clock source.
described in the body of this article. The screenshot shows the factory
The ISP port for the ATmega328 is default fuse settings of the Arduino
located at the edge of the board. Uno board. You can see that the
It’s nice to know that it’s possible boot loader is enabled (BOOTRST)
to connect an external programmer, because it means you and how much memory is reserved for it.
72
www.elektor-magazine.com | June 2014 | 35
220R
+5V voltage. In this case the program must calculate
in the opposite direction to determine the actual
supply voltage (see Listing 4). The first mea-
surement (Figure 5) shows the supply voltage
28 27 26 25 24 23 22 21 20 19 18 17 16 15 with USB power, which in this case is about 4.5 V.
Then an external power supply is connected while
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
the program is running, so the supply voltage is
ATmega328p provided by the distinctly more precise 5 V volt-
age regulator. The corresponding measurement
GND
VCC
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
is accordingly close to 5.0 V.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
73
36 | June 2014 | www.elektor-magazine.com
‘-------------------------------------------
‘UNO_AD4.BAS Temp
‘-------------------------------------------
$regfile = “m328pdef.dat”
$crystal = 16000000
$baud = 9600
Dim D As Word
Dim N As Word
Dim U As Single
Do
Config Adc = Single , Prescaler = Auto , Reference = Internal ‘1,1 V
Waitms 200
D = Getadc(8) ‘Temperature
D = D - 338
Print D ; “ deg”
Waitms 500
Config Adc = Single , Prescaler = Auto , Reference = Avcc ‘5 V
Waitms 200
D = Getadc(0)
U = D * 5.0
U = U / 1023
Print U ; “ V”
D = Getadc(1)
U = D * 5.0
U = U / 1023
Print U ; “ V”
Loop
74
www.elektor-magazine.com | June 2014 | 37
across the load resistor, yielding a current of the total current should not exceed 200 mA. In
19 mA. The additional power dissipation in the our experiments we found that the ports could
microcontroller is 17 mW (0.9 V x 19 mA). Sig- handle currents up to 100 mA without damage,
nificant warming cannot be expected from this which means that even small DC motors could be
amount of power, and that’s exactly what we saw operated directly from the ports without driver
from the temperature measurement (Figure 7). ICs. However, if you want to be on the safe side
According to the data sheet, the maximum cur- the rule is to never exceed 40 mA.
rent at any port pin should not exceed 40 mA and
‘---------------------------------------------------
1k
‘UNO_AD5.BAS Hi/Lo Input Threshold +5V 100u
LED
‘---------------------------------------------------
$regfile = “m328pdef.dat”
$crystal = 16000000
$baud = 9600 28 27 26 25 24 23 22 21 20 19 18 17 16 15
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
Dim D As Word
ATmega328p
Dim Dmin As Word
GND
VCC
RES
Dim Dmax As Word
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
Dim N As Word 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Dim U As Single
Config Portb = Output
16MHz
Config Adc = Single , Prescaler = 64 , Reference = Avcc ‘5 V
100n
22p 22p
For N = 1 To 10000 140028 - 18
Dmin = 1023
Dmax = 0
Do
For N = 1 To 10000
Portb.5 = Not Pinc.5 ‘13 100k A5
D = Getadc(5) ‘A5 100µF GND
If D < Dmin Then Dmin = D
If D > Dmax Then Dmax = D
Next N
U = Dmin * 5.0
U = U / 1023
Print “Min = “;
Print U , “ V”
U = Dmax * 5.0
U = U / 1023
Print “Max = “;
Print U , “ V”
Loop Figure 9. The upper and lower switching thresholds of an
input.
75
38 | June 2014 | www.elektor-magazine.com
1695
test equipment to determine the switching thresholds of a
digital input. Now we want to the same thing entirely auto-
$
matically with the built-in A/D converter (see Figure 8).
Waterproof, individually addressable LED strip that runs on 5 V. Can be
For this we use two new variables (Dmin and Dmax) to hold chained to form longer strips or cut for shorter sections. Other lengths
the values of the lower and upper switching thresholds (see and LED densities available.
Listing 6). The measurement loop is preceded by a control
loop which ensures that the input voltage is already within 37D mm Metal Step-Up/Step-Down
the desired range before the actual measurement starts.
Gearmotors Voltage Regulator
At first the maximum possible value is stored in the Dmin
variable and zero is stored in the Dmax variable. The pro-
$
2495 S18V20ALV
ITEM #2572
gram then looks for the actual limit values. This is done
using a random-sample approach. The longer the mea-
$
1595
Other sizes available
surement session lasts, the more accurate the determined
•• 3Adjustable
V to 30 V input
limit values are.
This program shows how to use loop counters. The For-Next
•• Several gear ratios stocked
Versions with integrated
4–12 V output can be
above or below input voltage
loop counts down from N = 1000 to 0 and executes the encoders also available • 2 A typical max output current
instructions inside the loop exactly 1000 times. Although one A-Star 32U4 Micro
thousand iterations may sound like a lot, the entire process ITEM #3101
1275
takes only a fraction of a second. The large number of mea-
surements increases the probability that the measurement
$
results include the true limit values. This means that you have
a good chance of finding the correct values after just one Smallest
ATmega32U4
round. This can be seen from the fact that the subsequent board
rounds do not yield any significant changes.
•• 1’’ATmega32U4
× 0.6’’ programmable module with native USB
with Arduino-compatible bootloader
For N = 1 To 1000
… instructions … Mini Maestro 12-Channel Sub-Micro
Next N USB Servo Controller Servo
ITEM #1352 ITEM #1053
2995 495
Figure 9 shows the result of this experiment. The switching
$ $
thresholds are 2.23 V and 2.62 V. These are approximately
the same as the values determined using the external test
equipment. Other
(130568-I)
• USB, serial, and internal scripting • 3.7 g
servos
•
available
control Specs at 6 V:
Web Link
• 6-,also18-,available
and 24-channel versions •• 60.07oz·insec/60°
[1] www.elektor-magazine.com/130568
99
Get a little pushy!
$ 95
Arduino-controllable tracked robot small enough for mini-sumo (less
than 10 cm × 10 cm) and flexible enough for you to make it your own.
Individual parts and kit version also available — build your own
configuration!
Finding the right parts for your design can be difficult, but
you also don’t want to spend all your time reinventing the
wheel (or motor controller). That’s where we come in:
Pololu has the unique products — from actuators to
wireless modules — that can help you take your design
from idea to reality.
Find out more at: www.pololu.com
76
www.elektor-magazine.com | June 2014 | 39
Microcontroller
BootCamp (4)
User interfaces
By Burkhard Kainka Serious microcontroller applications usually have some sort of interface between
(Germany)
humans and the machine. You can do a lot with a just a small display, a few but-
tons, a potentiometer and a couple of LEDs. What’s more, you can hit the ground
running with the Arduino shield developed by Elektor, which is described elsewhere
in this edition.
First let’s see what it takes to drive a display. Arduino board. You can also see the other periph-
Various types are available, including graphic and eral components there: two LEDs, two pushbut-
text displays. Text displays range from tiny with tons and a potentiometer. If you wish, you can
a single line of 8 characters, to large with four build all this yourself on a piece of prototyping
lines of 20 characters. As the saying goes, small board. You don’t even have to use exactly the
is beautiful, and in many cases you don’t have to same type of display module. One with two lines
display a lot of characters. That’s why the new of 16 characters would work just as well. If you
Elektor shield has a small two-line text display like to keep things simple, you can order the new
with eight characters per line. In any case, that’s shield on the web page for this article [1], where
enough room for messages such as “V=3.3V” as usual you can also download a file containing
or “I=8.2mA”. What more do you need on your the code for the programs described here.
electronics bench? The display occupies six pins on port D: D2 to
D7. That’s handy, because it leaves exactly two
LCD connection spare for the serial interface: D0 (RXD) and D1
Figure 1 shows how the display module on the (TXD). Ports B and C remain free for whatever
shield is connected to the ATmega328 on the strikes your fancy. All standard display modules
77
58 | July & August 2014 | www.elektor-magazine.com
of this sort can optionally be operated in 8-bit is designed for two lines of 16 characters (16 *
mode, which means that the data is sent to the 2), even with this small display. That’s not a
display over eight lines in parallel. However, 4-bit problem in practice because the program keeps
mode has become customary to reduce the num- on running without an error if you accidentally
ber of port pins used on the microcontroller. In write up to 16 characters in a line. However, when
this mode each data byte is sent to the display you see the truncated output on the display you
in two steps. For example, if you want to write will have to consider how you can shave a few
an upper-case A you have to send the hex value more characters off the message.
&H41 (binary &B01000001, or 65 in decimal nota-
tion), as specified in the ASCII code table. In fact After you initialize the display, you should clear
you first send &B0001 and then &B0100. How- everything with the Cls (Clear Screen) command.
ever, you don’t actually need to know all these Everything written after this lands on the first
details or how to go about initializing the display, line, starting at the left end. Here it is the word
since Bascom does all the hard work for you. If “Elektor” – seven characters, short and sweet.
you simply write Lcd “A” in your program, the If you send another character to the LCD now, it
letter A will appear on the display. will be written to position 8 on the first line, but
Our first example program (Listing 1) shows how what you actually want is to start with the sec-
it all works. First you need a Config instruc- ond line. For this you use the Locate command.
tion to tell the compiler how the display pins Here Locate 2 , 1 causes the next character
are connected to the port pins. Next you have to be written to the leftmost position of the sec-
to initialize the display with the Config Lcd = ond line. The program outputs an incrementing
16 * 2 instruction. You can choose from several number at this position. The count variable N is
formats here. The format 8 * 2 is not supported incremented once per second, so you can use
because the display controller in the LCD module the program to measure time. For example, it’s
1k
'------------------------------------
$regfile = "m328pdef.dat" +5V
10k
'ATmega328p
$crystal = 16000000 '16 PWMB PWMA
MHz 28 27 26 25 24 23 22 21 20 19 18 17 16 15
$baud = 9600
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
ATmega328p
Dim N As Word
GND
VCC
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Config Lcdpin = Pin , Db4 = Portd.4 ,
Db5 = Portd.5 , Db6 = Portd.6 , Db7 =
Portd.7 , E = Portd.3 , Rs = Portd.2
16MHz
100n
Config Lcd = 16 * 2
Locate 2 , 1
D0
D1
D2
D3
D4
D5
D6
D7
GND
R/W
Figure 1.
RS
E
VEE
VCC
78
www.elektor-magazine.com | July & August 2014 | 59
taken me 1200 seconds to write these lines since on its own what has to be done, which is very
I started the program. That’s 20 minutes, which convenient for the user. In other languages the
means I’m pretty slow today. programmer has to do more in these situations.
On the display you will see an underscore after
There are two Lcd instructions in this example the number that was output, which represents
program, and it’s important to understand that a cursor. The cursor shows where the next char-
they do two completely different things. The pure acter will be written if and when it comes. This
text output instruction simply copies the charac- may be very helpful when you’re busy typing
ters set in quotation marks by the programmer. something in, but in our case the cursor is irri-
By contrast, the instruction Lcd N converts the tating. Fortunately, we can do something about
numerical value in the variable N into text and this with the Cursor Off command, which is ini-
then sends this text to the display. In this case tially commented out in the code. If you delete
N is an integer, but in other cases it could be a the comment character, recompile the program
real number with decimal places. Bascom decides and download it to the microcontroller, you will
79
60 | July & August 2014 | www.elektor-magazine.com
A two-channel voltmeter
Happen to need a two-channel voltmeter? List- Listing 2: Displaying voltages.
ing 2 shows one of many possible solutions.
'------------------------------------
In the previous instalment we told you how to
'UNO_LCD2.BAS Voltage A0, A3
convert the data provided by the A/D converter
'------------------------------------
into a voltage reading. Now let’s see how you
$regfile = "m328pdef.dat"
can show it on the display. First we write a string
'ATmega328p
at the start of each line that identifies the input.
$crystal = 16000000 '16
The is followed by a space character to separate
MHz
it from the reading. We also tack on a couple of
$baud = 9600
spaces after the reading (in this case a real num-
ber) has been output. Without them, the follow-
Dim D As Word
ing situation could occur: Your last reading was
4.859 V, and the new reading is 5.0 V. Bascom Dim U As Single
outputs “5.0” just as it should, but the rest of
the previous output is still there on the display, Config Adc = Single , Prescaler = 64 ,
so you see “5.059 V” and wonder what’s going Reference = Avcc '5 V
on. To prevent this from happening, we always
delete anything that might be present after the Config Lcdpin = Pin , Db4 = Portd.4 ,
current output. Here we accept the risk that this Db5 = Portd.5 , Db6 = Portd.6 , Db7 =
may involve characters intended for a larger dis- Portd.7 , E = Portd.3 , Rs = Portd.2
play that are not visible on the actual display. Config Lcd = 16 * 2
80
www.elektor-magazine.com | July & August 2014 | 61
1k
1k
100u...
470u on the measurement input, and then its charge
+5V
10k is measured. If the capacitor last saw 3 V from
10k
the potentiometer during the previous measure-
ment, some of its charge will still be left because
28 27 26 25 24 23 22 21 20 19 18 17 16 15 it cannot discharge quickly enough through the
extremely high impedance of the LED. For this
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
ATmega328p reason, the microcontroller data sheet recom-
mends that the internal resistance of the signal
GND
VCC
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 source should not exceed 10 kΩ. Here we are
dealing with many megohms instead, so it’s bet-
16MHz
ter to set the potentiometer to zero. Then you
100n
will see that there is still a measurable voltage
Figure 2.
22p 22p
140064 - 12
at low light levels. This circuit shows that the
Using the analog inputs. A/D converter has very high input impedance. A
typical digital voltmeter with an input impedance
that a perfectly ordinary LED can also act as a of 10 MΩ would not be able to measure the LED
photodiode – in this case as a light sensor. If you voltage, but the ATmega can do so.
shine a bright light on the LED, it can deliver a
voltage as high as 1.5 V—three times as much With very high impedance signal sources, you
as a silicon photodiode. That’s because a larger can connect a 10 nF bypass capacitor in parallel
band gap means a greater voltage, which applies as shown in Figure 3. This makes the measure-
equally well to the forward voltage of a diode and ments much more reliable. Even with very dim
voltage it generates. light, you can now measure a voltage of roughly
Another thing you might notice is that at low light 1 V. For all PN junctions with their exponential
levels the voltage measured on ADC2 is affected characteristic curves, the rule of thumb is that
the voltage rises by approximately 70 mV when
the current is increased by a factor of 10. This
Listing 3: Measuring the LED voltage means that the span between 1.0 V and 1.5 V cor-
and the potentiometer voltage. responds to seven decades of current range, and
Do thus seven decades of brightness. That should
Locate 1 , 1 be more than enough for measuring from 10 lux
Lcd "A2 " to 100,000 lux, and we can see the makings of
a light meter here. Maybe that’s an attractive
D = Getadc(2) 'LED1
job for an enthusiastic reader? There’s nothing
U = D * 5.0
wrong with a nice collection of Bascom programs
U = U / 1023
with contributions from many people.
Lcd U
Lcd " " PWM outputs
When precise timing matters, timers come to the
Locate 2 , 1 fore. Most microcontrollers have several timers.
Lcd "A3 " Timer1 of the ATmega328 has a resolution of
D = Getadc(3) 'Pot 16 bits. You can regarded it as a sixteen-stage
U = D * 5.0 counter, similar to the well-known CD4040 (which
U = U / 1023 has only twelve stages). A clock signal (or other
Lcd U pulse signal) entering at one end comes out at
Lcd " " the other end with a lower frequency. You can
use the CD4040 to build a clock generator or a
Waitms 1000
frequency divider, or as the basis for a binary
Loop
pulse counter. Timer1 can also handle all of these
81
62 | July & August 2014 | www.elektor-magazine.com
and Timer2).
1k
1k
One of the many operating modes of Timer1 is 10n
GND
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AVCC
AREF
way as a dimmer on the AC line which simply
switches the voltage on and off at a high rate. ATmega328p
GND
VCC
RES
Here we want to do the same thing by using the
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
PWM output to control the brightness of an LED.
The PWM output has a maximum adjustment 16MHz
82
www.elektor-magazine.com | July & August 2014 | 63
1k
1k
that Bascom uses the “Phase Correct PWM” mode
instead of the “Fast PWM” mode. You can learn
+5V 10k
more about this from the microcontroller data
sheet, but that’s more like a book than a data
sheet. Here Bascom saves developers time and
28 27 26 25 24 23 22 21 20 19 18 17 16 15
effort by always choosing a reasonable option for
GND
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AVCC
AREF
each setting. This means you can obtain work-
ATmega328p ing results with Bascom even if you don’t fully
GND
understand all the details.
VCC
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
the PWM outputs. tion is used to assign the button names to the
port inputs. This means that when you write If
1023. You could for example program the out- S1 = 0 Then in your code, Bascom knows that
put to switch off when the counter reaches 511, what you actually mean is If Pinc.1 = 0, so the
which results in a PWM signal with a 50% duty state of port pin PC1 has to be polled. Another
factor. This is achieved by repeatedly comparing important consideration is that the buttons are
the counter value with the programmed value connected to ground and therefore require pullup
in a compare unit. There are two compare units resistors. As a result, the program normally sees
in the microcontroller, so you can generate two the quiescent state S1 = 1. This only changes to
independent PWM signals with the same timer. S1 = 0 when someone presses the button. That
PWM1A is output on port pin PB1, while PWM1B makes things very simple. When you press S1
is output on pin PB2. That reminds us of some- you reduce the PWM output level (assuming it
thing. You guessed it: LED 2 is connected to PB2 is greater than 0), and when you press S2 you
through a 1 kΩ series resistor. Coincidence? Not raise the PWM output level (assuming it is less
at all, because now you can use the PWM signal than 1023). This proceeds in single steps if you
to control the brightness of the LED. press the button briefly, but if you press and hold
The program in Listing 4 uses the PWM1A out- the button it changes continuously at ten steps
put together with the A/D converter input ADC3, per second. The current output value is always
which is connected to the potentiometer. The shown on the LCD.
measured value of the potentiometer setting is
used as the comparison value for the PWM sig- Button polling
nal. If you set the potentiometer to mid-range, The program shows a very simple example of
the PWM output is a symmetrical square wave. how to use buttons and corresponding program
Everything from 0 to 1023 is possible. The actual branching. Of course, there are lots of other ways
output value is shown on the LCD. If you want to obtain the desired result. Developing the ideal
to look at the signal, you can connect a scope user interface for a given task is a fascinating
probe to PB1 (Arduino pin 9). job. You can configure a wide variety of things
with two buttons and a potentiometer. A lot can
What is the frequency of the PWM signal? That be achieved with just a few buttons.
depends on the frequency of the clock input to the The program uses everything that the Elektor
timer. It can be the clock signal from the Arduino shield has to offer. The LCD shows the two cur-
board (16 MHz), but it can also be a lower-fre- rent PWM values, the potentiometer controls the
quency signal derived from the clock signal by PWMA output, and the two buttons control the
passing it through a prescaler. Here the initializa- PWMB output and thereby LED 2. Is that really
tion instruction Prescale = 1 means that the full everything? Well, not quite—LED 1 isn’t doing
83
64 | July & August 2014 | www.elektor-magazine.com
anything. So let’s plug a wire into PB1 (Arduino ness using the potentiometer. The LCD reveals
pin 9) and ADC2 (Arduino A2) to connect PWMA how well this went. Then the two players swap
to LED 1 through a 1 kΩ resistor and a jumper roles. All differences between the two values are
(Figure 4). Now you can control the brightness recorded. The player with the lowest score at the
of the LED with the potentiometer. end is the winner and is designated “Hawkeye”.
With two LEDs whose brightness can be set (140064-I)
independently, you have the makings of a little
Web Links
skill-testing game. The first player sets the bright-
ness of LED 2 to some arbitrary value using the [1] www.elektor-magazine.com/140064
buttons. Then this LED is covered, and the sec- [2] http://b-kainka.de/bastel49.htm (in German)
ond player tries to set LED 1 to the same bright- [3] http://en.wikipedia.org/wiki/Polarizer
84
www.elektor-magazine.com | July & August 2014 | 65
Microcontroller
BootCamp (5)
Using timers
One of the longest chapters in the ATmega328 which has a resolution of 16 bits. Now, all the
data sheet is the one that describes its three timer module does is simply act as a counter of
timers. The timers can be used in so many dif- regular events. In this case, with a 16-bit counter,
ferent ways that we only have space here to look the maximum count is 65535, after which the
at a small fraction of the possibilities. The main counter returns to zero: this is called ‘overflow’.
application areas are in measuring time inter- If things are arranged so that the counter incre-
vals and frequencies, and in generating various ments once a microsecond we can measure time
signals including PWM output. intervals of up to 65535 µs. Between the crys-
tal oscillator and the clock input of the counter
Measure those microseconds there is a programmable prescaler (similar to the
The need to measure time intervals often crops arrangement for the A/D converter) that divides
up in electronics. For example, we might want to down the clock frequency. If a 16 MHz crystal is
know how long it takes to output some data to an used then the required division ratio to obtain
LCD module: is it milliseconds or microseconds? a 1 MHz clock for the timer is 16. So our first
Without some inside knowledge, the only way attempt reads
is to measure it. The ATmega328 has a suitable
module already on board in the form of Timer1, Config Timer1 = Timer , Prescale = 16
85
30 | September 2014 | www.elektor-magazine.com
but unfortunately this does not work. The error takes around a millisecond per character because
message we get is ‘Prescale value must be 1, Bascom allows plenty of time for the controller
8, 64, 256 or 1024’, and the data sheet of the in the module to accept the data. If you take a
ATmega328 confirms the problem. So, we shall look at the E signal on the LCD (port pin PD3,
set the prescaler ratio to 8 and obtain an input Arduino pin 3) using an oscilloscope you will see
clock to the timer of 2 MHz and a maximum the 1 ms delays.
interval measurement of 32767.5 µs. At any time
the 16-bit register that holds Timer1’s value can Measuring the period of a signal
be read or a new value can be written to it. This A small modification to our program (see List-
makes our example very straightforward. Before ing 2) allows it to measure the length or period
outputting the data to the LCD module we set the of a pulse. We use input PC0 (Arduino A0: see
counter to zero (‘Timer1 = 0’). After outputting Figure 2), and we will measure the time between
the data we read the counter value (‘D = Timer1’) two rising edges of the input signal. We need two
and we have the result of the measurement in sampling loops to detect an edge reliably: first
units of 0.5 µs. To convert to microseconds we we wait until the input reads as a zero, and then
simply divide the result by 2. Listing 1 shows we wait until it reads as a one. This will detect
the complete program.
86
www.elektor-magazine.com | September | 31
10k
not blowing strongly enough. Perhaps it might
be worth turning a couple of lights off and try-
ing again...
28 27 26 25 24 23 22 21 20 19 18 17 16 15
In order that we have a reliable signal to mea-
sure, the program includes its own clock source,
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
ATmega328p taking advantage of the PWM outputs. However,
there is a potential problem. PWM1A and PWM1B
GND
VCC
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
cannot be used because we have already commit-
ted Timer1 for making the measurement itself.
16MHz That leaves us with Timer0 and Timer2, each of
which can also drive two PWM outputs. However,
100n these extra outputs mostly appear on Port D and
Figure 2. 22p 22p would therefore interfere with the LCD interface.
140049 - 12
Measuring the period of a An exception is PWM2A which is on port pin PB3
signal. (Arduino pin 11), and so this is the one we use to
output our test signal. Timer2 has only an 8-bit
the first rising edge, and we set the timer reg- counter but if we set its prescaler ratio to 256,
ister to zero. We now wait for the second edge, we can obtain an output frequency of 16 MHz /
and read the result from the timer. 256 / 255 / 2 = 122.549 Hz and hence an output
If a finger is touched on the input pin, the micro- period of 8.16 ms. Connecting PB3 to PC0 lets
controller will receive a signal picked up from the us measure this signal, and the reading we get
mains supply, which will be at 60 Hz in the USA is 8160 µs. Result!
and 50 Hz in most other countries. We would
expect a result of around 16667 µs or 20000 µs Square wave generator,
respectively. In a real experiment (carried out 125 Hz to 4 MHz
in Europe) a reading of 20030 µs was obtained, A variable-frequency signal generator is often a
'----------------------------------------- Timer1 = 0
'UNO_Timer2.BAS Timer1, 0.5 us Do
'----------------------------------------- Loop Until Pinc.0 = 0
... Do
Loop Until Pinc.0 = 1
Dim D As Word D = Timer1
D = D / 2
Config Timer1 = Timer , Prescale = 8 'Clock 2 MHz Locate 1 , 1
Config Timer2 = Pwm , Prescale = 256 , Lcd "Timer1 ="
Compare A Pwm = Clear Up Locate 2 , 1
Ddrb = 255 Lcd D
Pwm2a = 128 Lcd " us "
Print "Timer1 = ";
Do Print D;
Do Print " us"
Loop Until Pinc.0 = 0 Waitms 1000
Do Loop
Loop Until Pinc.0 = 1
87
32 | September 2014 | www.elektor-magazine.com
'----------------------------------------- If S1 = 0 Then
'UNO_Timer3.BAS B1 Fout 250 Hz...4 MHz D = D + 1
'----------------------------------------- If D > 100 Then D = D + 1
... If D > 1000 Then D = D + 100
If D > 10000 Then D = D + 1000
Dim D As Long If D > 64000 Then D = 64000
Dim F As Long End If
Dim N As Byte If S2 = 0 Then
If D > 2 Then D = D - 1
Config Timer1 = Pwm , Prescale = 1 , Pwm = 10 , If D > 100 Then D = D - 10
Compare A Pwm = Clear Up If D > 1000 Then D = D - 100
If D > 10000 Then D = D - 1000
Tccr1a = &B10000010 'Phase-correct PWM, Top=ICR1 If D > 64000 Then D = 64000
Tccr1b = &B00010001 'Prescaler=1 End If
Locate 1 , 1
D = 8000 F = 16000000 / D
Do F = F / 2
N = Ischarwaiting() Lcd F
If N = 1 Then Lcd " Hz "
Input F Icr1 = D
D = 8000000 / F Ocr1a = D / 2
If D > 64000 Then D = 64000 Waitms 50
If D < 2 Then D = 2 Loop
End If
useful tool to have. At 1 MHz it might be used to a terminal emulator program or using buttons.
test a frequency counter, or at 440 Hz it might When pressed, the buttons increase or decrease
be used to tune a violin. The program shown in the division ratio, and the program calculates
Listing 3 covers the whole range from 125 Hz the resulting frequency and displays it. A brief
to 4 MHz. Timer1 is used as a frequency divider press of a button changes the ratio in small steps,
and the signal is output on the PWM1A pin (B1, while a longer press causes the ratio to change
Arduino pin 9: see Figure 3). This is an exam-
ple of an application where we are going a little
beyond the standard uses of Bascom, and in par-
125 Hz ... 4 MHz
ticular its built-in initialization functions are not S2 S1
+5V
suitable for our purposes: we have to program
a few registers directly. I borrowed some ideas F+ F–
1k
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
D5
D6
D7
B0
X1
X2
88
www.elektor-magazine.com | September | 33
Figure 4. (almost) continuously. Since it would be rather It is perhaps not completely obvious how the
Producing 1 kHz from tedious to step through all the possible ratios in program manages to respond both to button
16 MHz. this way (there are more than 65000 of them), presses and to input on the serial port. If we
the amount of increment changes depending on write simply ‘Input F’ then the program will wait
at this point until a value is entered, and will not
respond to the buttons. We therefore have to
Listing 4. Counting seconds check first whether there is a character available
exactly using an interrupt. on the serial interface: the function IsCharWait-
ing() returns a value of 1 if there is at least one
'------------------------------------
character available and zero otherwise. In our
'UNO_Timer4.BAS
case, if one character arrives on the serial inter-
'Timer0-Interrupt, Seconds
face then we know that there are more to come,
'------------------------------------
and we can safely read in a new value for F.
...
From F we can calculate the required division
ratio, in many cases obtaining an exact result. At
Dim Ticks As Word
440 Hz there is only negligible error, although at
Dim Seconds As Word
549 kHz, for example, only a rough approxima-
Dim Seconds_old As Word
tion is available: the program chooses a division
ratio of 14, which results in an output frequency
Config Timer0 = Timer , Prescale = 64
of 571428 Hz. The smallest division ratios give
On Ovf0 Tim0_isr
rise to the highest frequencies, namely 4 MHz,
Enable Timer0
2.666666 MHz, 2 MHz, 1.6 MHz, 1.333333 MHz
Enable Interrupts
and 1 MHz. If these are useful to you, then you
could build the design as a self-contained unit.
Do
The steep edges on the signal on output PB1
If Seconds <> Seconds_old Then
mean that it contains harmonics well into the
Print Seconds
VHF range. This means it is all too easy for the
Seconds_old = Seconds
signal generator to become a source of radio
Locate 1 , 1
interference. For example, if you connect the
Lcd Seconds
oscilloscope probe to the output but forget to
End If
connect the ground clip close by then a large and
Loop
rather effective VHF loop antenna can be cre-
ated via the ground connection to the USB port
Tim0_isr:
and then through the PC’s power supply and the
'1000 µs
mains. Unsurprisingly this can disrupt reception
Timer0 = 6
on nearby FM radios and hence relations with
Ticks = Ticks + 1
your neighbors! To keep on the right side of the
If Ticks = 1000 Then
EMC regulations (and your neighbors) it is neces-
Ticks = 0
sary to use either a screened cable, or a resistor
Seconds = Seconds + 1
close to the signal generator’s output to reduce
End If
the amplitude of the higher harmonics. A resistor
Return
of 1 kΩ in conjunction with a cable capacitance
of 30 pF forms a low-pass filter with a cutoff
End
frequency of 5 MHz. The edges of the signal are
89
34 | September 2014 | www.elektor-magazine.com
10k
looked at an example program that generates
a one-second clock. The timing used a simple
‘Waitms 1000’ command. Now this approach 28 27 26 25 24 23 22 21 20 19 18 17 16 15
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
of the program such as the infinite loop and the
ATmega328p
code that generates the output all take time. The
GND
VCC
RES
problem can be avoided using a timer interrupt:
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
that is, allowing a certain part of the code to
execute exclusively under control of a timer. It
16MHz
works as follows. A hardware timer counts away,
completely independent of other activity in the
100n
microcontroller. When it reaches its maximum 22p 22p
Figure 5.
count and resets to zero (‘overflow’) the program 140049 - 15 AC and DC voltage
executing in the foreground is interrupted and measurement.
a special piece of code called an ‘interrupt ser-
vice routine’, or ISR, is called. Within this piece every millisecond. The word variable ‘Ticks’ is
of code we can carry out any actions that need incremented by one on each timer overflow.
to happen at exactly-specified time intervals. It When it reaches 1000 the variable ‘Seconds’ is
makes no difference how long the ISR itself takes incremented. The variables ‘Seconds’ and ‘Ticks’
to execute as the hardware timer is continuing can be read from the main program code. In
to count: the only thing that matters is that the this example the program outputs the number
routine completes before the next time the timer of seconds since it was started, both to the LCD
overflows and triggers the interrupt again. and to the terminal.
For the job of generating a precise one-second
clock Timer1 is overkill: the eight-bit resolution To ensure that the interrupt service routine is
of Timer0 is plenty for this application. We will actually called, the corresponding interrupt (the
arrange for the timer to overflow and hence gen- Timer0 overflow interrupt) must be enabled. This
erate an interrupt every 1000 µs. The interrupt is done with the line ‘Enable Timer0’. Interrupts
service routine, which, as it is triggered from must also be globally enabled, using the com-
Timer0, we have called ‘Tim0_isr:’, will thus be mand ‘Enable Interrupts’. The complementary
called every millisecond. The colon means that command ‘Disable Interrupts’ prevents all inter-
‘Tim0_isr:’ will be interpreted as a label, marking rupts from occurring.
a point in the code which can be jumped to. The
command ‘On Ovf0 Tim0_isr’ tells the micro- Averaging analog readings
controller to jump to this label whenever there Analog readings often have mains hum, at 50 Hz
is an overflow (‘Ovf’) on Timer0. The interrupt or 60 Hz depending on the local frequency, super-
service routine must finish with a ‘Return’ com- imposed on them. One way to try to remove this
mand: after that point the interrupt routine can is to take the average of a number of consecutive
be called again. samples: see Listing 5. If readings are aver-
The example program shown in Listing 4 ini- aged over an integer number of mains cycles
tializes Timer0 with a prescale ratio of 64, which (that is, over a multiple of 20 ms or 16.667 ms
means it increments at 250 kHz (see Figure 4). If respectively), the effect is to create a null at that
we did not take any further steps the timer would frequency. In other words, the hum will be sig-
overflow on every 256th clock, as the counter is nificantly attenuated.
eight bits wide. To arrange for an overflow every Again in this example we use a timer interrupt to
250 clocks, the first thing the interrupt service ensure accurate timings. We will take the aver-
routine does is load the counter with the value 6. age of 500 consecutive readings from ADC0 (see
The result is that the routine is called exactly Figure 5), which a total of 400 ms. Now this is
90
www.elektor-magazine.com | September | 35
91
36 | September 2014 | www.elektor-magazine.com
10k
...
28 27 26 25 24 23 22 21 20 19 18 17 16 15
B5
B4
B3
B2
B1
AVCC
AREF
D5
D6
D7
B0
X1
X2
16MHz
Config Timer0 = Timer , Prescale = 64
On Ovf0 Tim0_isr
100n Enable Timer0
22p 22p
140049 - 16
Config Timer1 = Counter , Edge = Falling , Prescale = 1
On Ovf1 Tim1_isr
Enable Timer1
Figure 6. Frequency meter with test output.
92
www.elektor-magazine.com | September | 37
'----------------------------------- Do
'UNO_Display.BAS COM input B0 'Input Text1
'---------------------------------- Input #2 , Text1
... Locate 1 , 1
Lcd Text2
Dim Text1 As String * 16 Text2 = Text1 + " "
Dim Text2 As String * 16 Locate 2 , 1
Lcd Text2
Open "comb.0:9600,8,n,1" For Input As Loop
#2
'Software COM input at B0 End
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
RX-Input 2
we try an input frequency of 6 MHz we find that
approximately every other pulse is lost, giving
a reading of around 3 MHz. At frequencies up
100n to a little over 4 MHz, however, the counter is
16MHz
very accurate.
Observe one special aspect of the ‘Print’ com-
22p 22p mands. Normally Bascom terminates each print
10k
command by emitting Chr(13) and Chr(10), which
Contrast
makes it easy to send the output of one Bascom
1 2 3 4 5 6 7 8 9 10 11 12 13 14
program to the input of another. However, in the
receive direction only the character Chr(13) is
D0
D1
D2
D3
D4
D5
D6
D7
GND
R/W
RS
E
VEE
VCC
93
38 | September 2014 | www.elektor-magazine.com
overwrites the old on the same line rather than the frequency measurement). The disadvantage
beginning a new line for each: see Figure 7. is that communication during the next program
upload using the bootloader will be disturbed. It
External display is easy to forget this, which can lead to a lot of
A simple solution to the problem of not being head-scratching when you next make a change
able to use the display directly is to use another to the program!
Arduino. One, with an Elektor shield fitted, func- The second possibility takes advantage of Bas-
tions as the display module, while the other acts com’s ability to implement a serial port in soft-
as the frequency counter, sending its results over ware using any desired port pin. Here we have
a serial interface to the first. Listing 7 shows the chosen PB0 (Arduino pin 9: see Figure 8) and
code for a simple display with scrolling output. use the command ‘Input #2, Text1’. The text is
The last two lines are always shown. received just as reliably, and there is no inter-
The program offers two possibilities for receiving ference with program upload.
serial data. The command ‘Input Text1’ (com-
Web Links
mented out) uses the normal serial RX input on
D0. This input is connected to the USB interface [1] Roger Leifert, ‘DCF Tester’, Elektor June
on the Uno board via a 1 kΩ series resistor. This 2014, www.elektor-magazine.com/130571
signal can be overridden using a low-impedance [2] Martin Ossmann, ‘AVR Software Defined Ra-
drive, as for example happens when the RX pin dio’, Elektor April 2014,
on the display unit is connected directly to the TX www.elektor-magazine.com/100181
pin of the transmitting Uno (the one carrying out
Advertisement
DLP-IO8-G DLP-IOR4
8-Channel Data Acquisition 4-Channel Relay Cable
DLP-TH1b
Temp/Humidity Cable
Only
$29.95! DLP-RFID1
HF RFID Reader/Writer
DLP-FPGA
8 I/Os: Digital I/O USB-to-Xilinx FPGA Module
Analog In
Temperature
USB Port Powered
Single-Byte Commands
www.dlpdesign.com
94
www.elektor-magazine.com | September | 39
Microcontroller
BootCamp (6)
The SPI interface
By Serial communication with each unit of information following the previous one is
Burkhard Kainka
actually the normal situation. That’s how we talk to each other in person or by
(Germany)
phone, read and write text—or in the case of Retronics: send telegrams.
In many cases all you need is a single line to transmit data. However, adding a
clock line makes things more reliable. Let‘s find out.
Q5
Q6
Q7
Q8
OE
QS
CL
Q1
Q2
Q3
Q4
1 2 3 4 5 6 7 8
clock line. The clock signal always clearly indi-
28 27 26 25 24 23 22 21 20 19 18 17 16 15 cates when the next bit is available on the data
line. That eliminates the need for precise agree-
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
D5
D6
D7
B0
X1
X2
16MHz
Port extension with a shift register
100n
The first thing we want to try out is actually not
Figure 1. 22p 22p
an SPI interface, but instead something entirely
140245 - 11
Connecting a type 4094 shift different. Shift registers have been around for
register. a long time (before microcontrollers were even
95
30 | October 2014 | www.elektor-magazine.com
invented) and are good for understanding how terminal emulator. This makes it easy to com-
serial data transfer works. They can also be put pare the output levels of the shift register to the
to good use in combination with a microcontrol- digital value being output.
ler. That’s because port lines are always scarce, If you need more than eight outputs, you can
especially on Arduino boards. A port extension use the Qs output of the 4094 IC. Each bit that
with a shift register can help ease the scarcity. is clocked into the shift register appears at the
With a type 4094 8-bit shift register, you need
three lines to talk to it and you end up with
eight new outputs. You can increase this to 16 Listing 1. Output using a shift register.
by connecting a second shift register, or even
'------------------------------------
80 if you connect ten shift register ICs in series. 'UNO_shift.BAS Shift Register 4094
If you need a lot of outputs, that’s an especially '------------------------------------
low-cost way to meet the requirement. $regfile = "m328pdef.dat"
$crystal = 16000000
Figure 1 shows the connections to the Uno board. $baud = 9600
The 8-bit shift register has a clock input (CL)
and a data input (D). The data is applied to the Dim Dat As Byte
D input one bit at a time, starting with the most Dim D As Byte
significant bit, and a rising edge is applied to the Dim N As Byte
clock input CL for each bit. The data is shifted Dim B As Bit
through the individual flip-flops of the register
step by step with each clock pulse. There is also
the strobe input STR. When a pulse is applied Sr Alias Portb.4 '4094 pin 1
to the strobe input, all the data present in the Da Alias Portb.3 '4094 pin 2
shift register is transferred to the type-D flip- Cl Alias Portb.2 '4094 pin 3
flops connected to the output pins. You could Config Portb = Output
also tie the strobe input to the supply voltage
...
Vcc, but then all the intermediate results of each
shift operation would appear on the outputs. By
Dat = 0
contrast, if you apply a strobe pulse after all the
Do
bits have been shifted in, you only see the final
Cls
result on the outputs.
Lcd Dat
Lcd " "
The software for all this (Listing 1) is simple. To Print Dat
output a byte D, the code first copies the most D = Dat
significant bit to the bit variable B (B = D.7) For N = 1 To 8
and puts it on the corresponding port pin. It B = D.7
then generates a positive clock pulse on CL with Da = B
a length of 1 millisecond. A microsecond would Waitms 1
also be sufficient, but the slower output is easier Cl = 1
to see on an oscilloscope. After the clock pulse, Waitms 1
a shift instruction (Shift D , Left) causes all Cl = 0
bits of D to be shifted left by one position. This Waitms 1
puts what used to be bit 6 on the output. This Shift D , Left
process is repeated until all eight bits have been Next N
shifted out. At the end comes the strobe pulse, Sr = 1
and then all eight bits are present at the outputs Waitms 1
of the 4094. Sr = 0
The code continuously increments the data byte Waitms 1
Dat = Dat + 1
to be transferred so the outputs of the shift regis-
Waitms 500
ter change while the program is running. The
Loop
current value is shown on the LCD if the Elektor
End
Extension shield is fitted, and it is output to the
96
www.elektor-magazine.com | October 2014 | 31
Qs output eight clock pulses later. The D input of see the difference. There both parties have to
the next shift register can be connected to this agree on the transmission rate, and no breaks are
output. In this way you can connect as many allowed within an information unit. For example,
4094 ICs in series as desired, with the clock and if a radio operator wants to send an “X” (dash
strobe lines connected to all of them in parallel. dot dot dash) and stops in the middle to scratch
Of course, the software will have to be modi- his head, the two characters “N” (dash dot) and
fied accordingly. First it shifts out all of the bits “A” (dot dash) are sent instead. The situation is
necessary to fill the chain of shift registers (e.g. exactly the same with an asynchronous serial data
16 with two ICs or 80 with ten), and then it out- interface, where both parties have to agree on
puts the common strobe pulse. the baud rate. After the transmission of a byte
has started, all of the bits must be sent within
Manual data transmission a precise time frame. By contrast, with SPI the
Although you only need one line for the data, you timing is not critical and any desired delays are
also need a clock line when you use a clocked allowed. The additional clock signal makes the
serial interface, as in the above example with a transfer entirely independent of the speed. No
shift register or with the SPI bus. If you compare matter whether the data rate is just one bit per
this with Morse telegraphy, for example, you can minute or a million bits per second, the data will
97
32 | October 2014 | www.elektor-magazine.com
1k
1k
Data Clock
You can try this for yourself manually, where you +5V
(as the user) assume the role of master. You can
transmit a byte by pushing the two buttons S1
and S2 (Figure 2). This is not the usual way of
28 27 26 25 24 23 22 21 20 19 18 17 16 15
doing things, but it helps you understand exactly
GND
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AVCC
AREF
how it works. One of the buttons is for the data,
ATmega328p
and the other is for the clock. Aside from that
GND
VCC
RES
there’s nothing new you have to learn, since you
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
already know how a byte is put together. Here’s
how it works: First you send bit 7. If it is a ‘1’, 16MHz
you press and hold button S2; otherwise you
don’t. Then you press button S1 briefly without 100n
changing the state of S2. The receiving end (the 22p 22p
140245 - 12 Figure 2.
slave, which in this case is the Arduino board) Manual input and output.
then knows when it should read the bit from the
data line. Now you repeat the process for bit 6,
bit 5, and so on until bit 0. marked by a clock pulse. It may not have been all
that easy, but the microcontroller had no problem
The program in Listing 2 displays the data in reading the data. You can regard this as a test
both directions. At first the microcontroller is of your concentration, and if the LCD shows the
the master and you are the slave. A byte with a right result, you pass the test.
random value is sent, with the clock signal indi-
cated by LED1 and the data indicated by LED2. If you look closely at Listing 2, you will see that
If you watch carefully, you can read the trans- the bits are inverted when they are read. That’s
mitted byte. However, that’s not easy, so the because pressing the data button yields a zero
byte is also shown on the LCD and sent to the bit value. This is not especially intuitive, so the
terminal emulator. The individual bits also appear result is inverted when the bit is read to make
one after the other on the LCD and the terminal things easier for you. Another interesting aspect is
emulator screen: using the instruction D = Rnd (255) to generate
a pseudo-random number. In fact, this always
83 generates the same sequence of numbers, but
01010011 the Bascom Help gives some suggestions for what
you can do about this.
Then the roles change. Now you are the master,
and your job is to send exactly the same byte From microcontroller
back to the microcontroller. Here you can see that to microcontroller
the ability to send data at any desired speed is a In this example, data is sent over the SPI bus
big advantage, since you can take all the time you from one microcontroller to another. The data
want to decide which bit value to send next. For is this case consists of 10-bit readings from the
example, suppose you want to send the decimal A/D converter. This shows another advantage of
number 100. Bit 7 corresponds to decimal 128, SPI, which is that the data width is not fixed. No
which is more than 100, so it is ‘0’. Next comes matter whether you send 8, 10, 12 or 16 bits,
bit 6 with a value of 64, so it’s a ‘1’, and you’re the procedure is always the same. If the only
left with 36 still to send. This means that bit 5 objective were to connect two microcontrollers
(32) is a ‘1’, which leaves 4. The next two bits together, it would actually be less effort to use
(bit 4 = 16 and bit 3 = 8) are ‘0’, bit 3 (4) is a an asynchronous serial interface with the TXD
‘1’, and the last two bits are ‘0’. Now you have and RXD lines. The SPI bus, by contrast, is better
sent the binary number 01100100, with each bit for controlling and communicating with external
98
www.elektor-magazine.com | October 2014 | 33
99
34 | October 2014 | www.elektor-magazine.com
MOSI
SCK
ICSP ICSP
CS
+5V +5V
28 27 26 25 24 23 22 21 20 19 18 17 16 15 28 27 26 25 24 23 22 21 20 19 18 17 16 15
GND
AVCC
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
AREF
ATmega328p ATmega328p
GND
GND
VCC
VCC
RES
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
16MHz 16MHz
100n 100n
Figure 3.
22p 22p 22p 22p
140245 - 13
An SPI connection between
two microcontrollers.
hardware. Here the main purpose of the exercise is no chip select line, but the Reset line has the
is to illustrate the transmission protocol. same effect because programming takes place
As previously with the 4094 shift register, a third with the Reset line pulled low. Now we want to
line is involved here—in this case the chip select use these lines exactly as intended. This has the
line /CS. The slash (/) means that the signal on advantage that we can use the hardware SPI unit
this line is active Low. The chip select line allows of the microcontroller, if it has one. With hard-
you to connect several slave devices to a single ware SPI we do not have use program code to
master. In that case they share the data and clock put each bit individually on the data line as in the
lines, but each one has its own chip select line. previous examples, and everything is a lot faster.
When that line is low, the corresponding slave However, we still need a chip select line, and in
knows that it is selected. There’s also another this case we use the B2 line for this purpose.
benefit from using a chip select line. If there is The master uses the MOSI line as the output
any delay in enabling the slave, there may be and generates the clock and chip select signals
some confusion about which bits have already (Listing 3). The process is slowed down a bit by
been transferred. However, if the slave waits until three 1-millisecond delays so that all the signals
it sees a falling edge on its CS input (high to low can easily be seen on the oscilloscope. Besides,
signal transition), it knows that the transfer is we don’t want to make things too difficult for the
starting. And if a noise pulse is read as a clock slave. If you wish, you can test the boundaries
signal, the rest of the data for that transfer is by reducing the delays until transmission errors
trash, but on the next access everything is again start to occur.
as it should be. The three lines are inputs for the slave device
(Listing 4). It constantly waits for specific signal
The ATmega328 also uses the SPI bus for pro- edges on the /CS and SCK lines and then reads
gram download from an external programming in a bit from the MOSI line. Since everything is
device. The following lines are therefore avail- handled by software here, the code must wait for
able on the six-pin programming connector on each edge in a Do loop. This takes a bit of time,
the Arduino board and on the Elektor Extension so data transmission must be slower than with a
shield (ICSP in Figure 3): the clock line Serial hardware SPI implementation. The received data
Clock (SCK) on B5, the write data line Master is shown on the display and on the terminal emu-
Out Slave In (MOSI) on B3, and the read data lator. When you turn the potentiometer on the
line Master In Slave Out (MISO) on B4. There master board, the change is visible on the slave.
100
www.elektor-magazine.com | October 2014 | 35
SPI EEPROM 25LC512 realm, but SPI types are generally preferred in
There is a wide range of ICs available with an the professional realm because they offer espe-
SPI interface, including A/D converters, memory cially high operational reliability.
devices and display drivers. Serial EEPROMs from Figure 4 shows the connections to the Uno board.
Microchip and other companies are available at The pins of the original SPI interface (2x3 pin
low cost and are widely used. The 25LC512 (not header) are again used here. That makes it easy
to be confused with the 24C512, which has a I²C to build a convenient plug-in memory module
bus interface) has a capacity of 64 KB, and it is a by fitting the IC in a socket soldered to a small
good solution when the 1-kilobyte capacity of the 6-pin socket header. You only have to connect
ATmega328’s internal EEPROM is not sufficient. one additional line. This is the chip select line,
I²C EEPROMs are more widely used in the hobby which is again assigned to B2 because the Reset
line present on the connector cannot be used for
this purpose.
8 7 6 5
Here there are two data lines. MOSI (Master Out
HOLD
SI
VCC
SCK
Slave In) is the data output line and is connected
25LC512
to the Serial Input (SI) pin of the EEPROM, while
GND
WP
SO
CS
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
The relatively complex data sheet for the 25LC512
tells you what has to be sent to the device, as well
16MHz
as when and how. After the chip select line has
100n been pulled low, the memory chip first receives a
Figure 4. 22p 22p simple byte command that specifies what action
140245 - 14
Connecting a serial EEPROM. is to be performed. To read data from the mem-
ory, you have to send a ‘3’ command followed
by two address bytes forming the high-byte and
Listing 5. Reading and writing data over MOSI and MISO.
low-byte portions of the address. After that as
Sub Spioutin many data bytes as desired can be read out with
Din = 0 automatic address incrementing (see Listing 6).
For N = 0 To 7 The program displays the sequential addresses
Shift Din , Left and the data bytes that are read out. A brand-new
Din = Din + Miso or fully erased EEPROM always delivers only the
If Dout.7 = 1 Then Mosi = 1 Else Mosi = 0 value 255. Now let’s try to program some data.
Waitus 3 For that we use the byte command ‘2’. However,
Sck = 1
a bit of preparation is necessary first. Writing
Waitus 2
must be enabled by sending the command ‘6’
Sck = 0
(Listing 7). To check whether writing is enabled
Waitus 2
you can read the EEPROM status register, which
Shift Dout , Left
requires sending the command ‘5’. Each action
Next N
is only effective if you pull /CS low at the start
End Sub
and then return it to the high level at the end.
101
36 | October 2014 | www.elektor-magazine.com
102
www.elektor-magazine.com | October 2014 | 37
time for storing the data. If you exceed the page data is read back, that’s exactly what you see.
boundary (I tried it), the result is chaos. Then As usual, the entire program code (UNO_spiEE1.
the data you find in memory is totally different bas) can be downloaded from the Elektor website
from what you wanted to write to memory. For [1]. It performs the following actions in sequence:
this reason, the example program carefully obeys
the rules and writes 128 bytes to the first page • Command 6, write enable
from address 0 to address 127, with the data val- • Command 5, read status register; display for
ues in ascending order from 0 to 127. When the 1 second
103
38 | October 2014 | www.elektor-magazine.com
• Command 2, write 128 bytes starting at fer a lot of data during a single active chip select
address 0 phase. Consequently, this line (on port pin B2)
• Command 3, read memory starting at will still be operated “manually”. The Mode = 0
address 0; endless loop setting is also important, because there are four
different SPI modes.
Data logger The program excerpt in Listing 8 shows how the
One practical application for the 64-KB memory software SPI is used to read data from the serial
is a data logger. The objective here is to acquire EEPROM. The instruction Spiout Dout , 1 sends
measurement data from the ADC4 analog input exactly one byte, which is transferred in the vari-
once per second and store the data. The memory able Dout. In the other direction, the instruction
will be full in approximately 18 hours. Spiin Din , 1 reads one byte, which is then
available in the variable Din. The entire program
You don’t always have to program everything reads all the data from the EEPROM and shows
yourself, since Bascom has a lot of ready-made the contents on the display and on the terminal
functions for many situations. In this case you emulator screen.
have the option of configuring an SPI interface as As usual, the entire program code (UNO_spiLog-
a software interface using any desired port pins or ger.bas) can be downloaded from the Elektor
as a hardware interface using the microcontroller website [1]. It is too large to be listed fully here.
pins designated for this purpose. The hardware Pressing S1 starts a measurement run. It can be
SPI is especially fast and is commonly used for stopped at any time by pressing S2, after which
tasks such as driving graphical displays. However, the stored data can be read out.
this involves a whole lot of parameters that must
be configured for each specific application, which A timer interrupt routine (excerpt in Listing 9)
requires a detailed study of the ATmega328 data is used to control the timing during data acqui-
sheet. Things are a bit easier with the software sition. The voltage on ADC4 is measured and
SPI function, and it provides a reasonably high stored once per second. The 128-byte block size
transmission rate. Although writing your own SPI of the EEPROM is taken into account. At the start
procedure from the ground up is not a bad idea of each block, a write access is started and the
because it allows you to implement the timing current address is transferred, followed by 128
diagrams in the data sheets very clearly, the bytes of data. At the end of the block, the /CS
ready-made software SPI is more convenient and line is pulled high to allow the EEPROM to store
faster, which is why we use it here. the entire block. Since the /CS line is connected
The interface configuration specifies which lines to port pin PB2, LED2 on the shield is lit when the
are to be used. For Din, Dout and Clock we use line is high. The LED therefore flashes each time
the familiar MISO, MOSI and SCK lines, which a block of data has been transferred to memory.
are already available on the ICSP connector. The (140245-I)
SS line corresponds to the /CS line. In this case
Web Link
this line should not be operated automatically by
Bascom because it is usually necessary to trans- [1] www.elektor-magazine.com/140245
104
www.elektor-magazine.com | October 2014 | 39
Microcontroller
BootCamp (7)
The I²C bus
By Burkhard Kainka If you are running short of I/O lines on an Arduino Uno board, a remedy is
(Germany)
available. The I²C bus needs only two port pins and can address up to 127
external ICs. There are countless devices available with I²C interfaces, including
simple port expanders, EEPROMs and a wide variety of sensors. In the final
instalment of our Bascom course series, we show you how the I²C bus works. As
usual, the main focus is on interesting demo applications.
The Inter-IC bus or I²C bus (barring code, also The I²C bus can work with 5 V microcontrollers
sloppily written as I2C) is a two-wire data bus and ICs as well as 3.3 V devices. You can even
consisting of a data line and a clock line, origi- connect both types to the same bus. The two
nally developed by Philips (not: Phillips) for con- pull-up resistors, which typically have a value of
sumer electronics applications. Most television 2.2 kΩ, hold the bus lines at 3.3 V or 5 V (logic
sets or video recorders have a central processor High level) when the lines are not pulled low by
that controls a large number of modules. With a any of the pull-down transistors in the devices
data bus consisting of just two lines, connecting connected to the bus. Any 5 V devices on the
all these modules to the processor is easy. The bus also see 3.3 V as a High level because it is
processor is the bus master, as with the SPI bus, significantly higher than 2.5 V (half of the supply
and the peripheral devices are the slaves. A par- voltage), and of course 0 V is logic Low in any
ticular feature of the I²C bus is that every slave system. This means that you can easily connect
device has a 7-bit address. That allows a large the Arduino Uno board to a 3.3 V slave device.
number of ICs to be connected to the bus with- That’s handy because many recent ICs are only
out interfering with each other. Along with RAMs, designed to operate at 3.3 V.
EEPROMs, port expanders, real-time clocks, A/D The ATmega328 on the Uno board has an inte-
and D/A converters, there are a large number grated hardware I²C interface connected to pins
of special-purpose I²C devices such as display PC4 and PC5. However, Bascom also has spe-
drivers, PLL ICs and many others. An excellent cial commands that can be used to implement
reference book on I2C is available from Elektor, a software I²C interface using any desired port
see Further Reading [a]. pins, and of course you can also write your own
functions to set the lines High or Low using indi-
Data transfer and addressing vidual code lines. Here we only use the Bascom
The I²C bus consists of a serial data line (SDA) software I²C interface, but with the same port
and a clock line (SCL). One bit per clock pulse pins as used by the hardware I²C interface inte-
(as in a shift register) is transferred on the data grated in the microcontroller.
line. Usually the address bits are sent first, fol- These port pins are also used on the Elektor
lowed by the data bits. Each line has a pull-up Extension shield [1] for the Arduino Uno. There
resistor, and each line can be pulled low by any they are routed to the EEC/Gnublin connector K2,
device on the bus. Figure 1 shows the basic which can be used to connect Gnublin modules
bus architecture. The master generates the clock with an I²C interface over a flat cable, such as a
signal. The data can come from the master or module with eight relays [2]. The bus lines also
from the slave. have 330 Ω series resistors, which can be omit-
105
34 | November 2014 | www.elektor-magazine.com
ted if desired. However, they provide protection by the addressed slave. Then the master sends
against false signals resulting from reflections on the data byte, which is also acknowledged. The
long bus lines, and they can help avoid problems connection can be ended now by generating the
that may occur on buses with devices operating stop condition, or another byte can be sent to
at different supply voltages. For example, the the same slave.
input currents of 3.3 V peripheral devices could
exceed the maximum rated value if one of the I²C bus address with data direction
bus lines is accidentally set to a 5 V high level A6 A5 A4 A3 A2 A1 A0 R/W
for a prolonged period. The series resistors limit
the input current to a safe level. If the master want to read data from a slave, the
The I²C bus protocol defines several specific address must be sent with the data direction bit
states that allow every device on the bus to detect set to 1. The master then generates eight clock
the start and end of a transfer: pulses and receives eight data bits. If reception is
confirmed by an acknowledgement on the ninth
• Quiescent state: SDA and SCL are high and clock pulse, it can receive another data byte. At
therefore inactive. The Bascom instruction the end the master terminate the transfer with
I2cinit puts both lines in the quiescent a stop condition when no acknowledgement is
state but without internal pull-up resistors, received.
since they are located externally. Every I²C device has a fixed address. Part of the
• Start condition SDA is pulled low by the address is specific to the device type, and the rest
master while SCL remains high (Bascom can be configured by the user with the address
instruction: I2cstart). lines A0, A1, etc. fed out from the device. These
• Stop condition: SDA changes from low address lines are tied high or low in the circuit
to high while SCL remains high (Bascom to set the address bits. If the device has three
instruction: I2cstop). address lines fed out, such as the PCF8574 port
• Data transfer: The current sender places expander, up to eight different addresses can be
eight data bits on the data line SDA, which set. This means that up to eight devices of the Figure 1.
are shifted out by clock pulses on the clock same type can be connected to one bus. This I²C bus connections
line SCL generated by the master. The trans- port expander provides eight digital outputs, and between master and slave
fer starts with the most significant bit (Bas- the signal levels on the outputs are determined devices.
com instruction: I2cwbyte Data).
• Acknowledge (Ack): The currently addressed
receiver acknowledges reception of a byte +3V3/+5V
by holding the SDA line low until the master ATmega328
2k2
2k2
SDA
Addresses are transferred and acknowledged in
the same way as data. In the simplest case of a
SCL
data transfer from the master to a slave, such
as an output port, the following procedure is
used. The master generates the start condition
and then transfers the address of the port IC
in bits 1 to 7 and the desired direction of the
140293 - 11
data transfer in bit 0 – in this case, 0 for writ-
ing to the device. The address is acknowledged
106
www.elektor-magazine.com | November 2014 | 35
VCC
Master
2k2
2k2
SDA
SCL
107
36 | November 2014 | www.elektor-magazine.com
SCL
P7
P6
P5
P4
VCC
SDA
Int
oscilloscope while checking out your software to Device
2k2 PCF8574 under
see whether there are any signals at all on the
Test
GND
SCL and SDA lines, while the cable to the Gnub-
A0
A1
A2
P0
P1
P2
P3
1 2 3 4 5 6 7 8
lin board is not yet connected. What you have
overlooked is that the I²C bus pull-up resistors +5V
330R
330R
are on the Gnublin board. Since no pull-ups are
present on the host board, everything remains
in suspended animation and there are no signals
28 27 26 25 24 23 22 21 20 19 18 17 16 15
on the I²C bus.
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
The PCF8574 port expander ATmega328p
GND
VCC
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
bidirectional port pins. It does not have a data 1 2 3 4 5 6 7 8 9 10 11 12 13 14
the port status 255 (binary 11111111). You will 22p 22p
140293 - 13
only see low values (logic 0) if the port pins are
actively pulled to ground by an external device.
It is possible to use some of the port pins as may be necessary, which means you will need Figure 3.
outputs and the others as inputs. This requires different test software. Using the PCF8574 port
first configuring all of the input pins in the high expander.
state, the same as the output pins. PCA9555 16-bit I/O port
Figure 3 shows the bus connections and a poten- Sometimes eight more lines are not enough. The
tial application of the port expander as a digital Gnublin port expander module [2] provides 16
tester. The port expander can be powered from I/O lines using an NXP PCA9555 IC. The board
3.3 V or 5 V according to the operating voltage can be plugged directly into the EEC connector
of the item under test. Any desired digital circuit on the Elektor Extension shield. There is also a
with four inputs can be driven using the output Gnublin relay board that uses the same IC.
port pins P0 to P3. For example, you could apply NXP is the successor to Philips, and the PCA9555
an incrementing digital value to these four pins to is the rightful successor to the PCF8574. That’s
obtain all possible combinations of signal levels why the two devices have the same bus address:
on the circuit inputs. Cables and connectors can 64 (&H40). This sort of address recycling makes
also be tested the same way. Every open circuit sense because the address space is limited. In
and short circuit can be detected. any case, why would you need a PCF8574 when
Listing 2 shows an example of how to use a you have a PCA9555? Along with twice as many
mixed set of inputs and outputs. Here pins P0 I/O pins, the new IC has additional functions
to P3 output a continuously incrementing digital such as data direction control and inversion of
value. Pins P4 to P7 are used as inputs and must the input data.
therefore be set high in the write operation (Or Figure 4 shows how the IC can be connected to
&B11110000) The IC is addressed twice: first in the Elektor Extension shield using the EEC con-
the write direction (address 64) and then in the nector. The I²C bus lines and supply voltage lines
read direction (address 65). Incrementing the are connected using a flat cable. The two pull-up
value on the four outputs is the simplest possi- resistors on the Gnublin board are connected to
ble type of test stimulus. Depending on the item the bus lines by a pair of jumpers. These jump-
under test, a completely different test sequence ers must be fitted if only one board is connected.
108
www.elektor-magazine.com | November 2014 | 37
If you use several boards, perhaps connected That yields a bus address of 64. A total of eight
using the Gnublin distributor board [2], make boards can be connected, with addresses from
sure that the pull-up resistors are only enabled 64 to 72. With 16 I/O pins per board, that gives
on one board. It is possible to use several port you a grand total of 128 I/O lines.
expander boards because each board has jump- Listing 3 shows an application for the PCA9555,
ers for configuring address lines A0 to A2. In the which can also be used for testing other circuits.
default state, all three lines are tied to ground. As in the previous example, the port pins are
109
38 | November 2014 | www.elektor-magazine.com
2k2
2k2
After that you can send or receive one or two 24
VDD
bytes of data. For example, if you want to con- 2 1 I/O0.0
4
5
figure port 0 (with eight pins) and port 1 (with I/O0.1
SDA 23 6
SDA I/O0.2
the other eight pins), you first send the com- 22 7
SCL I/O0.3
PORT 0
SCL
mand ‘6’ after the address and then write two I/O0.4
8
9
data bytes, which are placed in registers 6 and 7. I/O0.5
1 10
INT I/O0.6
Zero bits in these bytes mean that you want to 3V3 GND 11
I/O0.7
14 13
configure the corresponding pins as outputs. One EEC PCA9555D
13
bits stand for inputs. In our example, all pins of I/O1.0
14
I/O1.1
port 0 are configured as outputs and all pins of 15
I/O1.2
port 1 are configured as inputs. Incidentally, pins I/O1.3
16
PORT 1
21 17
configured as inputs have integrated high-imped- 2
A0 I/O1.4
18
A1 I/O1.5
ance pull-up resistors (approximately 100 kΩ), 3 19
A2 I/O1.6
so open inputs are read as logic 1. You can use I/O1.7
20
VSS
the following command byte values: 12
Figure 4.
0 Input Port 0 140293 - 14
The PCA9555 on the Gnublin
1 Input Port 1 board.
2 Output Port 0
3 Output Port 1 for each command. In order to read a port, the
4 Polarity Inversion Port 0 IC must also be addressed again with the read
5 Polarity Inversion Port 1 bit set (address 65). If you examine the code
6 Configuration Port 0 closely, you may wonder why there is no I2cstop
7 Configuration Port 1 instruction. That’s because the code implements a
‘repeated start’ without a previous stop condition,
The commands 2 (Output Port 0) and 1 (Input since the two accesses always belong together:
Port 1) are used iteratively in the data loop of the writing the command to select the register to be
sample program. The IC must be addressed anew read and reading the register contents.
• I²C EEPROMs up to 64 KB (for example, the 24C512). These can be used to build data loggers
and lots of other things.
• The CY27EE16 is a crystal clock generator that can be programmed over the I²C bus. It is
used in the Elektor Software Defined Radio project. Software control with a microcontroller
opens the door to new possibilities.
• The SI4735 is a complete AM/FM receiver and has already been used in several Elektor
projects along with Bascom. Any desired frequency can be set using just a few I²C commands.
• High-resolution A/D and D/A converters often have I²C ports. One example is the ADS1115
16-bit A/D converter recently described in Elektor.
If you want to delve deeper into this subject, you can even build your own I²C bus IC. A Bascom
library for programming I²C slave devices is available for this purpose. That is a bit more
difficult than programming a bus master because the slave device must be able to handle the
bus speed set by the master. The Mastering the I2C Bus book has all the details.
110
www.elektor-magazine.com | November 2014 | 39
OSC
SCL
VCC
SDA
AGND
Ref
A0
Ext
A/D converter can come in handy. Unlike a PWM
PCF8591
2k2 output, it delivers a true DC voltage. You can use
DUT
GND
this to build a simple diode tester that measures
Ai0
Ai1
Ai2
Ai3
A0
A1
A2
1 2 3 4 5 6 7 8 the forward voltage at a defined current level (see
+5V Figure 5). The circuit operates at 5 V so that it
330R
330R
1k
28 27 26 25 24 23 22 21 20 19 18 17 16 15
ten right after the bus address is sent. A control
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
D5
D6
D7
B0
X1
X2
Figure 5.
Listing 4. A diode tester using the PCF8591.
A diode tester using the
PCF8591. '------------------------------------- I2cstart
'UNO_I2C4.BAS AD/DA PCF8591 I2cwbyte 145 'read
'------------------------------------- I2crbyte D , Nack 'Ain0
$regfile = "m328pdef.dat" I2cstop
$crystal = 16000000 Print D
$baud = 9600 Locate 2 , 5
Lcd D
Dim N As Byte Lcd " "
Dim D As Byte Waitms 100
Dim U As Word N = N + 1
... If D >= 50 Then Exit Do
N = 0 Loop
Do Cls
I2cstart Locate 1 , 1
I2cwbyte 144 'write Lcd " 1 mA"
I2cwbyte 64 'DA enable U = N - D
I2cwbyte N U = U * 20
Print N; Locate 2 , 2
Print " "; Lcd U
Locate 2 , 1 Lcd " mV"
Lcd N End
Lcd " "
111
40 | November 2014 | www.elektor-magazine.com
112
www.elektor-magazine.com | November 2014 | 41
Motion-Detector
Camera Trigger
using Arduino
By Rolf Blijleven I consider the Arduino to be very similar to Lego: siderable advantage of the Arduino, compared
(Netherlands) you can make all manner of things with it. This to other embedded platforms, is that there is a
is useful and instructive to do, and it results in tremendous wealth of firmware to be found, just
something useful. You can then use it for a while, free from the Internet.
and whenever you feel like it, you can take it After this, I wanted to trigger the remote control
apart again and use it to make something else. using a motion detector. There are many nice
A while ago, I made an infrared remote control for solutions to be found for this on the DIY market
my Nikon D80 using an Arduino. Not because such and on the internet, but these usually require
a remote control is that expensive, but because an AC power adapter. I didn’t want that, it had
making one yourself is much more fun and using to work without any wires. By chance, I spotted
an Arduino it offers many more possibilities. More- at the supermarket a night light powered from
over, it turned out to be child’s play: An IR-LED, batteries, with a motion sensor, for a couple of
a resistor and a piece of software that I found dollars (Figure 1). That won’t break the bank.
on the internet, nothing else is required. A con- It just went with the weekly groceries.
113
24 | April 2014 | www.elektor-magazine.com
Figure 3.
A sample schematic from
the datasheet for the
TL0001 made by the Chinese
company Treasure Link
Technology. Although it
looks very similar, it does
not correspond completely
with the actual circuit in the
night light.
114
www.elektor-magazine.com | April 2014 | 25
115
26 | April 2014 | www.elektor-magazine.com
My camera, a Nikon D80, turned out to have some if (val > 200 || timeCounter == quarter
unexpected characteristics. When you switch this ) {
camera into IR Remote mode it waits a while for timeCounter = 0;
an IR-command. If that does not happen it will
takePicture();
switch out of IR-mode by itself. Any command
delay(500);
after that interval is ignored. For my application
}
(nature photography) this was undesirable. The
waiting time can be adjusted in the camera up
to a maximum of 15 minutes. The source code for the firmware for this project
That is why the firmware will give the IR-com- is a free download from the Elektor website [5].
mand ‘stay awake’ if there has been no motion The binary file is only 4 KB, so with an Arduino
detected for 14 minutes. In this way you can with 32 KB flash memory there is plenty of spare
leave the camera for days, waiting for that one space for to add your won features. A trigger
rare animal to go by. based on sound would be another possibility.
This interval can also be made shorter. Without (130265)
the PIR-board you can then also use it to make
Internet Links
time-lapse movies of, for example, flowers that
grow and open. [1] www.treaslink.com/Upload-
Files/2010531152721141.pdf
To allow this interval to be changed in the code in [2] www.e-ele.net/DataSheet/BISS0001.pdf
a more obvious way, requires a little bit of com- [3] www.bigmike.it/ircontrol
putation. We are using Timer1, this is a 16-bit
[4] http://luckylarry.co.uk/arduino-projects/
timer, so counts from 0 to 65536. If we let the
arduino-ir-remote
timer start from a timerPreload of 3036 it will
count 65536 - 3036 = 62500 clocks and give an [5] www.elektor.com/130265
interrupt. The Duemillenove runs at 16 MHz; with
a prescaler at 1024 this becomes 15625 Hz, so
we will have a Timer1-interrupt exactly every
62500/15625 =4 seconds (ignoring any inac-
curacies of the clock). In the code this is imple-
mented as follows:
ISR(TIMER1_OVF_vect) {
TCNT1 = timerPreload;
timeCounter +=1;
}
116
www.elektor-magazine.com | April 2014 | 27
Intel Galileo-Arduino
Engineering sample hits E-Labs desk
By Thijs Beckers (Elektor Labs)
The Intel Galileo is a co-production between The prominent Intel chip is flanked by two Micron
Ivrea, Italy-based Arduino and big-gun chipmaker MT41K128M8JP-125:G 1-GBit DDR3L memory
Intel Corporation. Arduino’s new Arduino Certi- chips. The ample amount of 0402-shaped capac-
fied family starts off with the Intel Galileo board itors and resistors are all accounted for in the
and is supposed to address beginners and next fairly large schematic circuit, which—as cus-
level developers. With a 400-MHz 32-bit Intel tomary with all Arduino boards—is available for
Pentium® instruction set architecture compatible downloading [1] and comprises a whopping 27
processor, the latter group seems to identify the pages! BTW, the 3.5-mm audio connector is not
target market better than the former. But let’s used for audio, but rather represents the legacy
have a quick look at the engineering sample on RS-232 interface.
our desk. After unpacking, reading the Quick Start Guide
and downloading the development tools from Intel
Looking at the type, appearance and sheer [2]—we used Intel Galileo Arduino SW v1.5.3 for
amount of electronic components, this Arduino Windows v07.5—we unzipped the software and
family member sure looks like the Big Daddy of copied it to the root of our hard drive, as sug-
them all. Compared to the Uno it plays in a far gested in said guide. Using a different location
higher league and even the Yún doesn’t seem to could result in errors due to path length prob-
come close. Besides the standard Arduino pin- lems. And it did after we tried to run it from a
header configuration, we see a lot of other I/O, different location.
like a JTAG interface, an ICSP header, RS-232, The power supply must be connected prior to
and a USB client as well as a USB host interface. the USB cable. Otherwise the board might get
And of course an RJ-45 network port. damaged. The included wall wart is rated at a
hefty 5 V, 1.5 amps, which suggests this Arduino
matriarch is quite power hungry. After the client
USB port on the Galileo is connected to your PC
[1] http://arduino.cc/en/ArduinoCertified/IntelGalileo
or Mac, the driver installation process begins. Fol-
[2] https://communities.intel.com/community/makers/software/drivers lowing the instructions from the Getting Started
117
64 | March 2014 | www.elektor-magazine.com
118
www.elektor-magazine.com | March 2014 | 65
Arduino Yún
Bridging Two Worlds?
Source: www.arduino.cc
By Clemens Valens The Cloud is where you have to be today. However, to connect to the cloud you
(Elektor.Labs)
need an internet connection, preferably wireless. This is where the new Arduino
Yún board can help. Yún means cloud in Chinese and the board is equipped with a
Wi-Fi module to connect to it. But the Yún is more than Wi-Fi—it also runs Linux.
Arduino on Linux? Linux on Arduino? How does that work?
What’s on board? point (AP) and router platforms. The heart of this
Quite a lot, actually. Let’s start with what we are SoC is a 32-bit MIPS 24K processor, which goes
familiar with: the Arduino part. to show that you don’t always have to use ARM.
Like all Arduino boards the Yún is based on a micro- The SoC communicates with the Leonardo part
controller unit (MCU) from Atmel. In this case it is over a serial link.
an ATmega32u4 from the 8-bit AVR family. This The Atheros chip provides Wi-Fi connectivity, but
is the same MCU as the one on the Arduino Leon- it’s also connected to an Ethernet connector pro-
ardo board, and the Yún can actually be viewed as viding a wired network interface. Furthermore, it
a Leonardo with an on-board Linux co-processor has access to an AU6350 single chip integrated
a.k.a. Wi-Fi/Ethernet/USB/SD-card shield. USB2.0 hub and multimedia card reader controller
This complicated shield is built around an Ath- from Alcor Micro (mounted on the underside of
eros AR9331 chip (we have to trust the Arduino the Yún board—Figure 1). This chip offers the
documentation on this as there is no print on the Yún its USB host connector and micro SD-card
metal shield covering it). According to the data- slot. The AU6350 communicates over a USB con-
sheet of this chip, it’s a highly integrated IEEE nection with the Atheros chip.
802.11n 1x1 2.4 GHz System-on-a-Chip (SoC) Summarizing, the Arduino Yún combines—
for wireless local area network (WLAN) access on a single PCB the size of an Arduino Uno
119
108 | January & February 2014 | www.elektor-magazine.com
120
www.elektor-magazine.com | January & February 2014 | 109
USB
Rx Tx Host
ATmega Bridge Linino
Tx Rx
32U4 AR 9331
SD
Card
Getting started ino.local did not work for me). You should now
Breaking with traditions is one thing, but how see a page that asks for a password. The default
does it affect the ease of use of the board? Let’s password is “arduino”. Enter it and click on the
find out. Log In button. On the Welcome page that opens,
First of all, you need a micro USB-B cable to click on the Configure button.
connect the board. I have hundreds of Mini USB Set the time zone and select the Wi-Fi network
cables, but only one Micro USB cable as part of my that you want to use in the future, enter its pass
phone battery charger. While you are looking for phrase, etc. I did not change the default pass-
a suitable cable, download in the meantime the word, because I know I will forget it. I also set
Arduino IDE 1.5.4 or later (I downloaded version the REST API (the API that allows you to issue
1.5.4 r2), it’s only 134 MB and you will need it. Arduino port commands as URLs, see below) to
Install the software before connecting the board. open, but that is of course up to you. When done,
Now connect the board to the computer. On click Configure & Restart.
OSX or Linux Ubuntu 10.0.4 and up everything (Re)Connect your computer to the Wi-Fi net-
should work straight out of the box. On Windows work that you selected for the Yún and start
you may have to install a driver or two. Luckily the Arduino IDE. In the ToolsBoard list choose
a Windows installer is available which has the the Yún, in the ToolsPort list pick the very last
advantage that it can install the necessary driv- option “Arduino at xxx.xxx.xxx.xxx (Arduino
ers automatically. If you prefer to do it manu- Yún)” where the x’s form a valid IP address
ally, the drivers are in the drivers folder of the (192.168.2.6 in my case).
IDE distribution. Now you can try out a sketch. Open for instance
After connection and installing drivers where the Blink (or BlinkWithoutDelay) example and
needed the board should be operational. On my click the Upload button. After compilation you
board this meant that the red LED L13 started will be prompted for a password. Enter the one
blinking irregularly and the green On LED lit up. you assigned to your board (“arduino” for me
When, on your computer, you inspect the avail- since I didn’t change it). Uploading of the sketch
able wireless networks, you should see a new starts, the board is restarted (you may hear a
network named “Arduino Yun-XXXXXXXXXXXX” USB disconnect/connect sound on Windows) and
(where the X’s represent hexadecimal characters). the sketch is executed. Note that you don’t have
You may now be tempted to upload a sketch to to enter the password every time you upload a
the board, but the Guide to the Arduino Yún (‘The new sketch, you have to do it only once at the
Guide’ from here on) on the Arduino website [1] beginning of a programming session.
suggests that you first set up the Wi-Fi connec- That was not too bad, was it? It seems a bit silly
tion, so let’s do that now. to program a board that is physically connected to
Connect your computer to the Yún Wi-Fi net- your computer over Wi-Fi, but why not? BTW, it
work and open a browser. Point it to the address is also possible to use the Ethernet port to do all
192.168.140.1 (the suggested link http://ardu- this, but I leave that as an exercise for the reader.
121
110 | January & February 2014 | www.elektor-magazine.com
Linux
So, now you have an Arduino Leonardo that you
can program wirelessly over Wi-Fi or with a cable
over Ethernet. However, that was probably not
the main reason why you decided to invest in an
Arduino Yún board—more likely it was the Linux
part that triggered your interest.
When you look at the circuit diagram of the Yún
you may notice signal labels referring to ‘Hornet’.
Feeding ‘Hornet’ to a search engine together
with ‘AR9331’ takes you to OpenWRT, an open-
source community project that allows commercial
network routers to be used as Linux computers.
The Atheros processor on the Yún runs such an
OpenWRT-based Linux distribution named Linino.
Linino can be configured wirelessly. Enter the
Yún’s IP address in a browser, log in, and then
click on the “advanced configuration panel (luci)”
link. This will open a status page from where you
can access all kinds of parameters. At the top is
a black menu bar with many options. Take your
time to browse around. Have a look at the kernel
log page to see what happened during start-up.
Use the SystemSoftware menu to install or I’d suggest entering the Console example sketch Figure 3.
remove software, use the SystemStartup to from The Guide. When you compile and upload Welcome to the Linino
see what is loaded during Linux startup. Here you it to the board, you will be able to use the serial command shell.
can also add commands to be executed at the monitor included in the Arduino IDE to communi-
end of the boot sequence. Clicking the “Arduino cate with the board (as if it was a normal Arduino
Web Panel” link at the bottom of each page takes board). After receiving the welcome message
you back to the Yún’s homepage. type a capital ‘H’ to switch on the red LED L13.
Typing an ‘L’ will switch the LED off.
Take me to the bridge To make things a bit more complicated, you can
According to The Guide, you can access the do this also from a terminal like Tera Term or
Arduino pins (or ports) from the browser by typ- PuTTY. I used Tera Term and here is how to do it:
ing in the right URL (through the REST API). For Open Tera Term. In the New Connection dialog
example, it is possible to make the URL (replace select TCP/IP; for the Host enter the IP address
the first “arduino” by the name of your board) of your Yún. Set the Service to SSH and the Port
to 22. Click OK. (You may get a warning about
http://arduino.local/arduino/digital/13/1 the Host not listed in your cache or something;
simply allow whatever the program wants.) Now
set digital output pin 13 as if you called the a new dialog window is opened where you are
Arduino API function required to enter a user name (“root”) and a
Passphrase (the password you set for your board,
digitalWrite(13,1); “arduino” by default). If all goes well you should
now see the screen from figure 3 meaning that
You can also go the other way around and con- you are connected.
trol Linino Linux from within a sketch. This is
made possible by the Arduino library “Bridge” At the prompt (“root@Arduino~#”) type “telnet
that allows you to access the USB, Ethernet, localhost 6571” and hit Enter. Nothing will hap-
Wi-Fi and SD card devices in a sketch as well as pen. Nothing? Try typing ‘H’ or ‘L’. With these two
run scripts and communicate with web services commands you can control LED L13. Cool, right?
on the Linux module (Figure 2). The Guide gives an example sketch that runs a
To familiarize yourself a little with the Bridge, little Linux program named “curl”. Before trying
122
www.elektor-magazine.com | January & February 2014 | 111
Feeling lost?
Raspberry Pi I don’t know about you, but all this leaves me
Looking at the Arduino Yún one cannot help thinking of Raspberry Pi (R- with a rather strange feeling. On the one hand I
Pi), the $35 Linux board. So how do the two compare? have the familiar and extremely simple Arduino
The RPi has been available for over a year now, a large user community IDE running on my PC while on the other I have
has developed and many RPi to Arduino projects have been published access—through a browser on the same PC—to
together with lots of projects that accomplish the same tasks as those a web interface with tons of Linux options. Both
targeted by the Yún. The RPi does not have on-board Wi-Fi, but you programs target the same little blue board lying
can stick a cheap Wi-Fi dongle in its USB port. The RPi has support for next to my PC.
graphical displays, the Yún does not. The RPi requires Linux knowledge
whereas the Yún does not. The R-Pi does not need a host computer The Guide almost starts with Python and other—
to develop applications on, the Yún does. However probably the most to me unfamiliar—Linux concepts, and I cannot
important difference: Yún will cost you twice as much as an RPi help feeling a bit lost. Why would I use a little
microcontroller to run a Linux script if I can run
the script also directly on the on-board Linux
the sketch you may want to see its result in the system? What does the Arduino interface add
Linux console. At the prompt, type for the Linux programmer? Arduino is targeted
at people with little to no programming experi-
curl http://arduino.cc/asciilogo.txt ence, yet the user is supposed to know how to
control Linux from the command line?
Press Enter and wait a few seconds. You should
see appear the Arduino logo in ASCII (Figure 4). It is not that bad. For starters, you can skip the
The point of this demonstration is to show that whole Linux bit and just be happy with how the
whatever you can do in the Linux console, you Arduino team configured it for you. Using the
can also do it from within an Arduino sketch. REST API you can control the Arduino board from
a web application through URLs that you process
To be honest, I have tried the example sketch and in your own sketch. Thanks to the Temboo [2]
it compiles and uploads without problems, but I library you can easily interface an Arduino sketch
have not succeeded in making the result visible. with Twitter, Dropbox, Gmail, MySQL and many
I must have missed something here. more web services. For the more adventurous
there is Spacebrew [3], a software toolkit based
on Websockets to interconnect interactive things.
Once you master the Yún’s built-in Cloud support
you can go further and install your own programs
and utilities on the Linux module to enhance its
capabilities and your options.
Steep
The Arduino Yún is not for beginners. For exam-
ple, connecting the board to the Arduino IDE is
not Plug ’n’ Play at all. And once you get it going
you are supposed to use it for client-server web
applications. However, once you master all this,
the Yún can be an excellent tool to create fun
applications.
(130378-I)
123
112 | January & February 2014 | www.elektor-magazine.com
By Paul Court (UK) For the clock I decided against the traditional So here we have the project, a Numitron bulb,
7-segment LEDs in favor of something much more clock & thermometer built on a single board. The
beautiful in the form of ex-Soviet Numitron tubes. project is based on the Arduino UNO, meaning the
Numitrons are cousins of the Nixie tubes featured UNO and its IDE were used as development tools
in several Elektor projects, but run on about 4 only. In the final project there are no shields, no
volts rather than >150 volts, hence are more modules and no plugins—just an ATMega328 with
suited to experimentation. Just like the Nixies, peripheral hardware devices around it, although
Numitrons are readily available on sites such as that may be oversimplifying the build ahead, as
eBay at very reasonable rates. we’ll see.
124
30 | October 2013 | www.elektor-magazine.com
Circuit description
Looking at the circuit diagram in Figure 2, you
might be surprised to see that no multiplexing
is applied to the display elements. Instead, each
of the four digits of the clock has its own driver
IC type SN74LS47D. You can’t really multiplex
the IV-9, being a tungsten filament bulb it’s just
too slow and either flickers, or is too dim. Each
channel therefore has its own driver and a set
of four BCD drive lines, except V1, the highest
clock digit, which needs only two. While using
four 74LS47’s and no latch or multiplexer uses
up more Atmega pins, it is easier to understand
when it comes to coding. Each tube having its Figure 1.
own BCD lines, it can be directly addressed by Hey, the Numitrons have
arrived all the way from
toggling the pins High or Low.
Dot-ua Land. And a good
read included free! Paid for
On supply voltages, the circuit has two inter-
in US$ using a credit card,
nally: +5 V from an ordinary 78L05 regulator and cleared by customs, the
(IC1) and +4.00 V (adjustable on P1) from a little little tubes came all the way
switch-mode supply around the familiar MC34063 from the Ukraine.
125
www.elektor-magazine.com | October 2013 | 31
+5V
R1 P1 MOD1
18
17
16
6
VCC
+3V3
+5V
0R18
K1
VCC
-VIN
8
DRVC IC2 R3
6
RESET RI
13
IC1 7 C16
DCD
10k
7 MC34063AP 8 RES
L1 V+ 78L05
IPK +5V DSR
9V 1
SWC SWE
2 1 3 1
CBUS4 DTR
9
100n
18uH 2 10
TCAP
GND
R2 4 12
3
C3 C4 D1 C5 C1 C2 CBUS1 RXD
5 14
GND
10k
CBUS0 CTS
220p 22u 100n 100n TxD
100u 25V 1N5819 16V BOB-FT232
15
+5V
C6 C7
+5V +5V
+5V +5V +5V 100n 100n
20
21
C8
7
R4 S1 S2
AVCC
VCC
AREF
C9
23 4A 100n
8
R7 R5 R6
4k7
PC0
24 4D
VCC
Vbat
4k7
RxD RxD 2 25 4C 7 1
10k
PD4 PC6
1
10k
10k
2B 11
DS18S20 PD5 ATmega328-PU
2C 12 14 2A
4
PD6 PB0
2D 13 15
PD7 PB1
16 3A
PB2
17 3D
PB3
18 3C +5V
PB4
19 3B K2
PB5
GND
GND
PB6
PB7
3C 1 2
3B 3 4 3D
9
8
22
10
X2 RES 5 6
V+ V+ V+
2A
2B
4A
1A
2C
V+
3A
4B
1B
2D
4C
3B
3C
4D
3D
7
1
2
6
7
1
2
6
7
1
2
6
A
B
C
D
A
B
C
D
A
B
C
D
A
B
C
D
16 13 5 16 13 5 16 13 5 16 13 5
VCC Y0 a 1 VCC Y0 a 1 VCC Y0 a 1 VCC Y0 a 1
12 3 CA 12 3 CA 12 3 CA 12 3 CA
IC5 Y1 b IC6 Y1 b IC7 Y1 b IC8 Y1 b
3 11 4 3 11 4 3 11 4 3 11 4
C12 LT Y2 c C13 LT Y2 c C14 LT Y2 c C15 LT Y2 c
4 10 8 4 10 8 4 10 8 4 10 8
BI/RBO Y3 d BI/RBO Y3 d BI/RBO Y3 d BI/RBO Y3 d
100n 5 9 9 100n 5 9 9 5 9 9 100n 5 9 9
RBI Y4 e RBI Y4 e 100n RBI Y4 e RBI Y4 e
15 6 15 6 15 6 15 6
Y5 f Y5 f LA1 Y5 f Y5 f
8 14 7 2 8 14 7 2 8 14 7 2 8 14 7 2
GND Y6 g dp GND Y6 g dp GND Y6 g dp GND Y6 g dp
SN74LS47D SN74LS47D SN74LS47D SN74LS47D
IV-9 IV-9 IV-9 IV-9
V+ 120740 - 11
126
32 | October 2013 | www.elektor-magazine.com
(IC2). The IV-9’s filament voltage range is actu- plus this delay gives time for the RTC to recover
ally 3.15-4.50 volts and you want to set P1 to a between reads. If you read too often you get rub-
level that best suits your viewing. Each filament bish back! BTW La1 is a “grain of wheat” (GOW)
draws about 19.5 mA. The whole clock requires a style miniature light bulb as used in dollhouses
DC input of 7.5 V unregulated at about 500 mA. and model railways.
The internal supply voltages were chosen as On each cycle the code starts by checking if the
5 volts and 4 volts (3.00 V-4.50 V), and not set button (S2) has been pressed and if not then
3.3 V only, because updates the indicator bulb. At this point the RTC
is read and, using some basic Integer division
• the higher voltage allows the Atmega to math, the hours and minutes are each split into
work at 16 MHz; Tens and Units, each value is then sent to the
• 3.3 V is too low considering the 74LS47D correct Numitron digit.
LS-TTL ‘open collector’ outputs powering the For simplicity of the code each digit has its own
filaments from the V+ do not quite pull down routine but they all work in a similar way with the
all the way to 0 V when On. only exception being the ‘Hour Tens’ that only has
two binary bits (on IC5/V1). Finally using another
The heart of the clock is an ATmega328P, which useful math trick (Bitwise AND) we then convert
is the chip used in the UNO and Duemilanove the decimal number into Binary ready for the
series of Arduino boards. If purchasing or using 74LS47’s. More info on Bitwise operations may be
a chip with an Arduino Boot Loader pre-installed found at [2]. While it looks a little long winded
the Duemilanove version is preferred. it’s actually very simple, and a really useful way
Programming of the ATmega328P is via the stan- to convert Decimals to individual Binary digits.
dard 6-pin ISP header and AVR Studio, or via the We have two extra functions to add value and
optional Elektor FT232 BOB, the BOB being used interest. The first is called on the 10-second and
by the Arduino IDE software (V1.0 or greater) 30-second points, reading and displaying the tem-
with the Duemilanove Bootloader pre-pro- perature in degrees Celsius. This works as above
grammed on the ATMega. As far as the Arduino using Mod and Bitwise math to take the reading,
IDE knows it’s talking to a standard ‘Arduino convert it to binary and display it via the 74LS47s,
Duemilanove w/ ATmega328’ Board. the exception to this being the lowest digit that
The real-time clock (RTC) is a DS1307, and the is sent a decimal ‘10’ which makes the 74LS47
temperature sensor, a DS18B20—both compo- on that digit (V4) to display a ‘c’ thus complet-
nents are well supported in the Arduino and AVR ing the display to show for example ‘22.5c’. The
communities. Their ‘One-Wire’ data is read by 74LS47 is unable to display ‘f’, hence a Fahren-
the ATMega controller on the last two available heit readout is not supported.
port lines, PD2 (temperature) and PC4 (RTC). Lastly on the 50th second the showdate() rou-
The expected ISP (in-system programming) con- tine is called that displays the date, month and
nector is available (K2) to allow the ATmega chip then year.
to be programmed without removing it from its
socket. The code is very basic and I’m sure you could
MOD1, an Elektor BOB-FT232 (break out board), find other functions for the clock (Egg timer?),
is optional. It provides USB connectivity and is so why not join the project at Elektor.Labs [4]
useful to have if you are keen on working on the and show us your code/updates/tweaks, or just
clock software within your Arduino environment. share your ideas and suggestions.
127
www.elektor-magazine.com | October 2013 | 33
r p c bs
to
COMPONENT LIST
er
.e l e k
v i c e.c
Resistors C4 = 220pF
ww
R1 = 0.18Ω 3W C5 = 22µF 25V radial om
R2,R3,R7,R8,R9 = 10kΩ C9 = 10µF 50V radial w
R4,R5,R6 = 4.7kΩ C10,C11 = 22pF
P1 = 4.7kΩ preset, top adjust
Figure 3. Inductors
The double sided printed Capacitors L1 = 18uH choke, 3.4A, 0.036Ω (Panasonic type
circuit board designed for C1,C2,C6,C7,C8,C12,C13,C14,C15,C16 = 100nF ELC10D180E)
the project by Elektor Labs. C3 = 100µF 25V radial
Semiconductors
D1 = 1N5819
IC1 = 78L05
IC2 = MC34063
IC3 = ATmega328-PU, programmed, Elektor Store #
120740-41 [2]
IC4 = DS1307
IC5,IC6,IC7,IC8 = 74LS47
IC9 = DS1820
Miscellaneous
V1,V2,V3,V4 = IV-9 Numitron tube
JP1 = 2-pin pinheader + jumper cap, 0.1’’ pitch
X1 = 32.768kHz quartz crystal
X2 = 16MHz quartz crystal
MOD1 = BOB-FT232R (optional), Elektor Store #
110553-91
Bt1 = CR2032 battery
S1,S2 = pushbutton, SPST, chassis mount
K1 = power jack, 2.1mm, PCB mount
K2 = 6-pin (2x3) boxheader
La1 = 5V 300mW light bulb, GOW
PCB # 120740 [2]
Internet Links
[1] Nixie VU Meter, Elektor November 2012,
www.elektor.com/110744
[2] www.elektor.com/120740
[3] http://playground.arduino.cc/Code/
BitMath#bitwise_and
[4] www.elektor-projects.com/project/nu-
Figure 4. To set the clock, jumper JP1 must be removed
mitron-arduino-clock-and-thermome-
The assembled and tested (‘Run’ mode). Pressing the set button S2 will
board, ready for use. ter-120740.12460.html
cause the setclock() routine to be called, firstly
displaying the Minutes. Pressing the adjust but-
ton S1 counts up by 1 on each press or keeps
adding 1 every ½ second if holding the button.
128
34 | October 2013 | www.elektor-magazine.com
Difference #1
Okay, the GR Sakura board
is pink! But then, look at
the specs—Pink is Power!
Difference #2
It’s 8 bit, 16 MHz and lim-
ited memory of the AVR
controller against full 32
bit, 96 MHz and massive
memory capacity of the
Renesas processor.
The real question is, which
applications will actually
unleash GR Sakura’s full
potential.
Difference #3
The USB Host functionality
on the Sakura board shows
potential. It is implemented
with a Mini-B connector,
while a Type-A connector
can be fitted on the back
side of the board.
Mini B connector
Type B connector
USB Type A connector provides Host support
Board runs off USB voltage by default.
Board runs off USB voltage by default
129
90 | July & August 2013 | www.elektor-magazine.com
(130177)
130
www.elektor-magazine.com | July & August 2013 | 91
By Jens Nickel If a suitable protocol is defined, hardware connected to your PC can be controlled
(Elektor Germany Editor)
using a terminal emulator program. The necessary firmware can be rustled up
quickly using Elektor’s ‘Embedded Firmware Library’ (EFL), and the code can be
written without knowledge of whether a UART or some other interface will be used
for the connection. The protocol described here is ideal for experimentation and
development purposes.
In the previous edition we described our modular a code file for the board and a code file for the
‘Embedded Firmware Library’ (EFL), written in microcontroller.
the C programming language. This helps begin-
ners and old hands alike to develop code for However, the idea of modularity is taken even
an embedded project that is independent of the further in the EFL. Protocol libraries allow pro-
underlying hardware and which can therefore grams that communicate to be written without
easily be ported from board to board and from knowledge of the communication channel that will
microcontroller to microcontroller. This is achieved be used: it makes no difference whether com-
using a hardware abstraction layer consisting of mands and data are transferred using a UART and
131
26 | June 2013 | www.elektor-magazine.com
Mini protocol
When trying to get code up and running simple
tests such as turning a LED on and off or reading
a digital input can be very helpful. Often, how-
ever, a microcontroller board will not have any
buttons to help you carry out such tests. One
alternative option is to control the board from
a PC, for example over a serial interface. If the
protocol is restricted to ordinary printable ASCII
characters it is easy to use a terminal emula-
tor program to send commands to the board,
avoiding the need for special-purpose software
on the PC.
Map, blocks and board pins The peripheral blocks are shown in the middle, Figure 1.
The BlockProtocol includes the ‘x’ command spe- while on the right is a list of the pins on the board. Dump of the EFL tables
cially designed for use in development with the This is particularly important information for any- as seen in the terminal
emulator. On the left
EFL. The command causes the board to send a one planning to adapt or write an EFL board file
is the map with the
dump of the EFL tables to the PC. The tables are for a new board. More extensive documentation
microcontroller features in
used by the hardware layer to determine the on the internals of the EFL are given in the extra use (red numbers in the
microcontroller pins and registers that need to document that can be downloaded at [1]. board block diagram); in
be changed in response to a function call. The the middle is the block table
entries are specific to the board and any expan- Control via UART (blue numbers); and on the
sion board that might be attached. The screenshot We shall now show by example how easy it is to right are the board pins
in Figure 1 shows what appears in the terminal use the protocol in your own microcontroller appli- (green numbers).
window when the board shown alongside is used. cations. We will use an experimental node, famil-
On the left is the ‘map’ including the features of iar from our series on the ElektorBus. This is a
the microcontroller such as the UART and ADC very compact board based on an ATmega328. As
that are set up by the board initialisation code. usual we will connect the node to the PC using its
132
www.elektor-magazine.com | June 2013 | 27
A little code
In the main source file of the application code
itself we need to include the library header files
as follows.
#include “UARTInterfaceEFL.h”
Figure 2. RS-485 interface and a USB-to-RS-485 converter #include “BlockProtocolEFL.h”
Our small microcontroller (Figure 2). Instead of the ElektorBus protocol
board can be controlled over we will use our new text-based BlockProtocol. The main function in an EFL-based project is
a UART/RS-485 interface always structured in the same way: see List-
with the help of a simple
The necessary firmware for the microcontroller ing 1. In the application set-up function, which
ASCII text-based protocol
can be downloaded at [4]; the application can is called whenever the application starts up, we
and a terminal emulator
program. also be found in the EFL code base at [5]. An initialize the libraries:
overview of the files is given in the extra doc-
umentation mentioned above. Double-clicking UARTInterface_LibrarySetup();
‘ExperimentalUART.atsln’ opens the project in UARTInterface_SetBaudrate(0, 38400);
Atmel Studio: see the screenshot in Figure 3. BlockProtocol_LibrarySetup(UARTInterface_
On the right-hand side is a list of the files in Send, 0, UARTInterface_GetRingbuffer(0));
the project. The files Controller.h, Controller.c,
Board.h and Board.c form the hardware layer. Here the second line sets the baud rate of UART
interface block 0 (which is where we have our
RS-485 driver connected) to 38400 baud. The
Listing: third line needs a bit more explanation. We are
Basic structure of an EFL application. telling the BlockProtocol library that it should use
the function UARTInterface_Send when it wants
int main(void)
to send data from the board. The second param-
{
eter is the number of the UART interface block to
Controller_Init();
be used (even though on the experimental node
Board_Init();
there is only one UART interface).
Extension_Init();
The third parameter is a pointer to a ‘ring buffer’
ApplicationSetup(); (or ‘circular buffer’) to be used to store received
bytes. Here the required pointer is obtained as
while(1) the return value of the function UARTInterface_
{ GetRingbuffer(0), which is also implemented in
ApplicationLoop(); the UARTInterface library.
}
In the main application loop function, which is
};
called regularly when the application is running,
133
28 | June 2013 | www.elektor-magazine.com
Let’s try it
Having compiled the code and flashed it into the
board we can try it out using a terminal emula-
tor program such as HTerm [6]. First we have to
specify which COM port to use and the baud rate.
In the ‘input control’ window we have to set the
program up so that the <CR> character is sent
when the Enter key is pressed (see Figure 4). character sequences given above. Figure 3.
Code to control hardware
We will first use the library at its lowest level to Higher-level commands over a UART interface.
demonstrate direct access to to the output port Directly controlling port pins is not the main rai-
pins of the microcontroller. From the circuit dia- son d’être of the EFL. If we want to port our
gram of the experimental node [7] we can deter- device control program to use a different micro-
mine that the red LED on the board is connected controller board with different wiring, we would
to port pin PD4. Port D on the AVR microcontroller have to change the character sequences given
Figure 4.
corresponds to port index 3. And so we simply above inside the PC or Android software.
The HTerm terminal
type into the terminal emulator For this reason the mini-protocol also allows emulator lets you choose
hardware-independent control of digital inputs what is sent when the Enter
p 3 4 + <ENTER> and outputs, just like when writing code to run key is pressed.
134
www.elektor-magazine.com | June 2013 | 29
B 0 0 ? <CR>
on the microcontroller itself as described in the
previous issue.
With the string we can request the status of the test button (but-
ton 0 of button block 0). And with
L 0 1 + <CR>
R 0 0 + <CR>
we can switch on LED 1 in LED block 0 (which
on our board is the yellow LED). With we can turn on a relay.
Three-wire interface
The approach described above assumes that we
BlockProtocol have external access to a UART on the microcon-
troller, for example via RX and TX pins brought
Each command starts with a single character, possibly followed by one
out on the board. We also need a more-or-less
or two decimal values. These specify to the EFL the pin on which the
complete EFL microcontroller description file
command will act. A final character is used to specify any particular
which describes not only the I/O pins and ADC
action to be performed.
functions but also the UART functions that are
available.
x
Outputs the EFL map, block and board pin tables (see additional
When starting out with a new microcontroller (for
documentation at [1]).
example because you want to develop an EFL
microcontroller description file for other read-
p x y +, p x y –, p x y ?, p x y #, p x y *
ers to use) it is probably simplest to start with
For microcontroller pin y on port x: set high; set low; read state; read
the I/O functions. The first major hurdles are
ADC conversion result (if applicable); toggle under timer control.
to study the datasheet to determine how to go
about setting the level on the I/O pins and reading
b x +, b x –, b x ?, b x #, b x *
their status, and to find at least three general-
For board pin with index x in the board pin table: set high; set low; read
purpose I/O pins that are brought out to con-
state; read ADC; toggle.
venient points: fortunately most boards satisfy
this condition. It will then be possible to control
i x y +, i x y -, i x y ?, i x y #, i x y *
the board using the protocol described above,
For pin in position y within the block with index x in the block table: set
over a three-wire SPI interface implemented in
high; set low; read state; read ADC; toggle.
software. One of the wires is a clock signal, the
second carries bytes from the master (the PC)
C x y +, C x y –, C x y ?, C x y #, C x y *
to the slave (the board), and the third carries
For pin y of connector x: set high; set low; read state; read ADC;
bytes in the opposite direction.
toggle.
135
30 | June 2013 | www.elektor-magazine.com
ThreeWireInterface_LibrarySetup();
BlockProtocol_
LibrarySetup(ThreeWireInterface_Send, 0,
ThreeWireInterface_GetRingbuffer(0));
which checks whether the other device has taken inserted in the main application loop the processor
its data line high to indicate a request to send will regularly check whether a string, terminated
data. If so, the communication is set up. When with a <CR> character, has appeared in one of
using the UART for communication we do not the input ring buffers. If so, then its contents
need a listener function like this, as in that case are sent out on the other communication chan-
a microcontroller interrupt can be used to detect nel. Then the other input ring buffer is similarly
when characters arrive and automatically store checked, and so on.
them in the ring buffer.
It should almost go without saying that the gate-
Making contact with an Arduino way module itself is programmed in a manner
To demonstrate the EFL concept we selected an independent of the underlying communication
Arduino Uno board. The code for this board can channels and can therefore easily be used to per-
be found in the project ArduinoUnoEFL [4][5]. We form other similar gateway functions. The func-
decided to use general-purpose I/O pins PB0, PB1 tion OneToOneGateway_LibrarySetup takes two
and PB2 to access the board: these are brought triples of parameters to specify the two com-
out on the ‘digital’ I/O connector as Digital8 to munications channels being connected. In this
Digital10. A simple three-way cable (for exam- example the call appears as follows:
ple using Conrad order code 741221) provides
a practical solution (see Figure 5). OneToOneGateway_
Of course we also need a connection to the PC at LibrarySetup(UARTInterface_Send,
the other end, and that will also need to have a 0, UARTInterface_GetRingbuffer(0),
three-wire interface. We pressed an experimen- ThreeWireInterface_Send, 0,
tal node board into service as a gateway, con-
ThreeWireInterface_GetRingbuffer(0));
verting from three-wire format to RS-485 (and
thence to USB) and back again. The firmware We now just need to plug the cable into the
involved can also be downloaded at [4] or [5]: experimental node’s expansion connector (pins
the Atmel Studio project can be found in the PC0 to PC2, see Figure 6) and flash the appro-
directory ‘ExperimentalSPI’. priate hex file into its microcontroller.
The gateway function has its own library mod-
ule called OneToOneGatewayEFL. With a call to The pins used to form the three-wire interface
have to be specified separately for the two dif-
OneToOneGateway_Engine(); ferent boards. The three-wire interface on each
device is a separate peripheral block: recall that
136
www.elektor-magazine.com | June 2013 | 31
137
32 | June 2013 | www.elektor-magazine.com
Arduino
AC Grid Analyser
Frequency analysis
on a mini colour display
By Fidelis Theinert A fully fledged spectrum analyser on a basic Arduino board? In this article we’ll
(The Netherlands)
show you how this is possible when the software is tailored to the available pro-
cessing power. A standard Arduino can be turned into an AC line (‘mains’) sup-
ply spectrum analyser with the addition of a filter shield and a display shield. It
uses a Fast Fourier Transform to visualize the ‘pollution’ on the AC grid supply at
home or at the office.
138
28 | December 2012 | www.elektor.com/magazine
Electrical
Isolation Lowpass 10-bit ADC Window FFT
AC Line Attenuation
50 / 60 Hz Serial
Output
Spectrum
Counter
Arduino
Frequency Display Figure 1.
120546 - 11 Block diagram for the
spectrum analyser.
A simple Arduino board is much more powerful length of the array can usually be freely chosen.
and versatile than many readers realize. In this According to the Nyquist-Shannon sampling
article we’ll use a practical example to show you theorem the bandwidth of the signal may not
what the capabilities are of this small 8-bit micro- be greater than half the sampling frequency. In
controller, but also what its limitations are. We other words, the sampling frequency has to be at
will also explain how the various components of least twice the value of the highest frequency we
the microcontroller can be used efficiently and want to investigate. With a sampling frequency
which free development tools are required to of 800 Hz you can therefore analyse a signal with
set up projects such as these. The software and a bandwidth of 400 Hz, which makes it possible
hardware are of a modular construction, which to show the spectrum of the local AC grid supply
makes it possible to use the individual modules up to its sixth harmonic (60 Hz x 6 = 360 Hz),
in other projects as well. The application was or seventh (50 Hz / 350 Hz).
tested successfully on an Arduino Duemillenova,
UNO R1, R2 and R3. Hardware
The hardware consists of three boards: the
The circuit analyses the AC grid voltage using Arduino board [1], a filter shield developed by
a Fast Fourier Transform (FFT) and shows any the author and an LCD shield [2]. The two shields
harmonics present on the display. This can then can be mounted on the Arduino board (with the
be used to assess the quality of the supplied LCD shield on top, of course), which results in a
(energy) signal. These higher harmonics cause compact and sturdy module without any loose
losses in machines and motors that are converted wires. Figure 1 shows a block diagram of the
into heat, but which you still have to pay for. Fur- complete setup.
thermore, the efficiency of switch mode power
supplies in PCs and other devices is compromised When the Arduino has calculated the spectrum
by a supply signal that isn’t an ideal sinewave. it is also possible to send the result to a PC via
the serial port. The graph can then be plotted
Fourier analysis using a terminal program (Putty, for example).
The Fast Fourier Transform is a mathematical The transmission of the data requires a bit more
algorithm that converts a discrete signal from time since the whole graph is sent to the UART
the time domain into the frequency domain. The of the PC at a rate of only 57,600 baud. This
improvement in execution time of the FFT com- feature will be particularly useful to those of you
pared with the ‘Discrete Fourier Transform’ (DFT) who don’t have an LCD shield.
can be quite substantial for a large number of
samples (N). With an FFT the Fourier transform To make sure that the input signal doesn’t have
on a signal with N samples is split into two trans- any frequency components above 400 Hz, a fourth
forms with lengths of N/2. order Sallen-Key lowpass filter (or anti-aliasing
This results in a much reduced execution time filter) with a bandwidth of 350 Hz is put in front
since there are fewer calculations to be done, but of the input (Figure 2). The two Butterworth
it does mean that the number of samples (N) has sections are made around a TS912 dual opamp.
to be a power of two (64, 128, 256, ... , 4096 This opamp has a rail-to-rail output and a wide
...). In practice this is not a problem since the operating voltage range of 2.7 V to 16 V, which
139
www.elektor.com/magazine | December 2012 | 29
Power
RESET 1
3V3 2
5V 3
GND 4
R7 GND 5
47k VIN 6
R1 R8
C2 C5
330R
1k
JP1
100n 680n
C10 R5 R6 R3 R4
3 8 5 Analog
3k9 3k9 1k8 1k8
1 7 A5 1
2u2 IC1.A IC1.B
16V 2 6 C7 A4 2
X1 2
4 A3 3
100n A2 4
R10
10k R2 A1 5
C3 IC1 = TS912 C1 C8 C9 C4 LED1
A0 6
1k
1
100n 100n 22u 100u 100n
16V 16V JP3
Figure 2.
Anti-aliasing filter (350 Hz 120546 - 12
means it can be powered directly from the 5 V the microcontroller (an ATmega328P). The out-
supply voltage on the Arduino board. put of the opamp is also capacitively coupled to
connection A4 of the analog input connector of
The output signal from the filter goes directly the Arduino board, for measuring its frequency.
from pin 7 of IC1 to channel 5 of the ADC on In Figure 3 you can see the shield board designed
FFT tricks
The Cooley-Tukey ‘butterfly’ algorithm implemented here is a would be useful if we could store the samples as ‘single
so-called ‘in-place transformation’; this means that the result precision floating point’ variables instead of integers. (In an
of the transformation is stored in the same buffer where the FFT we don’t just have simple mathematical operations such
original samples of the input signal were stored. The input as addition and multiplication, but also sines, cosines, square
signal is therefore no longer available after the conversion. roots and exponentiation.) However, these require 4 bytes
The advantage of this is that the memory required for each for storage, making the buffer 2048 bytes long, which is
the buffer is halved , which is extremely useful when the as much as the available memory in the microcontroller, and
microcontroller used (ATmega328) only has 2048 bytes of therefore too big for the chosen platform. One way round this
memory available. dilemma would be to halve the number of samples for the
FFT to 128, but this will result in a lower spectral resolution
As was mentioned previously, the number of samples has and is therefore undesirable.
to be a power of 2. This condition is satisfied by the buffer
size of 256 (28) samples. The ADC has a resolution of 1024 A different solution is therefore required if we want to keep
levels, which means that two bytes are required to store working with 256 samples. The real and imaginary parts
each sample, or 512 bytes for all of the samples. During of the complex numbers are still stored as integer values
the calculations there is also a requirement for a buffer to (2 bytes/sample) and converted into a ‘floating point’
hold the imaginary part of the numbers, since the Fourier variable before each mathematical operation and converted
transform is a conversion of complex numbers. This buffer back into integer format afterwards. The extra overhead
obviously needs to be the same size as the buffer for the real for these two conversions obviously makes the system a bit
part and therefore requires 512 bytes as well. In total we slower, but it is acceptable. The rounding error introduced by
therefore need 1024 bytes to store the values for the FFT, the conversion (1/65536) is a factor of 64 smaller than the
which is half of the available memory. quantization error of the ADC (1/1024) and can therefore
be ignored (see the functions ‘FFT_Int2Float ()’ and ‘FFT_
In order to use the maths library of the GCC compiler it Float2Int ()’ in the module fft_lib.c).
140
30 | December 2012 | www.elektor.com/magazine
for this filter. Note that there are some extra com-
ponents on this board that aren’t used in this
application. The board layout is available from
the associated web page for this article [3].
The AC line voltage is electrically isolated using
a AC power adapter (an ‘old fashioned’ type with
a transformer). The output of the adapter is con-
nected to terminal block X1. Since the filter has a
gain of one, a signal with an amplitude of 2 Vrms
(5 Vpp) is sufficient for the dynamic range of the
ADC in the ATmega. The output signal from the
transformer is not distorted in practice because
the transformer is effectively unloaded, as can
be seen in Figure 5 (the supply for the analyser Figure 3.
comes from a separate adapter or via the USB The PCB layout for the filter
connection). shield.
In order to use the memory as efficiently as possible, the function can be applied to the real part of the samples (this
samples from the ADC are initially stored in the buffer for window function is necessary in order to satisfy the boundary
imaginary numbers. At that point in time this buffer isn’t conditions for the FFT).
used by the FFT routine.
An important aspect of the FFT implementation is the so-
called ‘bit reversal’ algorithm, which is used for addressing
the samples in the buffers. At first this may look like a simple
7 6 5 4 3 2 1 0 operation (the bits of the value of a register have to be put in
input reverse order, as shown in the figure above), but it isn’t very
easy to implement. Fast implementations use extra hardware
registers for this. Another alternative that is often used is a
table, but this requires a lot of memory and that is in limited
supply in a small microcontroller. The slowest method is to
shift all bits out of one register and shift them into another
output register in reverse order. This requires a loop with a counter
7 6 5 4 3 2 1 0 and a total of 16 shift operations. A combination of these
120546 - 17 methods is used here, which provides a good compromise
between memory usage and processing time. The byte is
Bit reversal algorithm split into two lots of 4 bits. Each of these 4-bit values is
reversed using a table and the results are then combined in
the right order into a byte again (see the function ‘fft_BitRev
The samples are then normalized and scaled, with the results ()’ in module fft_lib.c). The resulting time saved is 3 µs (2 µs
stored in the buffer for real numbers. instead of 5 µs) for each instance this function is called, but it
The imaginary buffer can then be cleared and the window does require an extra 75 bytes for the code.
141
www.elektor.com/magazine | December 2012 | 31
Figure 4.
The three boards next to
each other.
lcd_driver.c lcd_driver.h Low-level functions and definitions for driving the LCD
menu.c menu.h Functions for reading the switches on the LCD shield
142
32 | December 2012 | www.elektor.com/magazine
Figure 5.
The AC grid voltage at
the primary side of the
transformer (blue) and the
voltage at the secondary
(yellow).
Once the timer (timID) has indicated the start of quency plot with a linear scale along the vertical
the next cycle, the 256 samples for the FFT will axis. Another press of this button changes the window
function
be read in via the ADC with a sampling frequency display to the time domain (scope function) and
of 800 Hz. Next, the true effective value (TRMS) the first 96 scaled samples in the input buffer are
of the signal voltage is calculated. In the next shown (see Figures 7 and 8 for the different FFT
step the frequency of the signal at pin A4 on con- displays). Another press of the first button shows
nector ‘Analog In’ is measured using the built-in the complete contents of the input buffer after
display
comparator and Timer1 of the microcontroller. the window function has been applied. A fourth
The next steps (scaling the measured values and press switches back to the frequency domain
120546 - 14
applying the selected window function) prepare with a logarithmic scale, as it was when the cir-
the measured signal for the actual FFT calcula- cuit was first switched on. The bottom button is
tions. At the end the result of the calculations is used to select the type of FFT window function.
sent to the display of the LCD shield to show the When the analyser is switched on the default Figure 6.
spectrum of the signal. window type is Hanning. Pressing the bottom Flow chart of the program.
143
www.elektor.com/magazine | December 2012 | 33
a b
Figure 7.
Logarithmic (a) and linear
(b) plots of the frequency
domain shown on the
display.
button switches to a Blackman window and the 1372 bytes have been used of the available mem-
next press changes to a Hamming window. The ory (RAM 2048 bytes), of which 1024 bytes are
selected window function is always shown at the for the FFT buffers. This is about 2/3 of the avail-
bottom-right of the display: ‘N’ for Hanning, ‘B’ able memory. Only about 18 KB have been used
for Blackman and ‘M’ for Hamming. of the available program memory (ROM 32 KB).
Bearing in mind that the Arduino bootloader
The middle button turns the serial communica- needs about 2 KB, this leaves more than 10 KB
tion with a terminal on and off. When no data is of ROM for possible enhancements.
sent to the computer, the colour of the indica-
tor for the window function turns from yellow to Conclusions
green and back again after each spectrum has The system described here is a fully functional
been calculated. When the serial communication real-time FFT analyser for the AC grid supply,
is turned on the indicator turns from yellow to red. which uses a 256-point floating-point FFT to cal-
culate a new spectrum every 2.5 s and shows it
Performance graphically on a display. All this is implemented
Depending on the compiler settings, a calcula- on an Arduino platform with only one third of its
tion of the FFT can take up to 1 second in the program memory used.
most unfavourable condition without optimization
(-O0). The size of the compiled program is then This application shows how efficient and powerful
about 26 KB. With optimization turned on (-O2) an 8-bit microcontroller can be when it is used
the length of the executable shrinks to about to perform advanced mathematical calculations
18 KB and the calculation of the FFT takes only and display the results graphically, as well as
700 ms. The time taken to run through a complete including a user-interface and communications
cycle of the program (measuring the frequency, with a PC. But there are also limitations, such as
acquisition, calculating the TRMS, scaling, win- the available memory (which restricts the FFT to
dow function, calculating the FFT, scaling, plot- 256 points) and the time taken to perform the
ting on the display) takes about 2100 ms with calculations, although there is further room for
optimization (-O2). improvement.
A cycle time of 2500 ms was chosen so that the For the development of this project only open-
system has a predictable and constant response. source tools were used and the Arduino platform
This is obviously when the transmission of data to itself is completely open and freely available.
the serial port is turned off. A detailed description The software is modular and it is very easy to
of the ‘-O’ flags can be found in the documenta- use any of the modules in another project. The
tion for the GCC compiler. software has been published under the GPL V3
144
34 | December 2012 | www.elektor.com/magazine
a b
Figure 8.
Plots of the time domain
shown on the display (96
samples without a window
function (a) and 256
samples with a Hanning
window (b)).
license, which means you can use it in your own Internet Links
projects if you want to. The conditions of the [1] http://arduino.cc/en
GPL are published on the website for the ‘GNU
[2] www.sparkfun.com/products/9363
Operating System’ [6]. The full software package
and the board layout can be freely downloaded [3] www.elektor.com/120546
from the associated web page on the Elektor [4] http://sourceforge.net/projects/winavr
website [3]. [5] http://gcc.gnu.org/onlinedocs/gccint
[6] www.gnu.org/copyleft/gpl.html
Possible enhancements
Despite the extensive functionality that is already Also read: Grid Frequency Meter, Elektor January
implemented, it would be possible to add a cal- 2012.
culation of the THD (total harmonic distortion)
to the analyser.
Another enhancement would be a speech analyser
that calculates the spectrum of a speech signal
with the help of an FFT and shows the result on
the display. This does require that the bandwidth
of the lowpass filter is adapted. The ADC in the
ATmega328 can still operate at its full resolution
of 10 bits at a sample frequency of 15 kHz, which The author
is more than enough for a speech signal with a
After completing his studies in
bandwidth of about 5 kHz.
electric engineering at Berlin
Technical University, Fidelis Theinert
The speed of the system can be improved if the
worked for over 20 years at various
acquisition of new samples happens in the back-
companies in Germany and The
ground via interrupts whilst the information is
Netherlands as a hardware and
being sent to the display. This results in a 0.3 s
software engineer and has also
(256 samples/800 Hz sample frequency) improve-
taught at colleges. He currently
ment. It is also possible to drive the display more
teaches electric engineering at
quickly using the SPI interface of the ATmega328.
Haagse Hogeschool, Delft, The
Another enhancement is to have an elaborate
Netherlands. His expertise is in the
program on the PC to display the graphs, but
field of embedded systems and in
then we would no longer have a self-contained
particular in signal processing of
FFT analyser on an Arduino board.
video as well as audio signals.
(120546)
145
www.elektor.com/magazine | December 2012 | 35
Design considerations
Some of the configuration commands, like
setting the baud rate of its serial port, are
more or less one-time only, and the mod-
ule remembers these settings. Since the
AT commands are human-readable it may
therefore be practical for the initial setup,
but also for experimenting, to be able to Figure 2. Fully built-up Wi-Fi shield on top of Platino,
communicate with the module using a PC also equipped with a 20x4 alphanumerical LCD.
AD POWER
A5 A4 A3 A2 A1 A0 VIN GND 5V 3V3 RST IOREF
IC1 +3V3
K2 K1
MCP1825S-3302E/AB
+5V +3V3
6 5 4 3 2 1 8 7 6 5 4 3 2 1
2
JP5 *1 +3V3
R7 R11 R9 +5V
S1
1k
1k
1k
C1 C2 C3 C4
+3V3
* D2 PVCC
2 17
VCC
RXD R8 R10 5 37
PIO0 RF_IO
6 3
PIO1 MOD2 AIO0
1k
1k
7 4
PIO2 AIO1
8 20
9 32 33 34 PIO3 USB_DP
VBAT VIN_3V3 VIN_1V8 VDDIO 9 21
PIO4 USB_DN
+3V3 RX 11 31
EXT ANT PIO5 SPI_MOSI
12 32
MOD1 PIO6 SPI_CSB
2 47 ASSOCIATED 13 33
JTAG_TCK GPIO28 PIO7 SPI_CLK
R12 R13 3 45 14 34
JTAG_TDO GPIO30 PIO8 SPI_MISO
4 44 15 22
47k
47k
1k
12 29
ADC1 SSPI_CS JP1
13 28 JP3
ADC2 SSPI_CLK
14 27 +5V
GND
VCC
DTR
TXO
CTS
RXI
MSPI_MISO/GPIO6 SSPI_MISO T2 T1 1 2 3
15 26 R3
MSPI_MOSI/GPIO7 I2C_DATA/GPIO8 TX_MCU
16 24 1k
MSPI_CLKI/GPIO5 GPIO20/CLK_22MHZ 6 5 4 3 2 1
+5V
17
VOUT_1V8 GPIO19/CLK_44MHZ
23 2x VCIO CBUS4
19 22 BC547B +3V3
MSPI_CS0/GPIO4 PWM0/GPIO10 +3V3 CBUS3
20 21 +3V3
MSPI_CS1/GPIO13 I2C_CLK/GPIO9 CBUS2
TX_FTDI
GND GND GND GND TXD MOD3 CBUS1
R4 R5 RX_FTDI
1 18 31 48 RXD CBUS0
BOB-FT232R
1k
1k
RX_MCU 1
K5 *2 CTS RESET
RI DCD
T4 T3 3 4
R6 RTS DSR
3 2 1 5 6
1k DTR
GND
2x ICSP
TX_MCU
RX_MCU
BC547B JP2
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 10
K3 K4
0 1 2 #3 4 #5 #6 7 8 #9 #10 #11 12 13
GND
AREF
SDA
SCL
120306 - 11
IOL IOH
host system or even disconnect it. Flexibil- rail, like Arduino, I thought it wise to add can be controlled with AT commands over
ity started creeping into the design. a voltage regulator to the system because a serial link; they run from 3.3 V; they have
The Wi-Fi module has to be powered from a the WizFi220 can consume up to 250 mA in similar dimensions and similar require-
3.3 V rail and it is not 5 V tolerant. The main active mode. As an example, the LP2985-33 ments. It is therefore not very difficult to
objective is to connect the Wi-Fi module’s voltage regulator on an Arduino Uno board add Bluetooth capabilities to the system,
serial port to a microcontroller. Such a sys- is specified for 150 mA, which is clearly not all you have to do is wire a usable Bluetooth
tem may run from 3.3 V, but if it is Arduino enough. Platino does not have a 3.3 V rail module in parallel with the Wi-Fi module
or Platino, then the microcontroller runs so this shield could provide it too. A jumper and that is what I did. I chose the Rayson
from 5 V. Level converters on the serial port should allow connection of the 3.3 V as pro- BTM220 module because I am at ease with
would therefore be necessary. The FTDI duced by the shield to the host system. it [4] and it is very cheap. We have used it
serial USB cable comes in two flavours, 5 V In this section you can replace all instances several times in Elektor too. [5][6]
and 3.3 V, and with level converters both of “Wi-Fi” by “Bluetooth” without having to
types would be usable. The BOB was not an change anything else and still have a coher- This completes the reasoning that deter-
issue because it can handle both levels. ent text. Indeed, many Bluetooth modules mined my design. Let’s now have a closer
Although the host system may have a 3.3 V are very similar to the WizFi module: they look at the circuit diagram (Figure 3). If you
COMPONENT LIST
K2 = 6-pin stackable header, 0.1” pitch,
Resistors vertical
R1–R11 = 1kΩ 5% 0.25W K5 = 6-pin (2x3) stackable header, 0.1”
R12,R13 = 47kΩ 5% 0.25W pitch, vertical
S1 = pushbutton, SPNO, through-hole,
Capacitors 6x6mm
C2, C3 = 100nF 50V, 5mm pitch Mod3 = BOB-FT232R USB-to-Serial
C5 = 220nF 50V, 5mm pitch Bridge, Elektor Shop # 110553-91, or
C1, C4 = 10µF 35V, 2.5mm pitch USB-to-Serial cable, Elektor Shop #
080213-71 (5 V) or # 080213-71 (3.3 V)
Semiconductors Mod2 = Rayson BTM22x Bluetooth
D1,D2,D3 = LED, green, 3mm diam. module
T1–T4 = BC547C, TO-92 case Mod1 = WIZnet WizFi2x0 Wi-Fi module
IC1 = MCP1825S-3302E/AB, 3.3 V volt- PCB # 120306-1 [1]
age regulator, TO-220 case
Miscellaneous
JP1 = 6-pin pinheader, 0.1” pitch,
JP2 = 4-pin pinheader, 0.1” pitch, straight
straight
K1,K3, K4 = 8-pin stackable header, 0.1” pitch,
JP3,JP4 = 3-pin pinheader, 0.1” pitch, straight,
vertical
w. 2 jumpers
have grasped my prose up to here, then the tors). For the Wi-Fi module the position of you have to pull it High. Finally, a low level
schematic will have only a few surprises. the LEDs is drawn correctly and you should on pin 7 (Alarm1) will wake up the module
not mount R8 and R10 (although nothing from deep sleep mode.
Putting it all together breaks or blows if you do), only R7 and R9 The level converters (R1-3, T1-2 and R4-6,
On the left side of figure 3 we have the Wi-Fi are needed now. T3-4) are straightforward and have been
module; on the right side we see the Blue- used before in Elektor. Transistors T1 and T4
tooth module. As you can see they share The WizFi module has some pins with spe- do the level conversion, but invert the sig-
the serial RX and TX lines, meaning that cial functions that you may want to use. nal at the same time. T2 and T3 correct this.
you should not mount both. It is either Since I didn’t use them I wired them to a The resistor values are not critical. I chose to
Wi-Fi or Bluetooth, never both. The mod- separate connector JP4. To enable you all use as many 1 kΩ resistors as possible.
ules also share two LEDs (D1 and D2); D3 to stack many shields on top of each other The RX and TX lines connect to JP1 and JP2.
is only used by the Wi-Fi module. A surprise I decided not to wire these signals to the These 3-pin jumpers may be a bit difficult
here may be R8 and R10. Maybe I could have Arduino extension connectors K1 to K4. If to understand at first sight, but when you
drawn a better schematic, but I didn’t, so you need them you can wire them your- follow the lines you should be able to fig-
I have to explain this in writing. It is actu- self. Pin 25 (GPIO 21) allows you to restore ure them out. With a jumper on JP1’s pins
ally very simple. The Wi-Fi module sinks the the module’s factory defaults. Pulsing it 1 and 2 the USB serial converter TX pin
current for the LEDs whereas the Bluetooth Low twice will restore the module to Lim- is connected to the Wi-Fi (or Bluetooth)
sources the current. As a result the LEDs ited Access Point mode, three pulses will module’s RX input. A jumper on JP2’s pins
have to be mounted the other way around restore it to Ad-hoc mode. Pin 46 (GPIO 29) 2 and 3 connects the USB serial converter
as drawn if you use the Bluetooth module. provides a hardware way of switching RX pin to the Wi-Fi (or Bluetooth) module’s
In that case you should mount R8 and R10 between Command (High) and Data TX output. This is the Configure-Module-
and not mount R7 and R9 (mounting R11 mode (Low). Pin 37 (GPIO 27) is needed With-PC mode. In these positions the USB
and D3 is useless unless you connect the when you want to upgrade the firmware serial converter cannot talk to the host MCU
LED to a free I/O pin on one of the connec- of the module (probably never). To do so system and it may be better to disconnect
Listing 1.
Example of setting up the WizFi2x0 module as a serial server using AT commands. The commands are in boldface, the module’s responses
are in italic. Comments appear in brackets (). See the WizFi2x0 user manual for more commands.
AT (wake up)
[OK]
AT+NSTAT=? (what is your status?)
MAC=00:08:dc:18:97:76
WSTATE=NOT CONNECTED MODE=NONE
BSSID=00:00:00:00:00:00 SSID=”” CHANNEL=NONE SECURITY=NONE RSSI=0
IP addr=0.0.0.0 SubNet=0.0.0.0 Gateway=0.0.0.0 DNS1=0.0.0.0 DNS2=0.0.0.0
RxCount=0 TxCount=0
[OK]
AT+WPAPSK=germaine,”Philippe Noirette” (set SSID & pass phrase)
Computing PSK from SSID and PassPhrase...
[OK]
AT+NDHCP=1 (request IP number from DHCP server)
[OK]
AT+WAUTO=0,germaine (automatically connect to germaine)
[OK]
AT+NAUTO=1,1,,8011 (setup for auto connect: server, TCP, port 8011)
[OK]
ATA (start auto connect)
IP SubNet Gateway
192.168.2.7: 255.255.255.0: 192.168.2.1
[OK]
the board from the host system. The board because of the shield mounted on top of connectors for K1 to K4 you should mount
will be powered from the USB port. it. This depends on the Arduino board and them on the solder side if you want to stick
If you put a jumper on JP1’s pins 2 and 3 its revision. the board on an Arduino or Platino.
and on JP2’s pins 1 and 2 the USB serial con- K5 is not connected. I only put it on the JP1 and JP2 are positioned in such a way that
verter can be used to talk to the host sys- board to provide access to its signals if the the jumpers should always be in the same
tem. In this case you should not mount the host is Arduino. position, i.e. both to the left, both to the
wireless module (or the level converters) as right or both absent. Actually, you could
this kind of communication may disturb it. Turning practical leave a jumper on JP2 in case you wanted to
This is the USB-Shield mode. To turn a circuit diagram into a real shield listen in on the MCU wireless module com-
Not installing any jumpers will put the or extension board a printed circuit board munications. This can be useful for debug-
board in Wireless mode and the USB serial (PCB) is needed. I designed one and you can ging purposes.
converter has no use. However, you could download the Eagle CAD files from the web JP3 has a few contacts in common with the
leave it on and wire it to other pins on the page that accompanies this article [7]. The BOB serial-to-USB bridge, the idea being
extension connectors. This is for instance PCB is the size of an Arduino Uno except that that you use either a BOB or an FTDI cable,
useful in an Arduino-with-software-UART it is a rectangle without the funny-shaped not both.
configuration or in a host system with more short side of an Arduino Uno. All the compo- For full 3.3 V systems the level converters
than one UART. nents are through-hole types; only the Wi-Fi and voltage regulator can be left off. In this
and Bluetooth modules have surface mount case you have to place two bypasses (wire
Finally some remarks on details. The Reset footprints. JP5 and the Bluetooth module are bridges) from R3-JP1 to R2-T2 and from
button is mainly intended for Platino which located on the solder side of the board; the R6-MOD1 to R5-T3. Resistors R3 & R2 and
doesn’t have one. It is also useful when the other components should go on the compo- R5 & R6 are positioned in such a way that
Arduino reset button becomes inaccessible nent side. Note that if you use non-stackable the wire bridges are very easy to install and
Figure 4. The three main configurations next to each other. The Bluetooth module is not visible (middle board) because it is mounted on
the other side of the PCB. Note that this is an early revision of the PCB that has the same shape as an Arduino Uno board. The final PCB is
rectangular.
span only minimum distance. You will also forget to put a drop of solder on JP1 of host system responds to these. It is up to
have to short JP5 on the solder side of the the BOB to configure it for 5 V or 3.3 V you to provide a host that can communicate
board. operation. Add 3.3 V voltage regula- over a serial link.
As mentioned before, the orientation of the tor and reset push button to taste. The
LEDs depends on the wireless module. The LEDs can be useful too, but you will have Thanks go out to Joachim Wulbeck of WIZnet
component print on the PCB corresponds to wire them to a connector pin. You Europe GmbH (www.wiznet.eu) for providing
to the Wi-Fi module. If you mount a Blue- should short pins 2 and 3 of JP1 and pins the WizFi220 modules and the Wi-Fi antennas.
tooth module you should mount the LEDs 1 and 2 of JP2 with a jumper or a wire (120306-I)
D1 and D2 ‘the wrong way around’. Also, bridge.
only mount R7 or R8 and R9 or R10.
The 3.3 V voltage regulator can be installed Testing
lying on his back or standing up. In case of the Wi-Fi or Bluetooth configura-
tions connect the board to a USB port on the Internet Links and References
Three main configurations are possible (Fig- PC using an FTDI serial converter cable or the
[1] Platino: www.elektor.com/100892
ure 4), but you may have a need for other BOB serial-to-USB bridge. Figure out which
variants: COM port was created by the operating sys- [2] USB-to-TTL serial cable:
tem (OS) of your PC (make sure you have the www.elektor.com/080213
• Wi-Fi shield — no need for the BOB if appropriate drivers for your OS installed. If [3] BOB serial-to-USB bridge:
you own an FTDI cable, although you you haven’t you can get them, including the www.elektor.com/110553
can always install one. JP3 is available necessary documentation, at www.ftdi-
for connecting the FTDI cable. Mount all chip.com) and start a serial terminal pro- [4] Experiments with Rayson Bluetooth
LEDs as indicated on the PCB and do not gram. The WizFi220 module has a default modules:
mount R8 nor R10. Level converters will baud rate of 115 200 bits/s, for the BTM220 http://elektorembedded.blogspot.
be needed and the 3.3 V voltage regu- module this value is 19 200. Both modules com/2010/08/rayson-btm222-btm112-
lator probably too, depending on your use 8 bits, no parity and no hardware flow bluetooth-modules.html
host system. control. In the terminal type ‘AT’ followed [5] Bluetooth with the ATM18:
by the Enter key. If you set up everything the
www.elektor.com/080948
• Bluetooth shield — similar as the Wi-Fi right way the module should respond with
shield except for the LEDs. D3 has no ‘[OK]’ or ‘OK’. If this test succeeds you’re [6] Bluetooth for OBD-2:
function and you should not mount R7 in business. Refer to Listing 1 for a working www.elektor.com/090918
and R9 but mount R8 and R10 instead. Wi-Fi example (don’t forget to adapt the [7] Wi-Fi shield: www.elektor.com/120306
As an antenna you can use a piece of SSID and pass phrase to your network).
[8] Wi-Fi shield on Elektor Projects:
(insulated) wire of 31 mm long. For a BOB serial-to-USB bridge configura-
http://www.elektor-projects.com/pro-
tion the steps are more or less the same
• Serial-to-USB bridge shield — just the except that there is no point in punching in ject/wi-fi-bluetooth-usb-shield-for-ardui-
BOB without level converters. Do not AT commands in the terminal unless your no-platino.12252.html
www app
Laser Projection with Arduino
1 CD+ 18
I1 O1
2 17
I2 O2
3 16
DC IN USB I3 O3
4 15
I4 O4
1 5 14 +5V
AREF I5 O5
MOD1 2 6 ULN2803 13
GND I6 O6
3 7 12
ARDUINO D13 I7 O7
28 4 8 11
RESET D12 I8 O8
27 5 GND 140mA
LASER
3.3V PWM/D11
26 6 9
5V PWM/D10
25 7
15R
GND PWM/D9
DIGITAL INPUT/OUTPUT
24 8
GND D8 25R
1 TIL119
23
VIN 5
9
D7
10
PWM/D6
22 11
AN0 PWM/D5
21 12
AN1 D4 2 4
ANALOG INPUT
20 13
AN2 PWM/D3
19 14 +5V
AN3 D2
18 15
AN4 TX > D1
17 16 10k
AN5 RX < D0
perfectly perpendicular to the axis of
10k
the shaft, so the originally linear beam
10k
path is converted into a cone. This cone
10k
strikes a second mirror on end of the +5V
10k
shaft of a second motor. The beam P1 P2
from the second mirror goes to the 10k 10k
10k
golb
pohs
swen
ppa www
You’ve just got hold of the latest whizzy IC and can hardly
wait to start experimenting with its functions. It is highly
likely that, like many modern devices, it will be controlled
over an I2C or SPI interface.
What would be useful at this point would be some
kind of versatile hardware tool based on a mi-
crocontroller that could talk to these in-
terfaces, as well as providing a few
digital I/Os and perhaps also a
PWM output. Ideally the tool
should be controlled using
a simple protocol over
a serial port, which
would allow a PC
to be used to is-
sue commands to it for maximum flexibility.
Purists can hack away using a terminal pro-
gram; those more comfortable with a graph- for which function.
ical user interface can easily rustle up something suitable.
In principle we might want to execute a set of commands Using a ready-made module means
in sequence, and this can be achieved using macros to au- that we are free to concentrate more on
tomate things. the software aspects of the project. Quite a lot needs
Most modern microprocessors, such as AVR microcontrollers, to be done in this direction: the firmware running on the Ar-
already include the peripheral interfaces needed to carry out duino needs to configure the Timer/Counter/PWM, I2C, SPI
such experiments. A ready-made processor board with all and UART modules, as well as set up the four analogue chan-
the required pins brought out on header strips would make nels and eight digital ports.
a good basis for all-singing-all-dancing hardware for meas- There are two ways in which the device can be controlled
urement, control and experimenting. For the ‘AVIOM’ (AVR from a PC.
Versatile I/O Module) the author chose an Arduino Nano
board (see photograph). This board basically consists of an - Use a terminal. A simple terminal emulator program suf-
ATmega328P and a USB interface for talking to a PC. In the fices, and the short commands make interactive control of all
lab we have also successfully tested the design using an Ar- functions easy. Sequences of commands can be defined as
duino Uno board. Alternatively, it is easy enough to construct macros, stored and then subsequently played back.
a compatible module with an ATmega328P and a few support
components. If a ready-made USB-to-serial TTL-level cable - Write a program. The hardware can be controlled using a
is used, the corresponding converter chip on the board can program written in Visual C# 2010 with a graphical user in-
be dispensed with. The do-it-yourself approach is made pos- terface, written specifically for this project by the author. The
sible by the fact that Arduino hardware and software is all user interface even allows Python scripts to be created and
open source: the hardware reference design, the program- executed to automate the operation of the hardware.
ming environment and a wealth of software examples are
available for free download from the Arduino homepage [1]. We will now look at the various parts of the software one by
The large figure shows which pin on the board is responsible one. For simplicity the AVIOM operates in one of a range of
Reset button
RX + TX LEDs ICSP Header
Pin 13 (L) LED
different modes, such as ‘analogue’, ‘digital’ and ‘wait’. The lower-case letters. Each command consists of letters, nu-
operating mode is selected by sending a full stop character ‘.’ meric values, or a combination of the two. A complete list of
followed by a single letter abbreviation as follows. commands would occupy more space than we have available
here, but is available for download from the Elektor web-
.a Analogue site [2]. As a taster will we show here some of the commands
.d Digital available in analogue, system LED and wait modes.
.c Configuration
.e EEPROM access Analogue mode (.a):
.i I²C ‘1’ Display the value read from ADC channel 1
.l System LED ‘s’ Read all four of the ADC channels and display their
.m Macros values
.r SRAM access
.t Timer/Counter System LED mode (.l):
.u UART ‘h’ Switch system LED on
.w Wait ‘l’ Switch system LED off
‘s’ Read system LED status
In each mode there are three basic commands available:
help (‘?’), status (‘#’) and command line (‘%’). The help Wait mode (.w):
command ‘?’ causes the built-in help system to list the avail- hhh Wait for hhh (hex) milliseconds (hhh = 000 to FFF)
able commands. The ‘#’ command displays status informa-
tion. The percent character ‘%’ introduces a command line: Commands (and this goes also for mode-changing com-
this means that subsequent characters are collected until the mands) can be strung together into sequences, as hinted
end of the line (indicated by a CRLF) and then executed. at above. For example, the following sequence switches on
Within the commands themselves capitals are distinct from the system LED and then reads analogue channel 3: ‘.lh.
ues: below to the left the cycle timing can be set in millisec- thon (‘Python for .NET’) implementation [3].
onds. The scripts are stored as text files in a workspace directory. A
The area marked ‘Digital’ shows the current state of the complete list of the script files found there is shown in the list
digital pins. Clicking on the relevant button switches a pin box in the middle of the screen. The working directory can be
between output, high-impedance-input, and input-with-pull- changed by clicking on the ‘C’ button.
up-resistor modes. Clicking on the display (when in output If a script is selected in the list box, the name of the file will
mode) changes the state of the output. appear in the text box above it and can be edited there if
Radio buttons are used to change settings in the area marked necessary. A copy of an existing script with a new name can
‘Timer Counter PWM’. In ‘Count_R’ mode the counter counts be created by editing the filename suitably and clicking on
rising edges, while in ‘Count_F’ mode it counts falling edg- ‘SaveAs’.
es. Clicking ‘TCN’ reads the current value in the counter and
clicking ‘Z’ resets it. If you have entered or edited a script in the large window at
In PWM or frequency generator mode (‘Fgen’) the ‘Frequen- the bottom right, you can try it out using ‘Exe’ or by double-
cy’ and ‘Duty Cycle’ fields are enabled. Since only integer clicking the script name in the list box. The file will auto-
values are allowed in the AVR’s ICR and OCR registers, the matically be saved first. Text output from the script (from
actual frequency and duty-cycle values are displayed, which ‘print’ commands) and error messages appear in the window
may differ somewhat from the values the user has entered. above. Make sure that ‘scan’ mode is not active when execut-
objects: ‘s’ for system functions and ‘a’ for AVIOM functions. $0
The AVIOM object ‘a’ is an essential part of any script file. All
AVIOM commands that can be executed by a Python script SEN1060 GPS Module With Antenna
are implemented as methods of this object. (A click on the Small,Low Cost,Easy To Develope
‘Help’ button lists the built-in functions.) In many cases the
$50
methods return ‘true’ or ‘false’ to indicate whether the com-
mand was successful or not. A complete list of the AVIOM
script commands can be found in the document available (HMC5883L) 3 Axis Compass Module
Integrate Compass Easily
from the Elektor website.
The following example shows how commands can be exe- $12.5
cuted.
6P3P 7.5Wx2 Tube Amp Kit
#
CNC enclosure, hifi sound quality, easy to build
# Demo: TIMER COUNTER
# set pwm $136
#
f = 1.0
dc = 7.0
res = a.TCPpwm(f, dc)
print “PWM f = “, res, “ DutyCycle = “,dc,”%”
You will doubtless now want to try the system out for your-
self. The first step is to install version 1.0 of the Arduino
development environment [1].
The download from the Elektor website includes a directory
‘AVIOM_1_0’ which should be copied to any convenient place
on the computer. It is now a good idea to create a shortcut to
the file ‘..\AVIOM_1_0\AVIOM\AVIOM\bin\Release\AVIOM. FRONT PANELS & HOUSINGS
exe“. Cost-effective single units and small production runs
The next step is to launch the Arduino IDE and set the ‘sketch-
book’ location to point to the directory ‘..\AVIOM_1_0\AR-
DUINO’. Connect the Arduino board and select the ‘sketch’ Customized front panels can be
‘AVIOM_Arduino_1_0’, compile the program and load the hex designed effortlessly with the Front
Panel Designer.
file into the microcontroller.
The Front Panel Designer is available
A first test can be carried out using a terminal emulator free on the Internet or on CD.
program (with settings 115200 baud, 8N1) or using the au-
thor’s dedicated program, which can be run by clicking on · automatic price calculation
the shortcut. The demonstration scripts should appear in the · delivery in 5 – 8 days
script list. And away you go! · 24 - Hour-Service if r equired
(100576)
Sample price: 34,93 €
[1] www.arduino.cc plus VAT/shipping
[2] www.elektor.com/100576
[3] http://ironpython.net
Schaeffer AG · Nahmitzer Damm 32 · D –12277 Berlin · Tel + 49 (0)30 8 05 86 95 - 0
Fax + 49 (0)30 8 05 86 95 - 33 · Web [email protected] · www.schaeffer-ag.de
Sound machines
By David Cuartielles (Spain) When talking about interactive
music instruments, I like to refer to them as
Practice makes perfect and in this the first of a series of arti- Sound Machines. They consist of three blocks: the data used
cles I will focus in presenting all the bits and pieces you need to generate the sound, the user interface, and the actual
to understand in order to play 1-bit sound from a digital pin sound engine.
on your Arduino board. We will start by looking at the easiest The data refers to the way the sound is used. It could be
way to create sound by means of a simple piezo buzzer or a generated, we could play back sounds from a sample collec-
speaker. I will then introduce the Arduino Tone Library, as a
simplified way to achieve the same functionality. And I will
close by introducing an advanced technique that allows play-
ing short sounds stored in the form of .wav files.
When it comes to the theory, you will be introduced to a
technique known as 1-bit Delta Sigma Digital to Analog con-
version, but don’t be scared by the name, the methods and
technologies are presented along with examples you can
easily reproduce with a minimal set of parts.
This article is going to lay the foundations for creating Interactive Sound Machines using
a standard Arduino Uno board. However, all the knowledge described here as well as
the code can be easily ported to Arduino Mega, Arduino Mini or any other member of
the Arduino 8-bit family. Jump the Arduino bandwagon, this stuff comes straight from a
creator of the platform!
tion, or just perform real-time sound alteration. Data is the we will move into using a proper loudspeaker to increase the
actual sound, the tones in a piano, or the bytes stored inside quality of the sound output.
the channels in a sampler.
The user interface defines the way the sound structures will The piezoelectric buzzer
be manipulated, or the mechanisms to modify the sound The contact microphone, also known as piezo buzzer is an
characteristics. The easiest example is the volume control electronic component made of a combination of two discs
usually implemented as potentiometer or rotary encoder. of different materials. One of them is metallic and the other
The UI is what the user manipulating the instrument has his/ one is usually ceramic, having the piezoelectric properties.
her hands (or mind) on to alter the sound engine’s behaviour. When applying a voltage to the component, the materials will
Finally, the engine defines the way data is displayed. It takes repel each other, producing an audible click. Making the volt-
the data and renders it as sound according to the control par- age difference zero will cause the materials to return to their
ameters introduced by the user through the UI. In our case, original position, again producing a click sound.
the engine is the Digital to Analog converter we are going to By applying a voltage of a sufficiently high frequency, the
implement via software. click produced by the piezoelectric will modulate into an au-
dible tone.
From blink to bee You can test this by connecting a piezoelectric buzzer to Ar-
The “Hello World” example to start using Arduino is making duino by wring its positive pin to, say, Arduino’s pin 8 and the
the LED on pin 13 blink. The code looks like this: negative pin to Arduino ground (GND), see Figure 1. Take
the previous code and modify the line that determines the
/* Blink pin the LED is connected to:
Get the LED on pin 13 to go on/off
http://arduino.cc
*/ int ledPin = 8; // define the pin for
// your Speaker or Piezoelectric
int ledPin = 13; // define the LEDs pin
When running this program you will hear the piezoelectric
void setup()
{ clicking once per second, which is the times when the voltage
pinMode( ledPin, OUTPUT ); changes at its pins. You can modify the code to make the delay
// configure the pin as output between clicks. The smaller the delay, the more the clicks will
}
merge into a modulated tone. Try to change both lines in your
void loop() program affecting the time between clicks to be:
{
digitalWrite( ledPin, HIGH ); delay( 1 );
// turn the pin on
delay( 1000 ); // wait 1sec
digitalWrite( ledPin, LOW ); Now you will be hearing a sound with a frequency of 500 Hz.
// turn the pin off You will also notice the sound to be louder due to a property
delay( 1000 ); // wait 1sec of the piezoelectric components. They resonate in the kilo-
}
hertz range, at least the ones we find commercially available,
as they are designed to be buzzers or contact microphones.
The example is self explanatory. With little changes we can If you want to experiment further in sound production with
use the same example to start playing sound out of our Ar- this technique, I would encourage you to not use the de-
duino. We will be using a piezoelectric buzzer and later on lay() function from Arduino. The reason for this is that you
will need a better time-resolution to produce a richer selec- In other words, period is the inverse of frequency and vice-
tion of tones. The function delayMicroseconds() is much versa. In this way, if we want to know the delay needed to
more suitable to sound production, as it is three orders of play say, the A4 tone using Arduino, we need to do a bit of
magnitude more accurate. maths again like:
Adding all these changes to the original Blink program, will 1
give us the Bee program: p= = 0.002272 s = 2.272 ms = 2272 µs
440
/* Bee If we want the program “Bee” to play A4, we need to modify
Make a piezoelectric oscillate
on pin 8 it so that the total time in both calls to delayMicrosec-
http://arduino.cc onds() adds up to 2272 microseconds.
*/
digitalWrite( piezoPin, HIGH );
int piezoPin = 8; // define where the delayMicroseconds( 1136 );
// piezoelectric is connected digitalWrite( piezoPin, LOW );
delayMicroseconds( 1136 );
void setup()
{
pinMode( piezoPin, OUTPUT);
} The different tones within a scale can be mapped to time-
delays so that they can be played using Arduino. Table 1
void loop() shows a full octave.
{
digitalWrite( piezoPin, HIGH );
delayMicroseconds( 1000 ); Table 1. Available musical notes / tone frequencies
digitalWrite( piezoPin, LOW ); Tone Frequency [Hz] Period [µs] Delay [µs]
delayMicroseconds( 1000 );
} C4 261.63 3822 1911
D4 293.66 3405 1703
E4 329.63 3024 1517
Playing tones
According to Fourier’s Law every sound is the result of adding F4 349.23 2863 1432
a series of sinusoidal sound sources having different phases G4 392.00 2551 1276
and amplitudes. We could say that sine and cosine waves are A4 440.00 2272 1136
the fundamental components of sound. B4 493.88 2025 1012
C5 523.25 1911 956
Unfortunately microcontrollers, and the one on the Arduino
Uno is not an exception, cannot imitate the sinusoidal shape
perfectly. We can anyway produce square waves by repeat- Arduino’s tone library
edly switching a pin HIGH and LOW. The tones produced in There is more to sound playing than just the tone. Musical
this way have the same frequency, but are not clean as they scores are expressed in notes, which are the tones played
add components to the pure sinewave. In musical terms, the for just a certain amount of time. In order to simplify the
instrument making square waves has a different tonal char- possibility of playing basic melodies using Arduino, we added
acteristic than the one making sinusoidal ones, although the a library to the system that handles all the math explained
tones proper are the same. so far in this article, as well as note durations. Now that you
The only issue we find here is that sound is expressed in understand how to play a tone at a relatively low level, I think
frequency (hertz or Hz) while microcontrollers work with it is convenient to introduce this abstraction to make easier
time. The total amount of time a HIGH-LOW oscillation lasts for you to create programs playing sound.
is what we call the period (seconds). There is a relationship
between period p and frequency f as expressed in the follow- This library is called tone and brings in a function allowing
ing formula: playing a tone. In the background this is done by controlling
1 1 some of the internal timers in the processor. One timer takes
f = → p= care of the tone itself, while the other monitors its duration.
p f There are two functions to play sound, called the same, but
will be. The following code assigns durations to the different // to calculate the note duration,
notes considering that the basic note lasts 0.5 seconds. // take one second divided by the
The function above could also be written in a way that would // note type. e.g. quarter note =
// 1000 / 4, eighth note = 1000/8,
make use of the more common variable of the beats per minute // etc.
(bpm). In that way we could make the instrument play at dif- int noteDuration = 1000 /
ferent speeds depending on the desired tempo for the melody. noteDurations[thisNote];
tone( 8, melody[thisNote],
noteDuration );
/* we define durations as numbers
/ between 1 and 7:
// to distinguish the notes, set a
[...]
// minimum time between them.
*/
// the note’s duration + 30% seems to
void playNote( int speaker, int theTone,
// work well:
int duration, int bpm )
int pauseBetweenNotes =
{
noteDuration * 1.30;
// menmotecnic: 120 bpm - - >
delay( pauseBetweenNotes );
// 1000 milliseconds for half note
// source http://bradthemad.org/guitar/
// stop the tone of playing:
// tempo_calculator.php
noTone( 8 );
long time = ( 1000 * bpm / 120 ) /
}
pow( 2, duration );
#define NOTE_B0 31
#define NOTE_C1 33 Now try including a potentiometer or any other analog sen-
#define NOTE_CS1 35 sor to modify the BPM rate and in that way play the melody
[...]
#define NOTE_G4 392 at different speeds.
#define NOTE_GS4 415 Our next instalment begins with playing 1-bit notes and
#define NOTE_A4 440 culminates in a method to make Arduino play a sound with
#define NOTE_AS4 466 barely more than a single line of code.
[...]
#define NOTE_CS8 4435 The files for this month’s instalment may be found at: www.
#define NOTE_D8 4699 elektor.com/120366
#define NOTE_DS8 4978 (120366)
A sound format that can be of great use is the 8-bit PCM WAVE (also
called Microsoft unsigned 8 bit). This sound format is of sufficient
quality to reproduce, for instance, recorded human voice. Thus,
the file you’ll need in the following step of the process should be
Figure 2. Spectrum of human speech saying “ta-te-ti-to-tu”.
exported as:
mono (1-channel),
8-bit, PCM WAVE.
Converting sound to text Figure 3. Dialog Window to import WAV files into your Arduino
Let’s look at importing your sound file as a header file that you can sketches as header files.
add to your Arduino sketch. Since the ATmega 328 microcontroller
has 32 KB of Flash memory space, you can use part of that space to
store the sound file. You can store large chunks of data into memory Playing back a sampled sound is not obvious, since it requires
arrays by using the Progmem library from Atmel’s toolchain. 8-bit changing some of the features of the Arduino core works. There
exist libraries for Arduino Uno that hide all of the complexity of mak-
const unsigned char sounddata_data[] PROGMEM = {128, ing this type of sound player. However, I want you to take a chance
128, 128, and see how this is done at low level, like in Figure 4.
[...] The trick that can get Arduino to play a sound sample is the so-
69, 62, 59, 57, 52, 50, 56, 65, 74, 86, 96, 109, 116, }; called Fast PWM, a feature of Atmel’s Atmega family (other brands
have it as well, but Arduino Uno runs on Atmel chips). There is a reg-
sound is nothing but a stream of numbers between 0 and 255. It can ister that allows running PWM at the amazing rate of up to half the
be declared like: clock speed. This allows nice things to be done like playing sound
I have created a tool for Arduino’s IDE that enables you to open WAV with 1-bit outputs. The only limitation of Fast PWM is that it oper-
files and import them directly as part of your code. You can down- ates at a resolution of 8 bits only. That’s why you should encode
load it from the link mentioned in the reference list, and add it to your sound files at 8 bits.
your sketchbook folder. Place the file into your Arduino sketchbook
folder and uncompress it there. To get it all to work, you use two out of the three internal timer reg-
It will add the following folder to your sketchbook: tools/SoundData istries inside the processor:
After rebooting the IDE, you will see a new item under the Tools
menu called SoundData. Clicking on it produces a dialog window
enabling you to select a WAV file (see Figure 3). The second but-
ton, titled Generate Code will open the WAV file, check whether
it is properly encoded, and add a new tab to your code titled
• The first clock operates at SAMPLE_RATE and is used to read the // increase the sample count
next value from the computer generated sounddata_data array. ++sample;
This value sets up the duty cycle for the PWM stream being out- }
put through pin 11.
• The second timer ticks at 62500 times per second (16000000 /
256), i.e. much faster than the playback rate (default SAMPLE_ low level commands in order to override the timers. I will describe
RATE is 8 kHz). As explained earlier, this generates a digital sig- the functions one by one. Let’s start with TIMER1:
nal whose spectrum is very similar in shape to that of the origi- ISR is the name for the interrupt handling function inside the micro-
nal sound file. In other words, by means of increasing the PWM controller. An interrupt is an event that will tell the chip to stop
frequency, the generated signal gets a spectrum that resembles doing anything it is into at the time and attend a certain event. Pro-
the one of the sound. In many applications this is more than cessors can have both internal and external interrupts. The internal
enough to play sound at a low price. ones are timers, while the external ones happen when certain pins
change from HIGH to LOW or vice versa.
Note: by adding this code to your Arduino sketch you will be over- This one instance of the ISR function is taking care of the arrival of
riding some of the basic commands of the Arduino language. E.g. internal TIMER1 events. Every time TIMER1 ticks, this function will
the PWM will stop working at some of the pins, and the delay() func- do the following:
tion will not operate as expected. This will only affect this sketch.
The first time this technique was shown in the Arduino world was • increase the counter used to address the sound data;
back in 2008. Michael Smith published an example on the Arduino • check whether the end of the sound data array is reached;
playground that would play the sound of a MAC computer boot- • if not at the end, load the next sample from the array;
ing up, upon reset of the Arduino Diecimila board. Michael’s code • if at the end, fade the sound to zero.
was based upon the work of many others which I have included as
references [3].
void startPlayback()
Just to make it easy, the SoundData tool will not just generate the {
sound information, but also include the code to the basic sound- pinMode(speakerPin, OUTPUT);
playback example inside your sketch. Beware, make sure your
sketch is empty when calling the tool, as it will overwrite anything // Set up Timer 2 to do pulse width modulation on
on your IDE. the speaker
The basic program to play 1-bit sound (Figure 5) includes a lot of // pin.
// No prescaler (p.158)
TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) |
_BV(CS10);
Both timers TIMER1 and TIMER2 are initialized as part of the startPlay-
back function. Let’s see how what looks like: Mask limit testing, Colour persistence modes,
measurements and math channels all as standard and free updates
Although the sequence of low level commands is explained in the
ALL MODELS INCLUDE PROBES, FULL SOFTWARE AND 5 YEAR WARRANTY.
code, a summary of it may be useful for the non-expert:
• turn the pin for the speaker into an output;
• configure the board to use the internal clock for this;
• initialise Fast PWM mode;
www.picotech.com/PS164
elektor 09-2012 166 57
from a program to play sound this way. In this case, the example by
default will call startPlayback() within setup. In this way the sound
will be played just once.
Closing words
This article is an introduction to different ways of producing sound
using Arduino. You have seen how to play tones by means of basic
Arduino functions. Libraries have been described that simplify the
way basic melodies can be played using inexpensive piezo buzzers.
Finally you got a sneak peek at the programming behind Fast PWM
to generate 1-bit sound.
Figure 6. Arduino Uno is perfectly capable of driving a small
loudspeaker directly.
All code chunks and tools discussed here are packed into a ZIP file
[4], including properly formatted sound files for you to try all the
examples. I also created a new tool for the Arduino IDE that will
• configure TIMER2 to run the PWM that will play the sound, the help you with the importing of short WAVE files into Arduino’s pro-
way to set the duty cycle to the PWM is by changing the value gram memory. The tool enables you to load sounds and play them
of the register called OCR2A; back, change their sample rate, play them backwards or scratch
• load the first sound sample into OCR2A; the sound. In terms of hardware, you connect your loudspeaker as
• stop the interrupts for a second – cli()– so that we can config- shown in Figure 6.
ure the TIMER1 without breaks;
• configure TIMER1 to tick for picking up the next sample; But don’t stop here! There is a lot to explore — for instance using
• load the last sound sample; pin 3 in parallel with pin 11 to produce stereo sound. Or create 8-bit
• restart the interrupts – sei(). synthesizers with the ability of mixing four sound lines on a single
channel. What about transforming your Arduino board into a MIDI
void stopPlayback() activated soundcard?
{
// Disable playback per-sample interrupt. Happy hacking. See you in a month.
TIMSK1 &= ~_BV(OCIE1A); (120427)
ting hooked to this trend of IoT in many ways. One of them is don’t see the library named ‘GSM3’ in the list, you will have to down-
through M2M and for that we teamed up with Telefonica’s Physi- load it from the official Arduino website hosting this project [4] and
cal Internet Laboratory. install it as explained under the ‘Library’ link. Essentially, adding a
We have been collaborating in making an official Arduino shield that new library to the IDE is done by creating a folder called ‘libraries’
easily handles GSM/GPRS (also known as 1G and 2G) communication. inside your sketchbook and uncompressing the file you got from the
We also wrote a library that simplifies the process of texting (SMS website [4] directly there. After that, you should restart the Arduino
transmission), setting up calls or publishing some data on a website. IDE and the library will then show up in the above mentioned menu.
The design is open source and the software can easily be ported With the library you will be installing not only the code to execute
to support other shields (with potentially other series of AT-Com- different commands on the modem, but also a series of examples
mands; more about this later). Finally, the shield comes with a SIM that will allow you to:
card, courtesy of Telefonica, that will offer you the possibility to
hook up to send/receive text (SMS), set/get voice calls, and con- • test whether the modem is working properly;
nect to the Internet through the GPRS network. • text, i.e. send/receive SMS;
• place/get phonecalls (voice);
Note: This shield is not locked to the accompanying SIM; you could • open a TCP/IP connection and exchange data over the Internet,
use any other GSM/GPRS SIM card available in your country. The using GPRS;
SIM is optimised for data communication; if you wanted to make • post/collect sensor information to/from Cosm (an online data
something controlled over Text (SMS), you should consider a SIM service);
card operating on your preferred network. If you were about to use • collect and show twitter messages.
the card coming with the board, you should follow the online acti-
vation instructions that come with it. The way to access the examples is simple: just use the menu to navi-
gate through “File / Examples / GSM3”.
Controlling a modem - AT commands
Modems are the devices behind the expansion of the Internet. In Checking your status
order to automate the way they would operate, a company called Likely, the first thing you want to do is checking whether your board
Hayes [2] invented in 1981 what would later be called the AT-Com- is working properly and whether your SIM card can be used by the
mand Set. The idea behind it all is for modems to change between shield for you to experiment with. To do so, open the example under
Command and Data operation modes. The Command mode would
allow performing operations on the modem itself like calling a cer-
// libraries
tain number, changing the baud rate (the speed at which the data
#include <GSM3ShieldV1ModemVerification.h>
would be transferred), etc.
The Hayes Command Set became a de-facto standard for the way
// modem verification object
devices should communicate over a serial port. That standard is still
GSM3ShieldV1ModemVerification modemTest;
operating inside many devices. When in Command mode, devices
start the communication sending the string “AT” which stands for // IMEI variable
“attention”. That would be followed by other strings that would String IMEI = “”;
translate in different operations to be performed at the other side.
On the other hand, when in Data mode, the data will just be proxied // serial monitor result messages
to/from the Internet through the modem. String oktext = “OK”;
The GSM/GPRS shield has a radio modem manufactured by Quec- String errortext = “ERROR”;
tel [3] that can be operated using a series of AT commands. Most of
those commands are specific to the modem on the board, but the void setup()
library that controls the shield has been written in a way that makes {
very easy to port it for other modems made by any manufacturer. // initialize serial communications
Also, the library hides all the complexity behind this mode of opera- Serial.begin(9600);
tion. Therefore, we are not going to refer to them in the rest of the
article. However, if you happened to use the GSM library in debug // start modem test (reset and check response)
mode, you would be prompted with many of those commands. Serial.print(“Starting modem test...”);
modemTest.begin();
Installing the GSM Library for Arduino Serial.println(oktext);
At the time of writing this article the Arduino IDE had reached revi- }
sion 1.0.1 and did not include the GSM library we’re about to dis-
cuss. Thus, when checking the menu ‘Sketch / Import Library’ if you
void loop()
{
// get modem IMEI
Serial.print(“Checking IMEI...”);
IMEI = modemTest.getIMEI();
the menu: “File / Examples / GSM3 / Tools / TestModem”. Figure 2. Configure your GPRS connection using the Serial Monitor.
This code example allows you to check whether Arduino can detect
your radio modem. It also opens a connection to the radio modem
on your GSM shield and attempts to read the IMEI (International It is specific for the operator providing you with connectivity
Mobile Equipment Identity) from it — that’s a unique number for through the shield;
every device capable of connecting to the GSM/GPRS/3G/4G net- • login: some operators require a login;
work. For example, your cell phone identifies itself via the IMEI. • password: some operators require a password;
Every device on the GSM network has a unique ID. If everything goes • proxy: this is the address to a server that will channel all your
fine, after uploading the example on your Arduino Uno, and opening communication inside the operator’s network. Most likely it
the Serial Port Monitor, you should see something like in Figure 1. won’t be used.
If your modem was working correctly, the next thing is to check
whether you can connect to the GPRS network. To do so, we will The way this example works is by asking you for the information the
open the example: File / Examples / GSM3 / Tools / TestGPRS. I will not shield needs to connect to the network.
show the code here, as it is very similar to the previous one. This
example is used for testing that your SIM card allows connecting to Tip: remember activating the option ‘Newline’ in the dropdown
the GPRS network (the one provided with the shield does), but also menu at the bottom of your Serial Port monitor. In this way, when
to test your settings. clicking on ‘Send’ on the monitor, the system will add an end-of-line
(EOL) character that is needed for the modem to know the informa-
Note: this shield is using GSM/GPRS technology and not 3G. 3G and tion package came to an end.
GSM SIM cards look the same, but remember that some of the mod- I everything goes fine, you will see a window like in Figure 2. Those
ern operators do not offer 2G (the way we call GPRS) services. messages mean that your shield can connect to the Internet using
Connecting to the GPRS network calls for a tad more configuring your SIM. Please note that even if you couldn’t connect to the Inter-
than just texting (sending an SMS) or placing a call. There are four net, it may still be possible for you to text-out, get text (send/receive
parameters that need to be configured: SMS) and set up calls.
• APN: acronym for Access Point Name or the name of the Texting (sending an SMS)
domain the shield will connect through to reach the Internet. Texting or receiving text (sending or receiving SMS) with the GSM
Figure 4. All in hand: Arduino Uno + GSM/GPRS shield + Figure 5. Information being displayed on a browser as sent from
TinkerKit shield + TinkerKit LED. the GSM shield.
The following is a part of the example DecodeSMS you can find at once you are done trying out the example.
the download link [5] for this article. The only thing left to do is uncompress the code of the PHP mem-
The actual circuit boards are shown in Figure 4. ory cell in the given file [5] at the root folder, to your http server.
Make sure the file has read-write-execute permissions to allow it to
Device working over GPRS rewrite itself. You can then type the URL to your server (for example:
Let’s now see how to use an analogue sensor (a slider in this case) and http://server.com/path/index.php) in a browser to see the informa-
send data from it to a website. On the server side I am using Apache tion update every 10 seconds. Like in Figure 5.
and a very simple PHP script that will be taking the data sent by the
board and will update a part of the script containing plain HTML. This Closing words
is what I like to call a simple ‘online memory cell’. It is a PHP script I hope you found this brief introduction to the use of GSM/GPRS
that can take the data and display it. It will be possible to access that technology in your projects both educational and fun. I want to
same page using a browser to see the data changing over time. You highlight how relevant it is that we, as makers, can benefit from
will need to use the same APN/login/pass combination you used in the existence of an omnipresent wireless network. If you don’t need
very data-hungry devices, you could start controlling lots of things
over the cellphone network. As long as there is a way to power up
void loop() your board, you can gather and process data from almost anywhere.
{ (120506)
client.connect(server, 80);
Serial.println(“sending data...”); Internet Links
client.print(“GET “);
[1] Ericsson’s memo on 50 billion connected devices:
client.print(path);
client.print(query); www.ericsson.com/res/docs/whitepapers/wp-50-billions.pdf
client.print(analogRead(A0)); // take the value [2] on the AT-Command set:
// on A0 and send it http://en.wikipedia.org/wiki/Hayes_command_set
client.println(“ HTTP/1.1”);
[3] Quectel AT-Command description:
client.print(“HOST: “);
client.println(server); http://datasphere.eu/en?t=/documentManager/sfdoc.file.
client.println(); supply&fileID=1285079825955
client.stop(); [4] Arduino website hosting information about the GSM shield:
delay(10000); http://labs.arduino.cc/GPRS/Index
}
[5] Compressed file with all the examples and images:
www.elektor.com/120506
the previous GPRS example for this code to work properly. Here’s how
to send an analogue value to a server every 10 seconds:
You need to have a server to run the php code that will be reading
Acknowledgements
from your Arduino board. The server needs to be publicly accessible
My sincere thanks are due to the whole Physical Internet Laboratory
as you want your Arduino board to post information to it and to read
that data using a browser, a phone or any other web-enabled device. team at Telefonica Research and Development, for their support in
Important: keep in mind that this code is just a proof of concept, it is making this new Arduino shield possible. Special thanks go out to F. J.
not safe in any way and you should probably disable the PHP script Zorzano for being there debugging code until late many nights.
The Android OS allows anyone to create physical add-ons for smart- Tip: before continuing reading the article, we strongly recom-
phones. This article provides an introduction into setting up your mend you download all the code used in it, as we are only showing
system for developing add-ons and Apps connected to them. excerpts, due to the length of each one of the examples.
The hardware designs are derivatives of different Arduino boards. Note: For this automatic App installation to work, you need to acti-
For this article, we’ll focus on using an Arduino Mega ADK, which is vate the option that allows your phone to install applications from
compatible with the first version of the AOAP. The code presented outside the official Google Play Market. Follow Settings à Applica-
in this article should work with later ADK compatible boards (like tions and make sure the option “Unknown sources” is active. Also
Arduino Due and the like). It should also be possible to replicate the make sure your phone has a data connection either over Wi-Fi or the
experiments shown in here using an Arduino Uno and a USB Host phone network to download the App from our servers.
Shield for Arduino like the one in Figure 1.
One of the main features the AOAP brings into play is ‘App Wakeup’ Installing the USB Host library for Arduino
upon accessory detection. When an accessory is plugged to the At the time of writing this article the Arduino IDE did not include the
phone or tablet, it triggers a call to an application whose name is USB Host library we are presenting. Therefore when you examine
determined by the accessory. In case the App wasn’t installed in the the “Sketch / Import Library” menu you don’t see the library named
device, the accessory would also inform about a URL from where to ‘USB Host’ in the list — you will have to download the library from
download and install the application. the official Arduino website hosting this project [3] and install it.
Thanks to the way AOAP works, once you have the right code run-
ning on the Arduino Mega ADK, even if you have no applications in
your phone ready to take advantage of the accessory, your Android Figure 2. Arduino Mega ADK connected to a PC and to a phone.
Essentially, adding a new library to the IDE phone’s screenshot is the same one as in
is done by creating a folder called ‘libraries’ code listing 1. In other words, you deter-
inside your sketchbook and uncompressing mine in your Arduino code, where the App
the file you got from the website [3] directly is located on the Internet. In this case, we
there. After that, you should restart the have set up a website for you to check all
Arduino IDE and the library will then show the examples from your phone. You should
up in the above-mentioned menu. open the browser in your Android device
If you had a previous version of this library or and see the site as in Figure 5.
one obtained from a different website than You will have to download it and install the
the Arduino one, we strongly recommend App by clicking the link underneath the
you uninstall it before bringing in this new image It will first get downloaded and then
version, as they might be incompatible. effectively loaded into your device. Next you
With the library you will be installing not need to click on the file (called “Elektor_
only the code to execute different com- Figure 3. Screenshot from the Serial Port MIAU.apk”) which will install it. If the App
mands on the modem, but also a series of Monitor when linking an Android device to is already in your phone, you will instead
examples that will allow you to: your Arduino Mega ADK. get an invitation to load the right applica-
tion when plugging in the accessory again
• test whether the ADK mode is working (Figure 6).
properly;
• send digital/analogue values from the board to the phone; Once you have given permission for the App to boot, it will present
• receive values from the phone into the board; a GIF image as part of a loop (Figure 7). We thought it would be fun
• debug the different types of USB devices connected to the to show that you can actually use some graphic capabilities on your
Arduino Mega ADK board. Android terminal and stay clear of the corny “Hello World” text on
the screen. However, this App is not using any information from
The way to access the examples is very simple, just use the menu the Arduino board, nor is it sending anything back to it — all it does
to navigate through: “File / Examples / USB is allow you to check whether your phone/
Host”. tablet supports Accessory Mode.
Let’s summarise what you need to have in place for you to replicate • The Arduino ADK Tool for Processing;
the experiments discussed in this article. • Eclipse [5] + ADT plugin (both optional and not used here).
• Android SDK with all the updates up to the latest version of the As with almost everything within the world of software, there are
APIs; many possible tools to write your applications in. The Android SDK
• Processing 2.0a8 or newer; is an external tool to your preferred code-editor that will compile,
Figure 5. Android phone showing Figure 6. Your Android phone asks for Figure 7: The MIAU App running on Android.
information about the App. confirmation before loading an App to
handle an accessory.
link libraries, simulate, compress and sign data captured by the Arduino Accessory.
your Android applications for a specific ver- (code listing 2)
sion of the OS. Processing is a tool aimed at graphic art-
On the other hand, Processing is a really ists and therefore it uses a paradigm where
good tool if you are starting to program, instead of having a ‘loop’, the main function
and therefore we created an Add-on for it in the program happens to be called ‘draw’.
as explained in the Getting Started Guide [3]. The code you write inside Processing’s IDE is
But if you really want to use a professional Java, but it hides all the complex operations
set of tools to write Android Apps on, you behind it. To check whether you have eve-
should consider Eclipse [5], the open source rything installed properly, follow the menu
IDE, and the associated tools. Android’s Sketch à Run on Device, as in Figure 9, this
developer site [6] explains how to install the should compile the code and upload it auto-
whole Eclipse-based toolchain step by step. matically to the phone.
I will assume you managed to install Pro- Once the App boots, you should see an
cessing and all the other tools without much image like Figure 10 on your phone’s
trouble. All of them are cross platform and screen. The App will boot directly and it will
should work for virtually any version of remain there. You will be able to look for it
your OS. So now we will write the code for on your App menu and run it as many times
Android using Processing. as you want to.
Your first App Figure 8. Screenshot of Processing running Note: At the time of writing, Processing
Android mode; note the Mode button in the
Before even thinking of making an App to didn’t allow signing Apps. The process of
upper right corner.
control your accessory, let’s make a very signing an App is what validates it for dis-
simple App using Processing to show some- tribution to other devices. Any Apps cre-
thing on the phone’s screen. For you to start ated with Processing will only work on the
writing Android Apps you will need to have devices you upload them to directly. If you
the Android SDK installed and the Process- wanted to send your Apps to a friend, you
ing software configured to compile Android should compile them using Eclipse, which
applications (Figure 8). Your IDE should is much more complicated at this point and
have a green colour scheme. will not be explained here. For example, the
“Elektor_MIAU” application is signed, allow-
Let’s start by making an App and running it ing anyone to download it from the Internet.
on the device straight away. The following
bit of code will show a square in the centre Make your App boot upon
of the screen. We will later add the code for Figure 9. Running the App directly on your accessory connection
the square to change depending on sensor phone. Now you made a very simple application
Figure 10. Screenshot App active on the Figure 12. Dialogue window to configure Figure 13. Dialogue window showing the
phone. the App to be booted for the accessory. notification for opening your Rectangle App.
that runs on your phone, it’s time to make it boot when you plug Acknowledgements
an accessory to it. Here is when the Arduino ADK Tool for Processing
Our thanks are due to the team at Circuits@Home for their first work
comes into play. If you installed it as explained at [3], no problems
on the USB Host Library for Arduino and Philip Lindsay for his work on
arise if you just call it from the Processing IDE. The new tool needed
for uploading accessory code to your Android device should show debugging the initial collection of official Arduino Libraries for Android.
up under the Tools à Arduino ADK menu as shown in Figure 11.
About the co-author
When choosing that option from the menu, the IDE will open a dia-
logue window allowing you introducing the name of the app and Andreas Goransson is an interaction designer based in Malmö.
the manufacturer of the accessory (Figure 12). That is what Android Currently he is teaching software at Malmö University and he is writing
Accessories need to identify the App to connect to. You will have a book about Android and Arduino together with David Cuartiellies.
to make this match with the code on Arduino, as shown below in
code listing 3.
You could now modify code listing 1 where we were calling the Hel- Internet Links & References
loCat application to call this new one. [1] Worldwide smartphone market, by operating system:
(code listing 3)
http://mobithinking.com/mobile-marketing-tools/
latest-mobile-stats/a#smartphoneos
Note that this is just a snippet of code to modify code listing 1 to
make it call your first Accessory App for Android. [2] Accessory Development Kit: http://developer.android.com/tools/
Having done that and uploaded the code to your Arduino Mega adk/index.html
ADK, whenever you plug your phone to the board, it will boot your [3] Arduino with Android at Arduino.cc: http://labs.arduino.cc/ADK/
drawing rectangle App, as pictured in Figure 13.
AccessoryMode
179
46 | December 2012 | www.elektor.com/magazine
Experimental setup:
the processing side
Let’s first look into the software you need to pro-
gram you Android applications:
180
www.elektor.com/magazine | December 2012 | 47
#include <AndroidAccessory.h>
// led variables
int ledPin = 10;
// counters
long timer = millis();
void setup() {
// start the connection to the device over the USB host:
usb.begin();
// configure the LED pin as output
Remember to upload it to the board once you return an affirmative answer. When the phone
are done! starts sending data, usb.available() will return
a value different from zero that Arduino will then
This code example will be listening to the USB read and store in the variable val. Something
port. Once the phone connects to the Arduino very similar to what’s pictured in Figure 3 should
board, the usb.isConnected() function will happen at your place too.
import cc.arduino.*;
void setup() {
// Lock PORTRAIT view
orientation( PORTRAIT );
// initialize the ADK object
arduino = new ArduinoAdkUsb( this );
if ( arduino.list() != null )
arduino.connect( arduino.list()[0] );
}
void draw() {
// Draws a filled rect based on arduino connection state
connected( arduino.isConnected() );
}
181
48 | December 2012 | www.elektor.com/magazine
pinMode(ledPin, OUTPUT);
}
void loop() {
// print to USB 10 times per second
if(millis()-timer>100) {
if (usb.isConnected()) { // is the USB connection open?
if (usb.available() > 0) { // is there data?
char val = usb.read();
// ‘a’ turns the LED on, ‘b’ off
if( val == ‘a’ )
digitalWrite( ledPin, HIGH );
else if( val == ‘b’ )
digitalWrite( ledPin, LOW );
}
timer = millis();
}
}
}
On the Processing side you will need to write a just lifted. We are detecting the events ‘start’
program that will check for the event of some- and ‘stop touching’ as we don’t want to flood
one touching the screen. If there is a connec- the communication port.
tion over the cable, the phone will then send ‘a’ Open your Processing IDE and search for the
to indicate someone just started touching the adk_digitalWrite example. You will find it at
screen. It will send ‘b’ to indicate the finger was the menu ‘Examples / Contributed Libraries /
[…]
182
www.elektor.com/magazine | December 2012 | 49
ArduinoADKUsb’ if you installed the library prop- GIFdecoder library we used last month to play
erly, see Listing 2. animated GIFs inside your Android app. As we
want to keep the programming simple to under-
The code in Listing 2 is mostly self-explana- stand, the way we recommend you animate your
tory. One part you might find a little upsetting Rabbit is very simple: you should make an array
is the method definition surfaceTouchEvent of images designed to contain the frames you
(Motion‑Event event). Java is a very powerful want to be part of the animation. This concept is
programming language and in this case it allows based in the Processing example Animated Sprite
overriding some of the existing callback methods. by James Paterson [2], see Listing 3 at [3].
Whenever you touch the Android’s screen, the
system is configured to call that specific func- Note: Listing 3 is not showing the Animation class
tion (surfaceTouchEvent(MotionEvent event)). that will open all the images named s00.gif, s01.
If you override it in your program (basically, if gif, etc. We recommend you check the full code
you rewrite it) you can make it do whatever you listing coming with the example Elektor_Sim-
want to. ple_Animation. You will also see a folder called
In this case, we detect two of the many system ‘data’ inside the Processing sketch folder contain-
events: MotionEvent.ACTION_DOWN (the equiva- ing all the images that are part of the animation
lent to start pressing the screen) to send ‘a’ to (54 in total).
Arduino and MotionEvent.ACTION_UP (the same
for stop pressing the screen) to send ‘b’. The ultimate setup
Next up, we’ll modify the example to include the
A bit more fun library handling the USB communication. We rec-
Once you have got the first example to run… ommend you merge the previous code with the
why not building something a little more fun? adk_analogRead example. The result will look
The Internet is packed with cats, that’s why we like the code in Listing 4 you can find at [3].
decided to create the Elektor_Miau application There are a couple of things in the example that
last month. We wanted to make a fun comment need to be highlighted. First there is an array
and, at the same time, show you that it is pos- called delays[i]. It’s responsible for control-
sible to start making things more appealing than ling the time each frame will be displayed in the
the classic “Hello World” example when learning animation. Every time you load a new frame,
programming. you also change the timer length in the condi-
tion millis()- timer > delays[index]. You
This month we want to build upon the story and are welcome to experiment with the numbers in
encourage you to learn how to make small ani- that array to see how it affects the animation.
mations using Processing. You can look for any Remember there have to be as many numbers
animated GIF on the internet and use it to rep- as frames in the animation.
licate what you will see in this example. The
only condition is that you need the animated Also there is a keyFrames[i] array that holds a
gif exported as frames. You should name them list of frames in the animation that will be expect-
in the following way: s00.gif, s01.gif, s02.gif … ing an action from the user for the movie to con-
You are of course welcome to use the same image tinue. In this case we are going to detect button
we are using for our article. We got an illustra- presses. The animation will stop at those frames
tor to draw for us a small rabbit appearing out until the user presses the button attached to the
of a hat. The goal of this example is making a TinkerKit Shield as seen in Figure 5.
small interactive game where you will use sen-
sors (a button and a potentiometer) to interact If you look at the line checking how many
with the small rabbit on the phone’s screen, like bytes arrived from the Arduino board, we check
in Figure 4. arduino.available() >= 3 to see whether
we got 3 bytes. The information is encoded as
This time it will best to start looking at the code follows:
to be running inside the phone. The first step is
building an animation inside Processing. If you • Byte 0: has to be 255, it’s a marker we use
are versed in Android, you can try porting the to distinguish when the block starts;
183
50 | December 2012 | www.elektor.com/magazine
Closing words
With this article we finish our introduction to Figure 5.
Android and how it connects to Arduino. You Button and potentiometer
should be ready to create new projects, build on top of the TinkerKit
robots where your phone can be the intelligence, Shield.
or integrate sensors that aren’t coming with your
Android device. If you make any cool projects
using any of our code don’t hesitate to drop us
a line at [email protected], we will be happy to
let the rest of the community know about them. Acknowledgements
(120573)
The Arduino design team expresses its
thanks to the team at Circuits@Home for
their first work on the USB Host Library for
Arduino and Philip Lindsay for his work on
Internet Links & References
debugging the initial collection of official
[1] Processing Project: http://processing.org Arduino Libraries for Android. Also to
[2] Animations using multiple images with Pro- Rodrigo Calvo who helped unveiling how to
cessing: http://processing.org/learning/top- improve the USB Host library to work with
ics/sequential.html Android 4.1 devices. And Laura Balboa for
[3] www.elektor.com/120573 her illustration job.
184
www.elektor.com/magazine | December 2012 | 51
185
54 | January & February 2013 | www.elektor-magazine.com
186
www.elektor-magazine.com | January & February 2013 | 55
Install the library for the display You will have to download the library from
I put together a library using code from several Elektor’s website [4] and install it. Remember
authors, just re-factored the code to respect that adding a new library to the IDE is effectively
the Arduino library style, and added a couple done by creating a folder called ‘libraries’ inside
of simple examples. The library is compatible your sketchbook and uncompressing the file you
with revisions of the IDE 1.0 and above. Every- got from the website right there. After that, you
thing is aimed at using an RTC (real time clock) should restart the Arduino IDE and the library
unit built on the Arduino prototyping shield, see will then show up in the above mentioned menu.
Figure 3. Note: Arduino’s IDE is going to include a system
Listing 1. Listing 2.
Listing 3.
#include <fonts.h>
#include <HT1632c.h>
#include <images.h>
#define heart_icon_width 17
#define heart_icon_height 16
void setup() {
display.setup();
}
void loop() {
display.image(heart_icon, 0, 0, heart_icon_width, heart_icon_height);
}
187
56 | January & February 2013 | www.elektor-magazine.com
188
www.elektor-magazine.com | January & February 2013 | 57
189
58 | January & February 2013 | www.elektor-magazine.com
Listing 4.
[…]
void setup() {
Serial.begin(9600);
/* Initialize a new chip by turning off write protection and clearing the
clock halt flag. These methods needn’t always be called. See the DS1302
datasheet for details. */
rtc.write_protect(false);
rtc.halt(false);
[…]
190
www.elektor-magazine.com | January & February 2013 | 59
Closing words
Figure 7. With the excuse of building ‘something’ from
“Hey, it’s been three days ‘any’ parts, we got the chance to experiment
since I last got some water”.
with drivers capable of driving hundreds of LEDs,
as well as with timers. The next step is going to
The second button is all about cutting the bat- be checking out whether my prototype fits the
tery power; unless you press it, the system is not purpose. I will place it in my staircase and see if
going to get any power. Microcontrollers wake ‘our’ plants get a better life.
up rapidly; in this case, the first thing you will I am sure you can figure out multiple uses for
see on the screen is the Arduino logotype and, a box with a display and two buttons beyond
after a few seconds, it will tell you the number the one described in this article: from clocks
to games. Just add a buzzer and some more
pushbuttons to the mix and turn it into a small
Listing 6.
game console.
[…] (120714)
saveTimeButton = digitalRead(saveTimePin);
if(saveTimeButton == LOW && saveTimeButtonOld == HIGH) {
// we will use the Time stored in t to save the time Acknowledgements
EEPROM.write(0, t.yr); // position 0 – year to the guys at the Arduino Store, who kindly
EEPROM.write(1, t.mon); // position 1 – month gave me the display I used for this project some
EEPROM.write(2, t.date); // position 2 – day time ago.
// clear the display
display.cls();
// show a thank you text
display.text(“nice!”, 2, 5);
// stay here
while(true) {};
}
saveTimeButtonOld = saveTimeButton;
}
191
60 | January & February 2013 | www.elektor-magazine.com
• Breadboard
• One 1-megohm (1 MΩ) resistor
• Jumper wires
• TinkerKit Module: Ultra Bright White LED +
TinkerKit wire
The idea
We are going to create a trap where a home-made
toy (pictured above) is constantly guarding our
cookie jar. If anyone touches the jar, a camera
hidden inside the toy shoots a picture and hope-
fully photographs the robber’s face.
Capturing the image is done with a program writ-
ten in Processing, which will be taking pictures
through the camera inside the toy. We will then
activate the camera from an Arduino board that’s
effectively detecting when someone touches our
precious cookie storage. This will be achieved
using the capacitive sensing capabilities imple-
mented on all ATMega chips.
The concept sounds simple enough and integrat-
ing the parts is also kind of easy. First we will
make a Processing sketch though that’s effec-
tively capable of triggering the photo shot using
Arduino over the computer’s serial port.
192
38 | April 2013 | www.elektor-magazine.com
(like me) read this other link [3] where it explains Table 1.
which packages you need to install for Process-
index name size fps
ing to take control of your webcam.
[0] /dev/video0 1280x720 10
From Processing we are going to use the Video
and Serial libraries, and both come with the IDE [1] /dev/video0 960x540 10
by default. Therefore you don’t need to worry [2] /dev/video0 800x448 15
about finding and installing extra libraries this [3] /dev/video0 640x480 30
time. ...
[86] /dev/video0 320x240 15
Capturing a (webcam) image
The latest version of Processing implements its
video library using Gstreamer [4], a suite of soft- Listing 1
ware tools capable of accessing various video /**
devices connected to your computer, pipe the * Check cameras in Processing
streams, implement codecs, etc. You can access */
the video stream coming from a camera con-
nected to the computer through a file handler. import processing.video.*;
For example, on *nix based machines (like Linux
or Mac OSX), all your video devices are listed Capture cam;
under the file /dev/videoXX, where XX represents
void setup() {
a different number identifying different cameras.
For this project, as I am programming from my
String[] cameras = Capture.list();
laptop, /dev/video0 represents the camera that
comes embedded with the computer, while / if (cameras.length == 0) {
dev/video1 is the camera I have placed inside println(“There are no cameras available for capture.”);
the toy guard. exit();
The first thing you need to take into account is } else {
that the same camera can have different configu- println(“Available cameras:”);
rations in terms of resolution and image shooting for (int i = 0; i < cameras.length; i++) {
speed. Therefore, you should run the program println(“[“+i+”] “+cameras[i]);
shown in code Listing 1, and check out the }
}
different cameras and possible configurations
}
in your computer as they will come out printed
in the console inside your Processing IDE. The
void draw() {
program processes code to do a survey of cam- }
eras available in your computer.
193
www.elektor-magazine.com | April 2013 | 39
shown in Listing 2, this will effectively activate picture whenever you either press “p” or “P”.
the selected camera.
Look at the highlighted line:
Using the keyboard in Processing saveFrame(“pic-######.png”) — this is the
We want to capture an image when someone method that saves an image in the sketch folder
touches the jar, and it is good to test that func- using the name “pic-######” in PNG format,
tionality beforehand. I would suggest you imple- and with that extension. The string of hash signs
ment and test something like using the keyboard ###### represents the picture number. Differ-
to take a picture. Just modify Listing 2 with the ent pictures will have different numbers.
code in Listing 3, and the camera will store a
Note that saveFrame() saves the information
contained inside the application’s frame only —
Listing 4
not the rest of the desktop.
void draw() {
if (cam.available() == true) { Timestamp that picture!
cam.read(); As saveFrame()writes a pixel-by-pixel copy of
}
your application into a file, it is possible to add a
image(cam, 0, 0);
layer of information on top of the image. In our
String timeStamp = String.format(“%02d”, hour());
case, we are interested in knowing when the jar
timeStamp += “:” + String.format(“%02d”, minute());
timeStamp += “:” + String.format(“%02d”, second()); was opened and its precious contents thieved! In
timeStamp += “ “ + year(); CSI Miami speak: the crime has to be evidenced
timeStamp += “/” + String.format(“%02d”, month()); in terms of time. So let’s put a timestamp on the
timeStamp += “/” + String.format(“%02d”, day()); camera picture as shown in Listing 4. This will
also print a note to STDOUT.
text(timeStamp, 10, height-10); // superimpose text on
image An experimental but successful snapshot is shown
in Figure 1. The low resolution is obviously due
// for the keyboard detection to work, you need to have
to the webcam, but the detectives will be equally
// clicked on the application window first (aka focus)
happy.
if(keyPressed) {
Next thing is writing some Arduino code to deter-
if (key == ‘p’ || key == ‘P’) {
saveFrame(“pic-######.png”); mine whether someone touched the jar, and
println(“capturing Frame at: “ + timeStamp); // report immediately return a command to the computer.
to the console
} Detecting touch with Arduino:
} CapacitiveSensor Lib
} Inside archive file 120745-11.zip created for
this article you will find a folder named “Ardu-
ino” which has a subfolder called “libraries”. The
Listing 3 archive file may be downloaded for free from
[6]. You should copy the contents of that folder
void draw() {
inside your Arduino libraries folder. The latter is
if (cam.available() == true) {
cam.read(); located inside your Arduino sketchbook folder,
} usually stored at “My Documents/Arduino” in
image(cam, 0, 0); Windows computers, or “Documents/Arduino”
in Mac and Linux.
// for the keyboard detection to work, you need to have
// clicked on the application window first (aka focus) The CapacitiveSensor library was originally made
if(keyPressed) { in 2008 by Paul Badger [5] and it comes with
if (key == ‘p’ || key == ‘P’) { one single but clear example. I recommend you
saveFrame(“pic-######.png”);
take a look at the Arduino playground page ref-
}
erenced at [5]; there you will find everything you
}
need to know about how capacitive sensors work.
}
Essentially, the library uses two digital pins: one
194
40 | April 2013 | www.elektor-magazine.com
195
www.elektor-magazine.com | April 2013 | 41
196
42 | April 2013 | www.elektor-magazine.com
References
[1] Processing project: http://processing.org
[2] Issues when using webcams in Processing:
http://wiki.processing.org/w/Video_Issues
[3] List of packages to get GStreamer for Linux
to take webcams inside Processing:
http://forum.processing.org/topic/how-is-vid-
eo-on-linux-handled#25080000001764427
[4] Gstreamer official website:
http://gstreamer.freedesktop.org/
[5] Paul Badger’s CapacitiveSensor Library for
Arduino:
http://playground.arduino.cc/Main/
CapacitiveSensor
[6] Cooke Guard project software:
www.elektor-magazine.com/120745
Figure 5.
Framed!! Who’s stealing our
cookies?
Listing 6
import processing.video.*;
import processing.serial.*;
Capture cam;
Serial myPort; // The serial port
void setup() {
[…]
void draw() {
[…]
if (myPort.available() > 0) {
int inByte = myPort.read();
if(inByte == ‘p’) {
saveFrame(“pic-######.png”);
println(“capturing Frame at: “ + timeStamp);
}
}
}
Figure 4. Detail of Cookie Guard’s neck construction.
197
www.elektor-magazine.com | April 2013 | 43
By David Cuartielles, For the mobile weather station, we wanted stu- balloon, we had to figure out a different method.
Clara Leivas, and dents to be able to actively gather data from That, along with a few other unexpected issues,
Tien Pham different environments—in the city, through the complicated what we thought would be a rela-
(Arduino Verkstad) parks, and along the riverbanks. Each environ- tively simple project.
ment might provide a different set of data for In order to keep costs low, we decided to use
the students to use for comparison. a cluster of balloons rather than a single large
We decided to use tethered balloons so that stu- weather balloon. We purchased a consumer-size
dents could control the placement and gather data helium tank along with regular party balloons.
at higher altitudes. This also gave us the oppor- Searching online, we were able to estimate that
tunity to include a time-lapse camera enabling it would take about 25 or so balloons to lift the
the kids to correlate and visualize their data to Arduino, a shield, the sensors and a lightweight
the different environments. frame or container.
Next up was the anemometer. To measure wind
Decisions, decisions speed accurately wasn’t our goal so much as to
We set about gathering the required electron- be able to compare the speeds. Deciding to only
ics—a temperature/humidity sensor, a micro- measure rotations per second, we thought to use
phone, a lightweight spy camera, and a CO2 sen- a light dependent resistor (LDR) and an LED to
sor. To our knowledge, there were no ready-built create a sensor. The idea was to place the LDR
wind speed sensors that would fit our require- opposite the LED with a paper disk in between
ments. Most anemometers require a stable sur- the two. The disk would have a slot cut out and
face or object to attach to in order to get their as the blades would turn and intermittently allow
readings, but because ours would hang from a light to pass through to trigger the LDR.
198
70 | January & February 2014 | www.elektor-magazine.com
Problem solving
As part of our first attempt we tried placing the
LED very close to the LDR hoping the magnitude of
the readings would obviate the need for shielding.
As suspected, this wasn’t the case, so we decided
to find a dark box to house the electronics.
This presented another set of problems regard-
ing mechanic assembly, and so we set out minds
on using an infrared (IR) sensor and a black and
white paper disk instead. This looked more hope-
ful as it required just one electronic component,
meaning less room for mechanical error.
We tested the assembly and got it to work. The
next part was a bit trickier—how to actually catch
the wind in order to spin the disk. Because our
aim was to build a lightweight weather station,
we couldn’t exactly use its weight to stabilize it
Figure 1. A paper-and-tape crafted model of a turbine for
so that an anemometer could spin while having
use as an anemometer. Hopefully!
a stationary point as reference.
Again, to make the assembly reproducible any-
where by students, we decided to try papercraft.
We made a simple turbine blade out of paper and Figure 2. About a dozen balloons appeared enough to
tape (Figure 1). We mounted it horizontally so lift the Arduino Mobile Weather Station assembly, rather
than 25 as initially calculated.
that regardless of wind direction, it would spin.
In order to keep the balloons stable, we clustered
the balloons at the four corners hoping that the
weather station wouldn’t spin.
199
www.elektor-magazine.com | January & February 2014 | 71
Bidirectional turbine
Finally, we decided to design and 3D-print a tur-
bine that would blow in both directions. This was
achieved by twisting the shape of the blades
along its spinning axis. Since the test school we
were working with already had a 3D printer, they
could at least print the turbine out at a reason-
able cost. It was a compromise, but we couldn’t
think of anything better at the time (and time
was pressing).
The person in charge of the mechanics (that
would be me, David) didn’t have much experi-
ence with 3D printing—in fact, it would be his
Figure 3. Initially impressed It was only then that we realized what should first time. After a few tries, a rough, workable
with the Rokkaku kite. have been obvious. The design of the anemom- prototype was printed.
eter wouldn’t work because the balloons would We tested it again and were glad to find that,
never be upright if tethered. At best, it would be yes, the anemometer worked at almost every
at a slight angle. Perhaps it was because of the angle. At this point, we decided to also laser-cut
Figure 4. Connection
traditional design of free-floating weather bal- a frame out of MDF (plywood) so that it could
diagram intended for
loons that we didn’t consider this, but having a be mounted properly, and construction would be
Highschool kids working
at the “connect-wire-x-to- tether changed the dynamics completely. consistent. This would help to enable the sensors,
wire-y” level. Defeated, we tried several designs, one of which especially the IR sensor, to work well. (In terms
of cost, it would be relatively inexpensive for
us to use the communal laser cutter to produce
lightweight kits that we could send off. Again, a
compromise, but still affordable.)
Our joy was short lived as we again went to test
it outside (no balloons, just outdoors). Somehow,
the IR sensors were receiving a lot of interfer-
ence being outside. The only solution we could
find was to spray-paint the plastic casing black.
Eventually, we would have to print the casing
using only black plastic since other colors allowed
interference. The IR sensor proved to be just as
temperamental as the LDR. Moving the sensor
a few millimeters closer or farther from the disk
would prevent the sensor from reading values
correctly. In fact, it seemed that the IR sensor
we were using had a small working range toler-
ance, but we finally managed to get it working.
Goodbye balloons
We found that 3D printers, while being great for
prototyping, weren’t necessarily the most accu-
rate (perhaps it was simply a matter of inexpe-
rience) or smooth. Small points of friction where
the print was bumpy meant it now required more
200
72 | January & February 2014 | www.elektor-magazine.com
201
www.elektor-magazine.com | January & February 2014 | 73
202
74 | January & February 2014 | www.elektor-magazine.com
By Jos van Kempen (The Netherlands) capability, which can be used as a fancy remote control unit. Some
microcontroller boards have built-in Bluetooth capability, and with
others you can buy adapters or shields to add this capability. In this
Nearly all microcontroller circuits intended to control something article we use a small Arduino board with a Bluetooth shield (ITead,
have some form of user interface. This often involves several but- priced at around 15 pounds) augmented by a small I/O shield spe-
tons, knobs, LEDs and an LCD module. If you want something really cifically designed for this article.
nice, you might even use a touchscreen. A remote control can Articles about adding Bluetooth functionality to your own circuits
also be handy – preferably one that does not have to be aimed so have been published in previous issues of Elektor (September 2004
precisely. and February 2010). Elektor also published an article about program-
However, ‘luxury’ interfaces of this sort usually cost more and are ming Android apps in the June 2011 edition, and an Android control-
more difficult to implement. This doesn’t have to be the case. As ler using the audio output was described in December 2011.
you will see, an attractive user interface with remote control does
not heed to be expensive, and the programming is reasonably easy. Many of you are familiar with microcontroller programming,
There’s a good chance that your have a smartphone with Bluetooth although you may not have any experience with programming a
VCC
Arduino Board
R2
VCC RE2.A
1 G5V-2 D2
560R
R1 16 1N4148
RE1 K4 K8
1 G5V-2 D1
560R
VCC LED2
K5 K1 8 8
AREF
T1
mobile phone, so we devote only minimum attention to micro- RST
1 1
16 1N4148 R6
7 7
GND
2 2 6 6
controller programming in this article. However, we discuss 3.3V
3 3 LED1
4k7
5 5
D13
5V D12
smartphone programming in detail, from downloading and GND
4 4 T2 BC547B 4 4
D11
5 5 R5 3 3
installing the software to programming the various compo- GND
6 6 4k7 2 2
D10
Vin D9
nents of the desired user interface. We hope this information BC547B
R4 1 1
D8
100R
will be sufficient to enable you to program a user interface T3
R3
560R
troller. The source code of the application described here (for LED3
BS170
both the microcontroller and the smartphone) can of course VCC
10k
2 2 5 5
A1 D4
3 3 4 4
‘input’ and ‘print’ instructions to receive commands and send A2
4 4
R9
3 3
D3
A3 D2
data from and to the Arduino board. 5 5 2 2
1k
A4 D1
6 6 8 9 1 1
For this project we also developed a simple shield consisting A5 RE2.B
4
RE1.B
13
D0
PWM
NTC
using Bluetooth. All relay, switch and sensor lines are available 120075 - 11
6k8
on connector K9.
The microcontroller software (written in Bascom) works as fol- Figure 1. Schematic diagram of the I/O shield consisting of a pair of LEDs
lows (see Listing 1). and relays, an NTC thermistor, a pushbutton, and a PWM output with a
FET and an indicator LED.
A loop checks whether a character has been received. If the
Arduino receives an ‘R’, output D11 is activated (LED1 lights
up and relay RE1 is energised). If it receives an ‘r’, LED1 and RE1 are a ‘P’ (short for ‘PWM’) is received, the routine ‘Input’ waits for a
de-energised. Output D13 and the second LED and relay behave in value (the smartphone must send the character string ‘\r\n’ after
a similar manner, but they respond to the characters ‘O’ and ‘o’. If the value), and this value is used to drive the PWM output. FET T3
COMPONENT LIST
Resistors K7,K8 = 8-way pinheader socket
R1,R2,R3 = 560Ω K9 = 10-way pinheader socket
R4 = 100Ω RE1,RE2 = 5V miniature relay (e.g. TE Con-
R5,R6 = 4.7kΩ nectivity type MT2-C93401 or OMRON type
R7 = 10kΩ G5V-2-H1)
R8,R9 = 1kΩ S1 = pushbutton with make contact (e.g.
B3F-1000)
Semiconductors PCB # 120075-1 (see [1])
D1,D2 = 1N4148
LED1,LED2,LED3 = LED, red, 5 mm
T1,T2= BC547
T3 = BS170
3
Figure 2.
1
Miscellaneous
K1,K2 = 6-pin pinheader
The PCB layout is designed such that
the connectors mate with a standard
4
has a maximum rating of 60 V / 0.5 A. LED3 provides an indication 2. The Android Software Development Kit (SDK) is available at [3].
of the PWM level. After installing the SDK, you should have a look at the rest of the
The measured value of the NTC thermistor sensor R9 is also printed site, where you can find a lot of information about program-
with the format ‘T;adc(0);t’, and the signal level at input D7 to which ming, help files and USB drivers for your smartphone (which you
pushbutton S1 is connected is printed as ‘G’ if it is high (‘1’) or ‘g’ if will need later for downloading your app). The USB driver may
its is low (‘0’). The interface is configured with standard data rate already be installed on your PC if you use USB to exchange pho-
of 9600 baud, which means that a small waiting time (around 30 to tos, music and so on between your PC and your smartphone.
40 ms) is necessary after a value has been sent. 3. Download the ADT plugin for Eclipse from the same site, and
make note of the folder it goes into.
Required software 4. We used Eclipse Classic 3.6.2 as the integrated development
The following software that you need in order to program apps for environment (IDE). It can be downloaded from [4]. (Note: the
Android smartphones can be downloaded for free: latest Android plugin at the time of writing was ADT12, which
is not compatible with Eclipse 3.7.)
1. The Java language is used for programming. The Java Develop- 5. After installing Eclipse, you have to install the Android plugin
ment Kit (JDK) can be downloaded from the Oracle website [2]. (under Help | Install new software | Archive). Select the ADT zip
Interface implementation
Double-click main.xml to display the smartphone screen interface
(see Figure 4). The tabs below the screen layout can be used to
switch between the graphical layout and the automatically gener-
ated code.
A horizontal layout is already defined on the interface, including a
ListView for the detected Bluetooth devices, an EditText for the enter-
ing the text you want to send during a chat session, and a button
for sending the text.
In many cases the easiest way to place a component is to drag it to
the Outline pane. An error may occur when you drag the size on the
screen if your PC is configured for a comma decimal marker. In this
case you must replace the ‘,’ in the file main.xml with a ‘.’.
When you place a button or a radio button, you can define the pro-
cedure that will be executed if it is clicked by entering the name of
the procedure for the OnClick property.
The LEDs and relays (digital outputs) on the shield are operated by a
set of buttons or checkboxes. For this purpose, place a linear layout
of checkboxes or (toggle) buttons on the screen or in the outline
(which is often easier). Figure 4. The interface screen layout. It’s often easiest to place the
The switch state (digital input) is indicated on the interface (for a controls in the Outline pane.
you must first select the right COM port in the terminal emulator
(e.g. Hyperterminal or Advanced Serial Port Terminal) and establish
a connection.
After the app has been downloaded and tested, establish a connec-
tion by clicking the menu button at the lower left and choosing the
desired Bluetooth device (select connect to device –secure). If every-
thing goes right, you should see the message ‘connecting’ followed
a short while later by ‘connected: <device name>’ (Figure 5).
The code in the BluetoothChat file may look like this:
Figure 5. Select ‘Connect a device – Secure’ and then select one of
the already paired devices or scan for devices that are configured private CheckBox chkD13, chkD11,chkDIM;
as ‘discoverable’. You have to enter a code for (initial) pairing; (The variable must be declared if the checkbox
‘0000’ and ‘1234’ are common codes. properties are read (or written) in the program)
209
2 7/8-2011 elektor
4k7
P1
Arduino Nano support board [2]. This support
RIGHT
board is a suitable size for fitting to this robot 250R 1
and can be connected to two servomotors D1 K1 R8
driving the robot by way of connectors that S1
1
2k2
have been provided for this purpose. VCC
IR C1
4
GL
IC1 330n
IS471F R7
The circuit shown here allows a mobile robot 2
VO 2k2
to detect information about its immediate GND
+5V
surroundings by means of two microswitches 3
RIGHT
(end-of-travel detectors), two photoresistors, C4
and three infra-red proximity detectors. All
330n R1
this will enable the microcontroller to steer +5V 220R
P2 R2
the robot correctly by sending appropriate
CENTRE 1M
commands to the servomotors. 250R VT935G
D2 K3 G1
The interface circuit for the three infra-red 1 Right IR sensor signal 1
detectors is standard, and has already been VCC 4
IR C2
Right switch signal 2
14
GL 15
used in [3]. Potentiometers P1, P2, and P3 let IC2 Right LDR signal 3
330n R9 16
you adjust the current drive to the transmit- IS471F 2 Centre IR sensor signal 4
VO 2k2 17
ting diodes, and hence the maximum distance GND 5
18
3 6
at which the detector will be able to detect 19
+5V Left LDR signal 7
the presence of an obstacle. The 2.2 kΩ resis- 20
LEFT Left switch signal 8
tors protect the microcontroller against the C5 Left IR sensor signal 9
21
22
accidental short-circuits that might occur if +5V 10
P3
330n R4
23
the microcontroller pin is configured as an LEFT 220R
11
24
output and generates a logic level different R3 12
250R 25
1M 13
from that generated by the detector. D3
VT935G
G2
1
IR C3
The microswitches make it possible to detect VCC 4 SUB-D25
GL
the presence of an obstacle on the route and IC3 330n
IS471F R11
thus avoid collisions. They force the micro- VO
2
2k2
controller’s input pin low. GND
+5V
3
LEFT R10
Support Board
for Arduino Nano
By Philippe Frétaud & François Auger (Saint-Nazaire Institute of Technology, France)
The LilyPad board is a circular Arduino for As these two outputs can’t supply much
clothing applications, and the Nano is a current, an additional 5 V linear regulator
small Arduino module (18 × 43 mm) spe- has been incorporated into the support
cially designed for use with prototyping board. The 9 V input rail is also wired to K6
boards and breadboards. and K7.
(100396)
In place of the standard Arduino’s female
connectors, the Nano has two rows of 15 K4
solder pins on a 2.54 mm (0.1”) pitch. So
LEFT SERVO
it looks quite a bit like the boards for older
R1
microcontrollers like the Basic Stamp 2 or 10k
Readers Circuits contain contributions from Elektor readers for experimental purposes and further development by others.
The circuit(s) presented on these pages have not been tested for reproducibility or actual use in the Elektor Lab.
XBee XBee
Arduino EtherShield
GND AN0 +5V Arduino
Sensor
Extension shields
In this project we use a ready-made shield called EtherShield, which
provides Internet access capability for the Arduino module. It has an
RJ45 connector and can be linked to a router or a switch by a UTP cable. Figure 1. The instrumentation network consists of a node, a
A separate software library is available for the EtherShield module. gateway and a data platform at www.pachube.com.
It’s usually possible to stack several Arduino shields on top of each
other, as long as there aren’t any conflicts between the inputs and
outputs that are used. Although a standard XBee shield is available
for the Arduino module, it poses a problem here because it can-
not be stacked with the EtherShield module. For this reason, we S2 Arduinoshield
JP3
developed a shield that can accept an XBee module and can also be RX
RX
+5V A5
IC1
plugged on top of the EtherShield module. TX
D2
XBEE/USB
78L03 +3V3
A4
A3
The design of our shield module is reasonably straightforward (see D3 S1
JP1
JP2 A2
D4 A1
Figure 2). We included the voltage regulator because the Pro ver- D5
TX
XBEE/USB
C1 C2 C3
A0
D6
sion of the XBee module can draw up to 100 mA, which is too much D7
10u 10u 100n
for the integrated regulator of the Arduino module. The XBee mod- D8 VIN
+3V3 +5V
ule also requires a 3.3-V supply voltage. D9
D10
GND1
GND2
XB1
The XBee module communicates with the Arduino module via D11
D12 XBee
+5V
+3V3
pins 1 (RX) and 2 (TX). The Arduino module also uses these pins for D13
1
2
VCC AD0/DIO0
20
19
RESET
AGND DOUT AD1/DIO1 S3
serial communication with the PC. As we want to be able to pro- AREF R1
3
4
DIN/CONFIG AD2/DIO2
17
18
6 15
a pair of switches in the communication path. The Arduino module R2 R4 7
PWM0/RSSI ASSOC/AD5/DIO5
VREF
14 R3
8 13
normally communicates with TTL-level signals on pins 1 and 2, so a
1k
1k
15k
ON/SLEEP
9 12
DTR/SLEEP_RQ/DI8 CTS/DIO7
voltage divider reduces the level of the input signal on the XBee RX
10 11
LED2 GND RF_TX/AD4/DIO5 LED1
pin to 3 V. Level adjustment is not necessary in the other direction, RSSI ASSOC
(except the pushbutton) are leaded types. The Eagle and PDF files
of the PCB layout are included in the download file for this article [2].
Figure 2. The schematic diagram of our Arduino shield is
reasonably straightforward.
Sensor options
We use the analogue inputs of the Arduino module in this project,
which means we can use simple sensors, such as an LDR to measure
light intensity. Another interesting application is measuring rela-
tive humidity. You can make a simple sensor for this by embedding
two nails about half a centimetre apart in plaster of Paris inside a VCC
length of plastic pipe. Connect one of the nails to +5 V and the other
to an analogue input pin and to ground via a 10-kΩ resistor (see
ANALOG IN
Figure 3).
A lot of other quantities can be measured using the digital inputs.
The number of ports can be expanded with an I2C bus device, such R1
10k
EtherShield modules (Figure 5). Plug the EtherShield module onto Firmware
the Arduino gateway module first, and then plug the XBee shield This brings us to the firmware, or as it is called, the sketches. The
module on top so its pins and switches are readily accessible. Set sketch that runs in the node (see the program code in the download
both switches in the USB position when you want to program the package [2]) is the simplest. Briefly, it waits for a certain time, takes
Arduino module, and set them to the XBee position when you a snapshot of the analogue inputs, and sends it via a serial link to the
want to transmit or receive data with the XBee module. If you only XBee module. The following description is based on the program
want to receive data from the XBee module, set the RX switch listing with line numbers provided in a separate document.
to the XBee position and the TX switch to the USB position. This
allows you to display IDE debug information on the serial port con- The time interval for reading and forwarding the measurement data
sole of the Arduino module. It may be convenient to fit the board is specified in line 10. Here it is set to 5 seconds, which is the short-
with headers with jumpers instead of switches so you can open the est allowable interval for sending data to Pachube with a non-pro-
TX signal path between the Arduino and XBee modules, which is fessional account. Line 11 assigns a unique name to the node. This
handy for debugging. name is not used at present, but you could use it in the gateway to
XBee modules must be properly configured in order to communi- distinguish between different nodes. In the setup routine, the trans-
cate with each other. They are supplied with a factory default con- mission rate is set to 9600 baud in line 42 (the standard value for the
figuration for operation in serial line emulation mode. You can con- XBee modules). Furthermore, serial communication is configured to
figure them by using the terminal emulator utility in a Windows, 8 bits, no parity and 1 stop bit without any additional parameters.
Minicom or Linux environment to send them Hayes AT commands, The loop routine is fairly simple. It calls the intervalPassed routine,
or by using the X-CTU software supplied by MaxStream [3]. This does which determines whether the configured interval (5 seconds) has
not require any special hardware. Set both switches to the USB posi- expired. If it has, the measure routine is called. First we send the
tion and unplug the AVR microcontroller from the Arduino module. name of the node to the XBee module (lines 29 and 30), followed
Now you can use the USB link to communicate with the XBee mod- by a colon (:). The for loop in lines 32–35 polls all of the analogue
ule via the PC. ports and sends their values to the XBee module (line 32). Line 34
Before using this sketch, you must edit it to have the right
parameter values for your Pachube account and network. In
line 13, enter the ID of the feed you have created, and in line 14,
enter your API key (from My Profile > Settings at [1]).
Connect the gateway to a router or switch so it can access the
BANDWIDTH
20 MHz to 12 GHz
SAMPLING RATES
50 MS/s to 5 GS/s
MEMORY
Figure 4. The node for reading the sensors consists of an 8 kS to 1 GS
Arduino module and an XBee shield.
RESOLUTION
Internet directly. Configure the network parameters in lines 16
8 to 16 bits
to 18 by putting the MAC of the EtherShield module in line 16
and its IP address in line 17. A MAC address identifies a specific PRICES
device in an Ethernet network. You can use nearly any value you
want for the MAC as long as it does not cause a conflict with
£125 to £6995
other devices connected to the router or switch. Choose an
unused IP address in the address space of your local area net- Latest Software Updates: I²C & CAN bus decoding,
work and enter it in line 17. Here you must replace the dots
in the IP address with commas, since the code in lines 16–18 mask limit testing, advanced triggers,
defines a set of arrays. The IP address of the Pachube site is spec- digital low pass filtering, rapid triggering
ified in line 18. In more elaborate software you could use the
Domain Name Service (DNS), which translates domain names
into IP addresses, but the approach used here is adequate for
our purposes.
www.picotech.com/scope2023
Line 20 allows you to set a debug flag that activates the _LOG
macro in line 23. This macro first reserves 256 bytes on the stack
82k
µC, and the output voltage is measured by one of the µC’s analogue IRF644
inputs. The driver adjusts the PWM signal according to the difference ANALOG_PIN 0
Vm R2 R3
between the output voltage measured and the voltage wanted. Arduino C1
*
22k
1k
We don’t have enough space here to go into details about how this 100u 35V
circuit works, but it’s worth mentioning a few points of special inter-
est. The small capacitor across the diode improves the efficiency of 090849 - 11
the circuit. The load is represented by R3. The components used
make it possible to supply over 1 A (current limited by the MSS1260T
683MLB inductor from Coilcraft), but maximum efficiency (89%) is grammed in assembler, allowing a maximum frequency of 62.5 kHz
at around 95 mA (at an output voltage of 10 V). To avoid damaging (the µC runs at 16 MHz). To sample the output voltage, a frequency
the controller’s analogue input (≤5 V), the output voltage may not of 100 Hz is acceptable, which means we can use Arduino’s standard
exceed 24 V. For higher voltages, the values of resistors R1 and R2 timers and analogue functions. The Arduino serial port is very handy
would need to be changed. — we can use it for sending the output voltage set point (5–24 V)
The MOSFET is driven by the µC, which is nothing but a little Arduino and for collecting certain information about the operation.
board. The Arduino’s default PWM signal frequency is around Thanks to the Arduino environment, it only took about half an hour
500 Hz — too low for this application, which needs a frequency at to program. You can download the software from [1].
least 100 times higher. So we can’t use the PWM functions offered (090894-I)
by Arduino. But that’s no problem, as the Arduino can also be pro- [1] www.elektor.com/090894
UV light box
By Gert Baars (The Netherlands) is not nearly as difficult as most people assume.
The required items are a housing with a sheet of glass, UV fluo-
Fabricating printed circuit boards is not something that every elec- rescent tubes and perhaps a timer. The latter is not really neces-
tronics hobbyist does for themselves. It is, however, not really that sary when a clock or watch is available. It therefore comes down to
difficult. The most important necessities are a PCB layout, a printer obtaining the UV fluorescent tubes with starters and ballast and a
(or perhaps a copier), a light box, photo-sensitive PCB material, housing with a sheet of glass.
chemicals and an etching tray. It is often the light box that presents There is a cheap solution for this. For the housing you can use a dis-
the most difficulties. carded sheet scanner with all the innards removed. The fluorescent
The PCB layout can be copied (using a copier) from an existing tubes could come from a face tanning machine, for example. In the
design or you can design it yourself using a computer and suitable 'prototype' made by the author the frame complete with the fluo-
software. The layout needs to be printed onto a transparency (make rescent tubes was cut out. This could then be placed in its entirety
sure that the transparency is suitable for use in laser or inkjet print- into the housing for the scanner. The accompanying starters and
ers). The necessary chemicals (sodium hydroxide for the develop- ballast fitted elsewhere in the old scanner housing and after con-
ing and iron(III)-chloride or copper-chloride for the etching) are not necting all the wires everything immediately worked as it should.
difficult to obtain. It is not necessary either to etch using a foam- Because the layout has to be in good contact with the PCB to pre-
etching box, but this does etch much quicker. Etching can simply vent shadowing, a sturdy piece of cardboard the same size as the
be done in a plastic tray, and etching with iron(III)-chloride will be a glass was attached to the inside of the lid of the scanner. It is neces-
little faster if the etchant is at about 40 °C. sary to apply some pressure on the lid during the exposure, but a
As already mentioned, exposing the PCB is often the most diffi- few books or fat catalogues on top of the lid is already sufficient. The
cult part. In the past, the PCB with the layout in an exposing frame exposure time for the best results with this construction appears to
would sometimes be placed in the bright sun and it is also possible be about 2-3 minutes.
to expose the PCB using UV lamps. To make a 'real' light box yourself (090088)
This tiny oscillator circuit, when coupled a superhet circuit: the oscillator output is frequency changes when a human hand
with the software running on the Arduino mixed with a fixed frequency generated by (or other electrically conductive object)
microcontroller board, has a huge range a further oscillator, arranged so that the dif- is brought near to the antenna. The addi-
of potential applications, allowing prox- ference frequency is in the audible range. tional capacitance of the hand affects the
imity-based control of any other circuit Our circuit has just one oscillator, never- frequency of the resonant circuit in the
or system. theless designed around the same princi- oscillator (Figure 1). It seems reasonable
The circuit has already found use in many ple as the Theremin. The oscillator is con- to call this circuit a ‘Theremin oscillator’.
installations and objects at the author’s nected to an antenna, and the oscillator’s As in the original Theremin, we convert
college. One example is where the device is the modulated high-frequency signal into
interfaced to the Max/MSP music and mul- the audible range; but rather than using a
timedia development environment, which is superhet converter, we do the job in soft-
capable of producing sounds that provide a ware on the Arduino board. Our ‘There-
pleasant contrast to the square waves that mino’ therefore replicates just the pitch
the Arduino can generate directly. 4.1MHz ± ∆f control part of the original instrument. It
Oscillator
would be possible to build a second Ther-
What makes a theremin a emin, suitably modified to provide a vol-
theremin? ume control, to emulate the analogue
The Elektor editorial team felt it was possible Theremin fully.
that calling this project a ‘Theremin’ might
be a little misleading. The musical instru- LC oscillator using a 74HC00
ment named after Russian inventor and The circuit shown in Figure 2 consists of
engineer Léon Theremin (born Lev Termen) an amplifier, made to oscillate by coupling
consists of two antennas, each connected its output signal back to its input. At the
to an oscillator. One oscillator controls the input to the amplifier is a parallel resonant
081163 - 11
pitch of the instrument and the other the circuit, made from a coil and a capacitor,
volume. The original Theremin is a purely which determines the frequency of the
analogue device, with the conversion from Figure 1. The parallel capacitance of a oscillator. Any extra parallel capacitance
the modulated high-frequency signal to human hand near to the oscillator affects due to the connected antenna will affect
an audible frequency being performed by its frequency. this frequency.
L1 R1 R2
The oscillator is followed by the two remain- C1 100k 100k
ing gates of the 74HC00, used to square up GND1
IC1 = 74HC00N
its output waveform into a TTL-compatible 150p
NP0 10MH
signal. This signal is suitable for connection GND2
to a digital input on the Arduino board.
081163 - 12
Arduino software
For our tests in the Elektor lab we used an
Arduino Diecimila board [1] with software
downloaded from the author’s project web-
site. In principle any Arduino board could be Figure 3. The oscillator can be built on a small piece of prototyping board.
//******************************************************************
// Timer2 Interrupt Service is invoked by hardware Timer2 every 2ms = 500 Hz
// 16Mhz / 256 / 125 / 500 Hz
// gate time generation for freq. measurement takes place here:
ISR(TIMER2_COMPA_vect) {
}
i_tics++; // count number of interrupt events
if (TIFR1 & 1) { // if Timer/Counter 1 overflow flag
mlt++; // count number of Counter1 overflows
sbi(TIFR1,TOV1); // clear Timer/Counter 1 overflow flag
}
quency counter is realised in the Arduino tion, called every two milliseconds under The frequency shifts and calibration values
firmware using Timer1 as a counter and control of Timer2 (see Listing 1). are output on the serial port for further
Timer2 as a timebase. When suitably con- processing or for viewing using a terminal
figured by the software, the counter incre- At the end of the gate period a global flag program on a PC.
ments by one for each pulse on the input variable is set. This signals to the main code
pin, typically over four million times per sec- (see Listing 2) that a new result is ready. A rudimentary DDS tone generator function
ond. Timer2 provides a gate time of exactly Since we are only interested in relative is implemented in software to produce an
1/10 s (100 ms). With an input frequency of changes in the input frequency and not its audible frequency on port B. The signal can
4.1 MHz Timer1 will increment 410 000 times absolute value, we subtract the first meas- be heard by connecting a piezo transducer
during the gate period, and the frequency ured frequency after power-up from each or small loudspeaker to digital pin 8 (PB0,
can be measured to a resolution of 10 Hz. reading. If after a preset number of read- or pin 14 of the ATmega168) via a 1 kΩ series
This precision is required in order to detect ings the frequency shift is less than a certain resistor. You can see and hear the circuit in
the relatively small frequency shifts caused threshold value, an automatic calibration is action in a video on the author’s project
by the Theremin effect. performed: this compensates for the effect website [4].
of long-term oscillator drift. The two par- (081163-I)
Timer1 is only a 16-bit counter and so it will ameters (number of readings and threshold
overflow several times in each gate period. value) can be adjusted to suit a particular
The overflows are counted and combined application. The calculated frequency shift
with the counter value to produce a final value (variable ‘tune’ in the listing) can be
result at the end of the gate period. All the used as a starting point for your own appli-
timing work is handled by an interrupt func- cation ideas.
f_meter_start();
tune=tune+1;
while (f_ready==0) { // wait for period end (100ms) using interrupt
PORTB=((dds+=tune) >> 15); // kind of DDS tone generator: connect speaker to portb.0 = Arduino pin8
}
tune = freq_in-freq_zero;
// use the tune value here for your own purposes like control of servos, midi etc.
// startup
if (cnt==10) {
freq_zero=freq_in;
freq_cal=freq_in;
cal_max=0;
Serial.print(“** START **”);
}
// automatic calibration
if (cnt % 20 == 0) { // try automatic calibration after n cycles
Serial.print(“*”);
if (cal_max <= 2) {
freq_zero=freq_in;
Serial.print(“ calibration”);
}
freq_cal=freq_in;
cal_max=0;
Serial.println(“”);
}
cal = freq_in-freq_cal;
if ( cal < 0) cal*=-1; // absolute value
if (cal > cal_max) cal_max=cal;
}
Internet Links
[1] www.arduino.cc/en/Main/
ArduinoBoardDiecimila
[2] www.atmel.com/dyn/resources/
prod_documents/doc2545.pdf
[3] www.elektor.com/080931
[4] http://interface.khm.de/index.php/lab/
experiments/theremin-as-a-capacitive-
sensing-device/
Most of the projects published in Elektor simply provide a solution to a problem or fulfil a need. Even
though their designers have often tried to propose an elegant circuit or design an attractive PCB, the
aesthetic effect of the project itself, if there is one, usually takes second place. One exception is the
Monome open source project, a USB luminous matrix keyboard that was designed to be good-looking.
Here, it’s the application that seems to have been relegated to second place.
Let’s make it clear right from the start: only produced in small runs and their
Main Specifications the project shown here is not espe- prices are high (expect to pay €/£ 375
cially good-looking. It was inspired or $500 for a basic Monome) — espe-
• 4x4 Monome shield for Arduino by the Monome project [2], which is cially when you think that they are
attractive. Even if the design of a genu- nothing more than simple USB keypads
• Open source hardware ine Monome may not to your taste, it that don’t even work with MS Word!
• 16 push buttons has to be admitted that the aesthetic
aspect of the project is very important The aim of the project described here
• 16 LEDs
— you only have to visit the website to is to offer everyone the opportunity to
• TLC5940NT LED controller see for yourself. produce their own Monome for a cost
• 12-bit LED brightness control Exclusiveness seems to be one of the that is derisory compared with the
project’s other goals. Monomes are price of a ‘real’ one. What’s more, our
222
26 Personal Download for Petar Crnojevic | copyright Elektor elektor - 10/2009
Monome can be used for any other pur-
pose, since the software, like the hard-
ware, is open and you can modify eve-
rything. The brightness of each LED
is 12-bit adjustable, thanks to the LED 16x
driver, and we can dream up all man-
ner of fine, luminous objects. USB
ARDUINO PC
But just what is a Monome exactly?
A Monome is a rather special USB key-
pad. First of all, the keys, positioned 4x4 SHIELD
on a grid (usually square, though not
090527 - 11
necessarily so), do not have predefined
functions, and are all identical. In addi-
tion, each key is fitted with an indica-
Figure 1. Block diagram of our Monome. Here, ‘Shield’ doesn’t mean screening, but indicates an extension card for Arduino.
tor that makes it light up.
223
10/2009 - elektor Personal Download for Petar Crnojevic | copyright Elektor 27
PROJECTS ARDUINO & MONOME
26
S1 S2 S3 S4
5V
21
17
AN5 VCC LD1 LD2 LD3 LD4
18 27 28
AN4 3V3 OUT0
19 19 1
AN3 DCPROG OUT1
S5 S6 S7 S8 20 2
AN2 OUT2
21 27 3
AN1 VPROG OUT3
23 22 24
VIN AN0 XLAT LD5 LD6 LD7 LD8
28
IC2
16 4
RESET XERR OUT4
5
S9 S10 S11 S12 OUT5
ARDUINO 6
9 18 OUT6
D7 GSCLK 7
1 10 23 OUT7
AREF D6/PWM BLANK
2 11 LD9 LD10 LD11 LD12
AGND D5/PWM 8
3 12 26 OUT8
S13 S14 S15 S16 D13 D4 SIN 9
4 13 17 OUT9
D12 D3/PWM SOUT 10
14 25 OUT10
D2 SCLK 11
15 OUT11
D1/TX
16 TLC5940NT LD13 LD14 LD15 LD16
D0/RX 12
8 OUT12
D8 20 13
7 IREF OUT13
D9/PWM 14
6 OUT14
D10/PWM R1 15
5 C1 OUT15
D11/PWM GND
GND1 GND2 3k3 22
24 25 100n
090527 - 12
Figure 2. Monome circuit diagram. The Arduino board is based on an ATmega8 or ATmega168, as the PWM function is not used.
for less than €/£ 35. What’s more, the controller pins. Broadly speaking, the
project can be used for other things, as interface is divided into two parts, one
it is just a simple display keypad. for the data transfer and the other for
refreshing the LEDs. This interface can
be optimised by combining certain sig-
Let’s get down to business! nals, but this has not been done here.
We are basing our design on an LED If you are thinking of experimenting
Arduino board [3][4], which is inex- with this device, you may like to know
pensive and easy to program, but you that it lets you adjust the maximum
can use a different controller board if S current individually for each output,
you prefer. The only thing that really a nice option that could cause you to
matters is the USB interface, which waste a lot of time. If you can no longer
090527 - 13
absolutely has to be a variant of the get one or more LEDs to light up, or if
FT232R chip from FTDI, otherwise the the brightness levels are no longer the
communication software wouldn’t be Figure 3. How to construct a touch LED using an LED, a same, it could be that you have inad-
able to detect your Monome. Because keyswitch, and a little bit of patience. vertently modified the parameters of
our Monome uses Arduino, it one or more outputs. In this
belongs to the family of Ardui- case, disconnect the power to
nomes, which is no pointless reinitialize the device — just
distinction, since it requires resetting isn’t enough.
special software. R1 sets the maximum cur-
You’ll find the block diagram of rent for all the outputs, you
the project in Figure 1 and the can select this to increase or
circuit in Figure 2. As you can reduce the brightness of the
see, it’s quite simple, thanks Monome overall.
to Arduino and the LED driver
IC1. This IC contains 16 current
sources for driving 16 LEDs.
So, these keys?
Each output is controlled by We’re going to construct the
12-bit PWM (pulse width mod- Monome keys using 10 mm
ulation), offering 4,096 bright- LEDs and miniature push-but-
ness levels per LED. This tons (Figures 3 and 4). The
device has a slightly special idea is to use the LEDs to
serial interface, not very well press the push-buttons. The
explained in the data sheet, LEDs are large enough to hide
but which only requires six Figure 4. Building the prototype. the push-buttons, and seen
224
28 Personal Download for Petar Crnojevic | copyright Elektor elektor - 10/2009
from above, only the LEDs are visible.
To obtain vertical-action keys, the LED COMPONENT LIST
leadouts have to be bent in such a way
as to obtain a sort of ‘shock-absorber’. Resistors
R1 = 3.3kΩ
Then the push-button is slipped into
the shock-absorber and the LED + but- Capacitors
ton assembly is fitted to the board. C1 = 100nF
Concerning the LEDs, before you start
making the keys, check that all the Semiconductors
D1–D16 = LED, 10mm diam.
LEDS have the same brightness for IC1 = TLC5940NT
a given current. It can vary from one
LED to another, especially with cheap Miscellaneous
ones. Don’t skip this step, since once S1–S16 = miniature pushbutton,
6x6 mm, e.g. Omron type B3W-1000
the keys have been fitted, it’s not easy
2 pcs 6-way SIL pinheader, 0.1”
to remove them again. (2.54mm) lead pitch
2 pcs 8-way SIL pinheader, 0.1”
(2.54mm) lead pitch
Construction PCB # 090527-1 [1]
Arduino board
A PCB has been designed for this
project [1], see Figure 5. In keeping
with the ‘Open Source’ philosophy,
you’ll find on the web page for this Figure 5. Component side of the PCB.
project the complete Eagle file (circuit
and PCB) which you can modify as you
wish. Take care not to route any tracks
beneath the push-buttons on the com- imum) and then starts all over again. have opted for automatic detection of
ponent side, as the LED leadouts This loop lasts for around 40 seconds the Monome by interrogating the USB
already pass through this space. or so. If you press on an LED, the peripherals; it’s not possible to do this
Start by fitting R1, C1, and IC1. Then brightness of this LED is reset and it manually, which is a bit of a pity.
fit the keys, starting with the ones in restarts its loop. In this way, you can In order to be recognized as a Mon-
the centre of the grid. Fit the push- create some hypnotic sequences. The ome, the peripheral must have a serial
buttons carefully horizontal, and try to first reception of a Monome command number in the format a40h-xxx (xxx is
keep a small space between the LED exits the demonstration mode. For the for you to choose). We adopted a40h-
leadouts and the board to improve the moment, your circuit still isn’t a real 001. Refer to the box for details about
vertical movement of the keys. This job Monome, so it is unable to receive programming the FTDI chip.
requires a bit of patience and accuracy Monome commands — so let’s carry
to end up with a satisfactory result. on getting it ready.
Finish the wiring with the pin head-
On the computer side...
ers that have to be fitted on the solder As explained above, the FTDI chip also To finish off our Monome, or rather
side of the board. has to be programmed. You may not the operational testing, there are two
know it, but this device is programma- more pieces of software to be installed
ble thanks to a small EEPROM mem- on the computer (Figure 6). The first
Initial testing ory. FTDI provides the MProg tool for piece of software, Arduinome Serial [6],
Two components have to be pro- doing this [5]. This is necessary in is used to translate the Monome com-
grammed before the Monome will order to make our Monome work with munication protocol into MIDI (Musi-
work: the microcontroller and, surprise, the driver on the computer. In their cal Instrument Digital Interface [7], a
the USB interface! The microcontroller efforts to make it as simple as possible language dating from the 80’s, mainly
is programmed like an Arduino, since to use a Monome, the driver’s writers used for synthesizers) or into OSC
it is an Arduino, and this can be done
from the Arduino environment [4], but
if you prefer, you can flash the micro-
controller directly with the HEX file. PC
You’ll find all the source codes and the
HEX file at [1]. Arduinome Serial
If you’ve programmed your circuit with 16x
the software available in the article’s monome App.
monome USB 40h prot.
web page, your Monome now has a 10h
demonstration mode that lets us see if (4 x 4) MIDI / ex. MAX/MSP
the board is working properly. Restart OSC
the circuit and watch the LEDs. You’ll
see first of all that all the LEDs light
up briefly: flash! Then the program 090527 - 14
goes into a loop which progressively
increases the LED brightness from zero Figure 6. Arduinome Serial translates the data sent by the Monome into MIDI or Open Sound Control (OSC). The translated messages
up to a certain maximum (not the max- are sent to the Max/MSP application via the computer’s internal network.
225
10/2009 - elektor Personal Download for Petar Crnojevic | copyright Elektor 29
PROJECTS ARDUINO & MONOME
(Open Sound Control [8], a language package from [1] and unzip it onto your
that is more recent, more powerful, computer’s hard disk somewhere.
and more flexible than MIDI). Ardui-
nome Serial has to be used for Mono-
mes based on Arduino or, to be more
Final testing
precise, Monomes that use the FT232R Connect the Monome to the computer
chip for their USB interface. (For the via a USB cable and run Arduinome
others, there is Monome Serial.) Serial. If all is well, the software will
The second piece of software to install find the Monome and display the serial
is Max/MSP [9] (also see inset). This number you’ve just programmed for it
software is a powerful graphical pro- (Figure 7). There’s no need to mod-
gramming environment for music, ify the parameters for our tests, the
audio, and multimedia which is used default values will do.
for developing multimedia patches. Now run Max/MSP and load the Mon-
The part of the environment that runs ome_test.mxb patch, in the ‘Monome
the patches (the runtime) is free, which base’ package (don’t take any notice
lets you run them on any Mac- or Win- of the messages about matrixctrl). A
Here’s what a PureData patch looks like for breaking down, modifying, and re-synthesising an audio signal using wavelets.
The rectangles in the ‘program’ part are objects that contain sub-programs,
the lines between the rectangles are the data flows.
Other music programming languages, albeit without nice graphical interfaces, are, for ex-
ample, Csound (www.csounds.com) and ChucK (http://chuck.cs.princeton.edu/).
226
30 Personal Download for Petar Crnojevic | copyright Elektor elektor - 10/2009
to communicate with each other. let you do a test that’s a bit more fun. you to modify the quite simple Mon-
Then, in the Monome_test window, Open the file and click the focus prefix ome software to sort everything out,
click the button next to the word ‘pair- button in the Monome_midi_64 win- as the options provided by Arduinome
ing’, just below the ‘keypads’ grid, and dow to change the Arduinome Serial Serial aren’t enough. Now it’s over to
select ‘press’. If you click somewhere in Address Pattern Prefix to </midi>. If you...
the ‘keypads’ grid, the corresponding you press an LED, you’ll hear a sound (090527-I)
box in the ‘lights’ grid changes colour. and you’ll be able to play a tune. You
Reboot the Monome. This is neces- can change the sound (the default is
sary because for some reason the piano) and the note values by clicking
Internet Links
automatic detection of the Monome and moving the mouse around in the [1] www.elektor.com/090527
by Arduinome Serial makes the serial matrix. [2] www.monome.org
link crash. Press the LEDs and you’ll
[3] www.arduino.cc
see the corresponding boxes in the You’ll probably have noticed that the
‘keypads’ grid light up. If you have boxes in the ‘keypads’ grid that light [4] www.elektor.com/080931
selected ‘press’, the LEDs will also up when you press the LEDs are some- [5] www.ftdichip.com/Resources/Utilities.htm
light up. If you release an LED, it will where in the middle of the grid, and [6] www.sourceforge.net/project/show-
go out. If you have selected ‘toggle’, the orientation is not the same as on files.php?group_id=235473&package_
the first press lights the LED, pressing the Monome. This is due to the Mon- id=285957
a second time will extinguish it. You ome software, which doesn’t com-
[7] www.midi.org/
can also use the mouse to click in the pletely follow the convention. Ardui-
‘lights’ grid to light up or extinguish nome Serial has a ‘Cable Orientation’ [8] www.opensoundcontrol.org/
the LEDs without pressing any keys. box that lets you choose between [9] www.cycling74.com
If all this is working, you Monome is ‘up’, ‘down’, ‘left’, and ‘right’ — see [10] www.Monome.org/data/app/base
operational (at last!) Figure 6. This box is used to orient
the Monome with respect to its USB
cable. So if you’re holding the Mon-
Just to finish off... ome in your hand with the USB cable
If your computer has a sound card, coming out downwards, you’ll need
the Monome_midi_64.mxb patch will to choose the ‘down’ option. It’s up to
Advertisement
227
10/2009 - elektor Personal Download for Petar Crnojevic | copyright Elektor 31
PROJECTS MICROCONTROLLERS
Microcontrollers fo
for
… Arduino for the enlightened
Clemens Valens (Elektor France Editorial)
Apparently Arduino is an Italian name – but when you search on the Internet, you mainly
find dozens of references relating to electronics and programming. What’s more, these
references are often in relation to art. Electronics and art – now there’s an interesting
subject that’s worth delving into! So just what exactly is Arduino?
At first sight, Arduino [1] is a small microcontroller board to certain functions, since this allows the Arduino develop-
with a USB port (Figure 1) that comes in several models. ment environment to be used for writing and compiling
There are even ‘daisy’-shaped boards (Lilypads) intended application before loading them onto the controller.
for wearable applications, i.e. to be incorporated into gar- The applications, called ‘sketches’, are written in a lan-
ments. The Arduino board is programmed in a language guage that closely resembles C. Hardly surprising – it is
very similar to C using Open Source tools available for C, but with some additional functions. All of the functions
Windows, Mac, and Linux. The hardware is also Open and presented as the language for Arduino form a Hardware
anyone can make their own Arduino – the circuit diagrams Abstraction Layer that lets you program the controller with-
and PCB photo masks are available free over the Internet. out needing to delve into the innards of the processor. The
Arduinos are used a great deal by artists who need elec- language has everything you need for most applications.
tronics in their creations. Broadly speaking, there are functions for digital and ana-
logue inputs/outputs, a few basic mathematical functions,
time management functions (delays) and a few function for
serial port communication – asynchronous (UART) and syn-
chronous (SPI).
The digital I/O functions let you manipulate the logic levels
of the pins, to read and write them. There is also a special
function that makes it possible to measure the duration of
a pulse. Using the analogue I/O functions, it is possible to
read voltages and generate PWM signals. Lots of appli-
cations don’t require anything more than this, and this is
exactly where Arduino’s strength lies. There’s no need to
go ferreting around in the registers and the controller data
sheet to make a PWM output or a counter work — the ‘dirty
Figure 1. work’ has already been done.
A Diecimila Arduino board.
The new Duemilanove If these functions aren’t enough, it’s perfectly possible to pro-
board is almost identical. gram on a lower level and, just as in standard C, you can
These boards are cheap also add libraries with their own functions. But do watch
and easy to find. out – if you go off into the darker depths of the Arduino
programming language, you risk losing compatibility with
the rest of Arduino community.
When you look at it a bit more closely, an Arduino is not
exactly a microcontroller board. In fact, an Arduino is quite The Arduino community? Already, Arduino is a microcon-
simply an 8-bit microcontroller from Atmel – an ATmega8 troller, as well as a development environment and a pro-
for the earliest Arduinos and now more likely to be an gramming language – now it’s a community too? Yes! In
ATmega168. This microcontroller is loaded with a ‘boot- fact, Arduino is more of a philosophy, the aim of which is
loader’ program that lets you load an application into the to popularize technology to make it accessible to artists.
controller via a serial port, without overwriting the boot- Arduino is a logical sequel to Processing [2] and Wiring [3]
loader. Since modern computers no longer have serial projects. Processing is a multimedia programming language
ports, a USB port is often used. All this becomes an Arduino and Wiring is a development environment for artistic elec-
when you decide to dedicate certain pins of the controller tronics But now we’re starting to get away from our original
228
32 Personal Download for Petar Crnojevic | copyright Elektor elektor - 2/2009
r Dummies …
point; refer to the box about the origins of Arduino if the name Freeduino for home-made
you want to find out more. Arduino boards. But after all, it’s only
a name, so let’s call ours Elektorino.
Elektorino
Simple, free programming is something we’re interested in. Implementation
What’s more, the electronics involved seem to be simple Before you can load a sketch into the Elektorino, you need
– so what could be more logical than to produce our own to load the bootloader. This is where things get more com-
Arduino-compatible system? Well, that’s just what we’re plicated, as there are two official Arduino bootloaders, the
going to do! only apparent difference between them being the way the
Our starting point is the basic Arduino Serial board. The sketch is run after loading. This is achieved by way of a
office computer I use all the time still has a serial port, but controller reset, which the Arduino environment can handle
for the unlucky owners of a computer that doesn’t, we’re if the board has been equipped for it, and if it has the right
going to use the USB-TTL cable [4]. In any event, we’re
going to be needing a TTL interface of some kind, as our
own Arduino will only have a TTL serial port.
J2 +5V +5V
1
Our processor is going to be an ATmega168, which we’ll 2 R9
3
R7 C3
even though the controller is perfectly capable of operat- 4
1k
5
ing at up to 20 MHz, the standard bootloader assumes a 6
1k
R8
100n
S1
moment we just want an Arduino board that works. J3 AREF VCC AVCC
J5
1 PIN0 2 1 RESET
To finish off our Arduino, all we need do is add an LED, a 2 PIN1 3
PD0 PC6
28 IN5 6
PD1 PC5
reset push-button, a few resistors and capacitors, and two 3 PIN2 4
PD2
IC1
PC4
27 IN4 5
connectors: one for the serial port and the other for pro- 4 PIN3 5
PD3 PC3
26 IN3 4
5 PIN4 6 25 IN2 3
gramming the bootloader. We need the latter to be able to 6 PIN5 11
PD4 PC2
24 IN1 2
PD5 PC1
program our Arduino for the first time, when the controller 7 PIN6 12
PD6 PC0
23 IN0 1
is still blank. Later, when the application is finished and the 8 PIN7 13
PD7 +5V
bootloader is no longer needed, this connector can be used 14
ATmega168
19 J1
PB0 PB5
for programming the controller directly from the application, J4
15
PB1 PB4
18 MISO 1 2
C1 16MHz C2
ure 2). Thanks to the simplicity of the circuit, we can build The circuit of the
22p 22p
it on prototyping board. D7 Elektorino. Not at
Unfortunately, we are not allowed to call our fine project all complicated. The
Arduino – only boards approved by the Arduino community connectors for the pins are
have the right to that name. This is why a second move- 080931 - 11
not necessary, they are
ment Freeduino [5] was created, which allows free use of mainly used as a reference.
229
2/2009 - elektor Personal Download for Petar Crnojevic | copyright Elektor 33
PROJECTS MICROCONTROLLERS
Hello world!
Once you have succeeded in loading the bootloader, it’s
time to see if Elektorino manages to communicate with the
Arduino environment and if it is possible to load a sketch.
So let’s install Arduino. I’ve only done it under Windows
XP, and that was extremely easy. All I had to do was down-
load a large zipped file and unzip it somewhere onto the
hard drive.
There are three commands that, broadly speaking, unlock the memory, load the program, set the fuses, and finally lock the me-
mory. Refer to the AVRDUDE instructions if you want to know exactly what is going on (sensitive souls are advised to refrain!). Loc-
king the memory is used to avoid overwriting the bootloader accidentally when loading a sketch.
A good website about the AVR is called Lady Ada [10].
230
34 Personal Download for Petar Crnojevic | copyright Elektor elektor - 2/2009
A real application
It’s all very well to have an Arduino development environ-
ment that works wonderfully well, but without a real appli-
cation, it’s not very interesting. I already had ten motorized
slider pots, and it was high time to put them to good use.
Why not with Elektorino? Elektorino has analogue inputs
and PWM outputs — everything we need to drive a motor.
So I’m going to suggest a driver for motorized faders. Note
that this circuit can be used with any ATmega168-based
Arduino board.
The fader in question (Figure 4) consists of a slider pot,
a small motor, and an assembly of a few rollers, springs,
and a piece of cord that enables the motor to move the
slider in both directions. This assembly allows the motor to
freewheel when the slider is unable to move – at each end Figure 4.
of its travel, for example. Apart from the 10K B marked on A motorized fader of
the fader, I didn’t have any technical data on it, but a few unknown make.
experiments showed that the motor turned at a suitable
speed when powered from 12 V. In this case, its consump-
tion was around 200 mA.
The B marked on the fader might lead us to think it’s a log +12V
IC2
7805 +5V +5V
model (as is often the case), but after checking, my faders
turned out to be linear ones. C4 C5
IN0
2k2
2k2
2k2
this circuit, states 00 and 11 are the same. A 5 V regulator T3 T4
the whole controller assembly and motor from 12 V. The BD139 M1 BD139
transistor are all NPN types, and those forming the bridge D1
M
D4
The sketch
Now that we have connected a motor driver to Elektorino
(Figure 6), it’s time to deal with the software. You’ll see,
the final sketch will be amazingly simple, thanks to the
power of the Arduino.
A basic sketch consists of two functions: setup() and loop(),
which are called by the layer of a lower level. In setup,
called once at runtime, we put everything that relates to ini-
tializing the system – for example, the inputs/outputs and
the serial port speed.
99.9% of embedded programs probably spend their whole
lives in a loop. This is why in Arduino this loop is already
implemented in the form of the loop function. This loop Figure 6.
function is called periodically and may be regarded as The Elektorino prototype:
Arduino’s main. It’s important to realize that, even though the ATmega168 is on the
it looks like a special function, loop is just like any other left and the double H
function in C. So its local variables are reset each time it is bridge to drive the motor
called and variables that are required to keep their values on the right.
between different occasions loop is called must be declared
globally (or as static, for those familiar with C.)
serial port to drive our circuit, and for this it needs to be
The setup in our sketch doesn’t contain anything very much. initialized. Thanks to the simplification offered by Arduino,
The Arduino pins are inputs by default, so only the two all we have to do is enter the communication speed – in
outputs need to be initialized. We’re going to be using the our case, 9,600 baud.
231
2/2009 - elektor Personal Download for Petar Crnojevic | copyright Elektor 35
PROJECTS MICROCONTROLLERS
Moving the pot wiper is done in loop. The principle is very may even begin to oscillate.
simple: if the voltage measured on the input pin is different To avoid these problems, we have used a Proportional Dif-
from the voltage required, the slider must be moved in the ferential (P-D) regulator. In this type of regulator, the system
direction which will reduce this difference. In real life, it’s reaches its final value without overshoot by continuously
a bit more complicated than that. To start with, there’s the adjusting the correction signal according to the difference
remaining to be corrected. So at the start of an adjustment,
when the error is greatest, a strong correction signal is
applied. Then, once the difference starts to reduce, the cor-
The STK200 compatible programmer used by the author. There are also simpler
rection signal reduces too and the system slows down.
programmers – it’s a matter of personal preference.
The correction signal consists of two parts: a signal propor-
D8
tional to the error (P) and a signal proportional to the error
20 C6
1N4148 reduction (D). With a properly adjusted system of this sort,
IC3 R12
the slider can be moved quickly without overshooting the
100k
100n
target value.
10
14
1 19
EN1
EN2
obtain the error to be corrected. From this value, we calcu-
15
2
3
late the two components P and D of the correction signal.
16
17
4 2 1D1 18 MOSI J7 The P component is the error multiplied by the constant Kp;
18
5
6
4
6
16
14
LED
SCK
1
3
2
4
the D component is obtained by multiplying the difference
19
20
7 8 12 5 6 between the current value and the previous error by the con-
8 11 9
21
9 13
1D2
7 RESET ISP stant Kd. The values for these constants were determined by
22
23
10
11
15
17
5
3
experimentation, and you can modify them to see how the
24
25
12 affect the adjustment. It’s highly instructive.
74HCT244
13
080931 - 13
The two components P and D are combined and the result
is adapted to the range of usable values. The pot slider
doesn’t move for values below 50, and the maximum value
for the PWM signal is 255.
Then we look to see if the error is small enough for us to be
problem of direction, but more significant still is the problem able to stop the motor. This comparison has to be performed
of inertia. Once the slider is moving, it takes a little time for for both slider directions. We leave a small margin for error,
it to come to a complete halt. So it’s easy to overshoot the since perfection is perhaps a little over-ambitious.
required position if braking occurs too late. In the event of When the error is small enough, we prevent further correc-
an overshoot, the slider has to be brought back, with the tions so as to free up the slider; we make the assumption
same risk of overshooting again, and so on. The system that the system is never going to overshoot the target value.
232
36 Personal Download for Petar Crnojevic | copyright Elektor elektor - 2/2009
In this way, it’s possible to move the slider manually, with-
out the system’s trying to move it back into place. (Who’s Listing 1 – Easy-peasy!
the strongest?)
Once the slider has been released, the system starts to out- void loop()
put the slider position periodically (10 Hz) via the serial {
port. The serial port input is also scanned and as soon as int error;
four characters have been received, they are transferred as int val;
a target value for the slider and the PD regulator is re-acti- int spd;
float spd_p, spd_d;
vated to move it to its new position. No format checking
is performed for the value received, the system requires an
// read wiper voltage.
ASCII four-digit value between 0000 and 1023. To mini- val = analogRead(slider);
mize errors, the target value obtained is limited between 3
and 1020, which minimizes problems of continuous activa- // Calculate error.
tion at the ends of the travel. error = val - target;
The serial port is not used while the motor is operating, as
this might produce interference, resulting in inaccurate posi- // Calculate proportional component P.
tions or even oscillation. I’ve not taken the trouble to find // Two directions – so use absolute value.
out why: I’ll leave that for you to do! spd_p = abs(error)*Kp;
(080931-I)
// Calculate differential component D.
spd_d = (last_error-error)*Kd;
last_error = error;
233
2/2009 - elektor Personal Download for Petar Crnojevic | copyright Elektor 37