Elektorusb
Elektorusb
This data
acquisition card
for connection to the USB
has eight digital outputs, eight digital inputs, two 10-bit
analogue outputs and eight 10-bit analogue inputs for
voltage swings of 0 to 5 V. The system’s core is a Microchip
USB-savvy microcontroller type PIC18F4550 programmed in C. The
circuit is built on a compact PCB and requires no external power supply.
Measurement cards and systems you the PC recognise a valid USB device!
can connect to a PC have been a con- In this article hopefully we cater for DIP40 device which should
stant success factor in the long histo- readers only interested in digital and an- attract applause and other
ry of Elektor. Whether it’s stand-alone alogue connectivity with the real world, expressions of approval from
for control over the RS232 or LPT ports as well as for those with a deeper inter- the I-hate-SMDs camp.
(anyone remember those?), as a plug- est in how USB actually works on a mi-
in card for the ISA bus (ditto) or now, crocontroller and, equally important, can
recently, for the USB, it’s a blockbuster be made to do something really useful
The circuit
if our readers can (1) generate and read — all at very low cost, of course. The circuit diagram of this small
digital control signals, and (2) do the wonder of technology is given
same for analogue signals! The card in Figure 1. It’s not much more
described in this article could be at the
PIC 18F4550 for USB than a powerful CPU (IC1) sur-
hub of a great many applications to do Fortunately, there are microcontrollers rounded by input and output
with measurement and control. that make the USB interface between connectors and a few status
the PC (the host) and the circuit we LEDs. The function of the con-
wish to design (the device) more or less nectors is as follows, with the
We want USB transparent. That’s because they are relevant PIC lines in brackets:
Arguably, RS232, ISA and even Cen- provided with dedicated hardware and
tronics are things of the past when it software to implement USB ‘the easy K1 = 8-bit digital output for 0-
comes to digital and analogue signals way’. All totally invisible of course to 5 V TTL swing (RD0-RD7).
specifically for measurement and con- those who just want to use the USB de- K2 = USB connector for linking
trol by/on a PC. USB is the way forward vice yet know nothing about it (which to your PC (RC4-RC5).
both in terms of speed and ease of con- should not include you)! One such K3 = 8-bit digital input for 0-5 V
nection, although the latter is a com- processor is Microchip’s PIC 18F4550, TTL swing (RB0-RB7).
plex matter especially as far as soft- which has the additional advantage of K4 = two analogue outputs for
ware is concerned. For example, a lot lots of (free) software being available 0-5 V swing (RC1-RC2).
of thought (and time) goes into making for it. Also, the device is available as a K5 = 8 analogue inputs (AN0/
16 elektor - 11/2007
on Card digital & analogue;
input & output
+5V +5V
R1
C1 C2
10K
R2 100n 470n
470R
S1
11
32
18
C3
VDD
VDD
VUSB
100n K1 1 6
MCLR RA4
RD0 1 2 RD1
RD2 3 4 RD3 RD0 19 2 AN0
RD0 AN0/RA0 K5
RD4 5 6 RD5 RD1 20 3 AN1
+5V RD1 AN1/RA1
RD6 7 8 RD7 RD2 21 4 AN2 AN0 1 2 AN1
RD2 AN2/RA2
L1 9 10 RD3 22 5 AN3 AN2 3 4 AN3
RD3 AN3/RA3
+5V RD4 27 7 AN4 AN4 5 6 AN5
RD5 28
RD4 IC1 AN4/RA5
8 AN5 AN6 7 8 AN7
* CCP1 1 2 CCP2 RD6 29
RD5 AN5/RE0
9 AN6 9 10
RD6 AN6/RE1
3 4 RD7 30 10 AN7 +5V
C4 RD7 AN7/RE2
5 6
PIC18F4550
10n 7 8 15 33 RB0
RC0 RB0 K3
9 10 CCP2 16 34 RB1
RC1 RB1
K2 CCP1 17 35 RB2 RB0 1 2 RB1
+5V RC2 RB2
1 R3 K4 36 RB3 RB2 3 4 RB3
RB3
2 27R 23 37 RB4 RB4 5 6 RB5
RC4 RB4
3 R4 24 38 RB5 RB6 7 8 RB7
RC5 RB5
4 27R 25 39 RB6 9 10
RC6 RB6
26 40 RB7 +5V
RC7 RB7
OSC1
OSC2
5
6
VSS
VSS
LED0
LED1
+5V
12
13
14
31
R5
R6 R7 R8
1M
X1
* see text
1k
1k
1k
D1 D2 D3
C5 20MHz C6
22p 22p
070148 - 11
Figure 1. Circuit diagram of the data acquisition card with USB connectivity.
RA0-AN7/RE2) for 0-5 V swing. the aid of a PLL and a frequency di- by the author using two free software
visor from the 20 MHz supplied by tools from Microchip: IDE MPLAB V7.5
Internal pull-up resistors are available the quartz crystal. The frequency of and C18 Student Edition V3.02. The
on RB, the digital input port lines. The 48 MHz is an exact multiple of the USB Microchip website has instructions for
analogue outputs have a resolution of bus speed (full speed, 12 Mbits/s). installation and use of both programs.
10 bits each using PWM (pulsewidth The source code of the firmware is dif-
modulation) at 2.9 kHz. If necessary Two status LEDs, D1 and D2, indi- ferent from the Microchip original. All
these outputs can be filtered with a cate the USB status. D3 is obviously project software is available as a free
simple RC network. The DC output the supply power indicator that lights download # 070148-11.zip from our
voltage Vo obtained after the filtering when the card is connected to the USB website at www.elektor.com. You will
may be calculated from: port on your PC. find at least three folders in the archive
The circuit’s supply voltage arrives via file: ‘driver’, ‘firmware’ and ‘PC’. The
Vo = 5D [volts] USB connector K2 and a small choke, content is an Aladdin’s Cave for fans
L1, to suppress noise, with C4 assist- of C, PICs and USB (and that should
where variable D is the duty cycle of ing to that effect. cover a lot of our readers!). A piece of
the PWM, taking a value between 0 That effectively leaves components S1, C code is shown in Figure 2; it’s these
and 1. R1, R2 and C3 at the MCLR input of the PIC fuse settings you’ll need to know
The analogue inputs also have a reso- micro. Well it’s just another wholly tra- if you’re not buying the chip ready-pro-
lution of 10 bits. ditional Reset network. grammed from Elektor.
The firmware file contains the whole
The oscillator in the PIC micro ticks at project and the result of its compilation
20 MHz using quartz crystal X1 and
PIC Firmware called TAD_v1.hex. The microcontroller
the usual pair of small capacitors for Where there’s simple hardware, there’s must be programmed with this file.
the parallel loading, and a high-value a massive amount of software behind it Those of you interested in the deep-
resistor (R5) for the feedback. all and usually lurking inside microcon- er workings of USB should know that
Actually the microcontroller runs at trollers. The firmware (object code) the the connectivity implemented on the
48 MHz, generated internally with PIC is faithfully executing was created card described on this article is de-
11/2007 - elektor 17
projects data acquisition
18 elektor - 11/2007
as PID the one of a demo card of the
PIC18F4550 Microchip. If the USB Data
Acquisition Card is used for commer-
cial purposes, it is essential to obtain
K3
a different VID/PID set of numbers—
this can be done, for example, through
R1
C3
R2
www.usb.org or through Microchip.
S1
This new combination should be in-
cluded in the source code of firmware
K5
that would be compiled again in or-
K1
der to obtain an updated .hex file
with which the microcontroller is
C1 R5
C5
programmed.
X1
The PC software would also have to
C6
be modified in the same way, since
R6
firmware and software must have the
D2 R7
IC1 K4
same VID/PID. Finally, the driver ‘mch-
C2
R4
R3
pusb.inf’ file would be modified.
D1
D3
Precautions
C4
K2
R8
L1
Some general precautions must be
mentioned. All expansion connectors
K1, K3, K4 and K5 include +5 V and
ground to power any cards that can be
connected to them. Great care must be Figure 3. Top side and bottom side component placement for the PCB. The PCB artwork is a free pdf download from our website but
taken to prevent short circuiting these bear in mind that the board is double-sided and through-plated.
terminals and not drawing more than
100 mA from any of them. Also, remem-
ber that these terminals are directly — avoid using family or kiddies PCs in ternal power supply, joining only appli-
connected to the +5 V and ground of any case). cation and USB Data Acquisition Card
the USB port of your computer (and If you need more current for a certain grounds.
you do not want that to take damage application, consider the use of an ex- Some precautions for the digital inputs
COMPONENTS LIST
Resistors
(all SMD 0805 case)
R1 = 10kΩ
R2 = 470Ω
R3,R4 = 33Ω
R5 = 1MΩ
R6,R7,R8 = 1kΩ
Capacitors
(all SMD 0805 case)
C1,C3 = 100nF
C2 = 470nF
C4 = 10nF
C5,C6 = 22pF
Semiconductors
IC1 = PIC18F4550 I/P, programmed, Ele-
ktor Shop # 070148-41
D1,D2,D3 = LED, SMD case 1206
Miscellaneous
K1,K3,K4,K5 = 10-way boxheader
K2 = type-B USB connector, PCB mount
X1 = 20MHz quartz crystal
L1 = VK200 or small ferrite bead with 2-
4 turns thin enamelled copper wire
S1 = pushbutton, PCB mount, 6mm
footprint
DIL40 socket for IC1
PCB (bare), Elektor Shop # 070148-1
Project software, file # 070148-11, free
download from www.elektor.com.
11/2007 - elektor 19
projects data acquisition
Files & file locations If you wish to modify the project to adapt it to your requirements it is
necessary to install the Visual compiler Microsoft C++ 2005 Express
and update it with Service Pack 1: Visual C++ 2005 Express SP1.
The complete project of the application for the PC is in PC\ TAD_V1_
Later we will install Microsoft Platform SDK for Microsoft Visual C++
win\ folder and its name is TAD_V1_win.vcproj. The compiled pro-
2005 Express. This serves to develop WIN32 applications, necessary
gram is in PC\TAD_V1_win\Release folder and its name is TAD_V1_
in this case to access the DLL mpusbapi.dll). All of it is free and can be
win.exe (for the program to work, the dynamic link library mpusbapi.
downloaded from the Microsoft website. There you will also find in-
dll created by Microchip must be in that same folder). The executable
structions about installation and examples.
one needs the .NET Framework. It is highly recommended to have the
operating system updated by means of Windows Update.
(K3): Do not apply voltages below zero in a comfortable manner. This card is
or higher than 5 volts to avoid damage
to the PIC microcontroller.
isolated using optotriacs.
2. Resistor-to-voltage converter card
About the
On the digital outputs (K1): each line
can supply a maximum current of
supplying a voltage proportional to the
input resistor. This voltage is applied
author
25 mA for logic High or Low levels. to the digital input. The author is a telecommunications tech-
On the analogue inputs (K5); the same 3. Voltmeter card with LED bar nical engineer working as a teacher of
as with the digital inputs. readout. electronics in a professional school in
On the analogue outputs (K4): each 4. Speed control of a DC motor. This Madrid since 1984. He is a keen electron-
ics enthusiast. He developed this card to
line can supply a maximum current of card controls the speed and direc-
enable his students to control small robots
25 mA for logic High and Low levels of tion of a motor through the analogue from the USB port in a PC by programmes
the PWM signal. outputs. written in C code.
Finally, the sum of all currents of all the 5. Driver card for stepper motors, capa-
digital and analogue outputs must not ble of microstepping through the dig-
exceed 200 mA. ital outputs.
6. Distance sensors card using the ana-
logue inputs.
Work in progress… 7. Relay card under control of the dig-
The USB data acquisition data card has ital outputs.
a lot of potential and the author has de-
veloped, and is busy developing, the Elektor and the author welcome other
following application cards: applications you may have developed.
Let us know!
1. Triac card for the 8 digital outputs, to (070148-I)
be able to control mains-powered loads
20 elektor - 11/2007