Σάββατο 4 Απριλίου 2015

Intel Galileo: Linux, SSH, Static Addresses and Other Tips

I've recently acquired an Intel Galileo Gen 2 development board. Intel Galileo (just in case you haven't heard about it) is Intel's answer to the Arduino driven hobbyist community. In fact, Galileo is (or supposed to be) pin compatible with Arduino Uno: you can even use Uno shields on it. Galileo is programmed using the same IDE as the Arduino (albeit patched by Intel) and the sketches - once recompiled - are (supposedly) compatible: You can just move your LCD display circuit from your Uno to your Galileo, recompile and upload the program and it should work. Well, for the most part that is...



So why buy a Galileo instead of (or in addition to) a standard Uno or other AVR based Arduino? The Galileo is using an Intel SoC (System on-a Chip) running at no less than 400MHz. It is equivalent in power to some older Pentium CPU. There is more to it: There is 256MB of RAM on board, a micro-SD card slot, an Ethernet port and even a micro PCI-Express underneath for your WiFi card. Galileo is admittedly over-specified for an Arduino. And with good reason: Even without any SD inserted, Galileo runs a small Linux version inside it. You will notice it as it takes some good time to start and appear on the USB connection. And there is an even more complete Linux version available as an SD card image. Linux and Ethernet easily make this device an IoT (Internet of Things) development board.

How would you access the Linux part of Galileo? Let's examine two scenarios:
  1. Access the built-in Linux. This is what runs on the Galileo if you don't insert any SD card.
  2. Access the SD card Linux (Download it from here, along with any other utilities you need).

Accessing the Built-in Linux via Telnet

If you look at most of the Galileo tutorials you will find more than a few ways to access the Linux part of your Galileo:

  • Use a serial cable and a (custom made) connector for the serial out pins (gen 2) or 3.5mm jack (gen 1). This is easy to do but most PCs lack proper RS-232 these days and I find USB to serial as a last resort scenario.
  • Run a sketch to reconfigure the USB port as a serial terminal and use that for connecting. There are a couple of problems with this approach: although it will supposedly work on the built-in Linux, it stops accepting input after you connect through i.e. PuTTy. You will get a login prompt and a shell but that's about it. It won't execute any commands. It works a little better with the SD card version of Linux but there is another serious drawback: the sketch permanently reconfigures the USB port for serial connection and you lose the ability to upload sketches.
What if you really want to have both Linux *and* your sketches running at the same time?

It turns out that the built-in Linux does not have an ssh daemon, so we will have to use telnet. No big deal. And instead of going the serial route, let's try that nice Ethernet port. Ethernet is a lot more common these days: just plug your Galileo into your home switch then upload a sketch that looks like the following:

void setup() {
  system("ifconfig eth0 inet 192.168.0.10 netmask 255.255.255.0 up");
  system("telnetd");
}

void loop() {
}

Obviously, you will have to change 192.168.0.10 to an address that matches your home setup. If you are using DHCP (very common on home routers) just make sure you assign it a static address outside the scope of the DHCP server (I assume that as a good admin you've kept a couple of addresses from being automatically assigned, haven't you?)

You are now ready to telnet into your device! Use PuTTy from Windows or the command line telnet client from another Linux box. This is what you will get:



Login as 'root'. There is no password set.
Now that you are in, there is no need for the script to keep executing. Use the top command:

# top


Find the process id for /sketch/sketch.elf and just kill it (it usually is at the top of the list):

# kill 929

Or you could simply upload another sketch to execute. Unlike the USB serial approach, your upload ability is not affected in any way.

Play with the built-in Linux as long as you like: you will soon find out it is pretty limited in what it can do. And the moment you unplug your Galileo all the settings are lost: your sketches are erased and the nice static IP you've assigned is gone. You will have to rerun the above sketch after every reboot.

Sooner or later (probably sooner!) you will decide to migrate to the SD card version of Linux.

Accessing SD Card Linux via SSH and making settings permanent

So you've created an SD card image of Yocto Linux using Intel's instructions. And now you want to SSH into it.
  • First, be patient. The SD card Linux may take more time to boot than the built-in version. You will know it has booted successfully when the USB connects to your PC (if you use Windows, it will make a sound)
  • The SD card Linux provides an SSH service. Even better, you can make your IP address (and other settings) permanent.
  • You will have to consider security if you leave your Galileo running and connected to the Internet.
As you can imagine, the script to get SSH access is even easier than before:

void setup() {
  system("ifconfig eth0 inet 192.168.0.10 netmask 255.255.255.0 up");
}

void loop() {
}


The SSH server is already running on SD card Linux, no need to restart it. Just assign your static IP and you are good to go! You can now login and make your settings permanent.

Use top and kill (like we did before) to stop the sketch from running. It is no longer necessary.  You may as well remove it so it won't run at next boot. We will make the configuration changes permanent anyway:

# rm /sketch/sketch.elf

The first thing you will want to do is assign a root password:

# passwd
Changing password for root
Enter the new password (minimum of 5, maximum of 8 characters)
Please use a combination of upper and lower case letters and numbers.
New password:
Re-enter new password:
passwd: password changed.

Change the /etc/network/interfaces file to assign your static IP address permanently. (It seems the only available editor is vi...) Find the following line:

iface eth0 inet dhcp

and change it to:

iface eth0 inet static
   address 192.168.0.10
   netmask 255.255.255.0
   gateway 192.168.0.250

The gateway part is only needed if you intend to give Galileo Internet access. You will also need to reconfigure /etc/resolv.conf for this (we will do it after a few more steps). The address on the gateway part is of course your home router's IP.

The above setting is not enough though: Galileo uses the Network Connections Manager (conman) to configure the network interfaces and the above file is ignored in this case. We will have to revert to the older method:

# cd /etc/rc5.d
# rm S05conman
# ln -s ../init.d/networking S05networking

Now reboot your Galileo:

# shutdown -r now

When it comes up again, just connect via SSH. No need to rerun the sketch as the changes to the files are permanent.

If you wish to connect your Galileo to the Internet, add this line to /etc/resolv.conf:

nameserver 192.168.0.250

Where 192.168.0.250 should actually be your router address. Or another available DNS server (if in doubt, use Google's DNS: 8.8.8.8).  For this to work, you must also have a valid gateway line in /etc/network/interfaces.
While still logged in as root, you may wish to change the weird 'clanton' hostname to something more cool. Just edit the /etc/hostname file and replace the contents with the name of your choice. Hostname will change on the next reboot.
If you wish to have a message of the day (motd) appearing at every login, create an /etc/motd file with your desired contents.

Securing your Galileo Linux

At a minimum, you will want to take a few security measures if you decide to connect your Galileo to the Internet:
  • Give the root user a password. We've already done that.
  • Disallow root logins via SSH.
  • Create a standard user account for 'normal' use.
Creating a standard user acoount is easy:
# useradd -g root johndoe

(Johndoe is actually not a good choice for a username, but you get the idea!). It is best to make your user a member of the root group. The permissions on some devices (like /dev/null) are read-write for the root user and group only and will hinder your ability to use some commands like scp unless your account belongs to the root group. Give your new user a password:

# passwd johndoe
Changing password for johndoe
Enter the new password (minimum of 5, maximum of 8 characters) Please use a combination of upper and lower case letters and numbers.
New password:
Re-enter new password:
passwd: password changed.

This is a good time to check whether the new account works. Just open a new SSH connection and try to use the new user instead of root to connect. If it all works, continue by disabling the root login via SSH:
Edit /etc/ssh/sshd_config. Find the line that shows

PermitRootLogin yes

And change yes to no:

PermitRootLogin no

Or, alternatively, just comment out the entire line. The default setting for SSH is to not allow root logins.
Just reboot your Galileo and you are good to go:

# shutdown -r now

From now on, you will use your new user account to connect via ssh. You can always use:
$ su -

to switch to root when needed.
Sudo would have been a nice addition to this Linux version, but it is not available by default and I haven't researched package management yet!
You could create some fancy bash startup files for your account. You may also use my version (a slightly simplified version of what I use on my FreeBSD machines). Just login as the user you created and:

$ wget http://www.freebsdworld.gr/files/galileo-dot.tar.gz
$ tar xvzf galileo-dot.tar.gz

You may need to press 'A' to overwrite an existing file. Logout and login again to apply the changes.

Getting an LCD Screen to Work with Galileo

LCD screens are very popular with Arduino 'users' as they add a whole new dimension to projects. The 16 character, 2 line variant seems to be the most common and it is the one I currently have.
Since Galileo is Arduino Uno compatible, all you would have to do is move your LCD circuit from your Uno to Galileo (to the same GPIO pins), recompile your program for Intel and upload it. Well, supposedly.
Because I followed these exact steps and got just a blank LCD staring at me. Apparently you need to update the LCD Driver libraries with the ones found here.
After unzipping the file, replace all the contents of the libraries/LiquidCrystal folder (in your Arduino installation folder) with the contents of the archive.

Rebuild and upload your project - your screen should now be working!


Happy coding!


Παρασκευή 31 Οκτωβρίου 2014

Calculating Filters with Your TI

I've developed a small set of programs to calculate and graph simple first order low pass or high pass RC filters. These run on TI 89 or 92/92Plus / Voyage 200. I may convert them for the TI 84 Plus C too, though I think this calc is mostly used by high school students rather than engineers. (Although some of us have a not so modest collection of scientific/graphing calcs!)

I will not repeat the theory of RC filters here, you can find some excellent articles on the web, such as:

Download the set of programs here:


Quick Walk-through of the Program

Unzip the downloaded file and use TI Connect to send it to your calc. The program creates a folder called electron for its own use, so it won't mess with any of your variables in main. It will also attempt to backup any settings it changes during startup and restore them on exit.
To execute the program:





The program will then automatically setfold to electron where the other subprograms are stored. Next, the main menu is shown:




Design allows you to enter a frequency and either a resistor or capacitor. It will then solve for the other component. Analyze will perform a calculation of a filter based on the values of both components. Components and resistors may be selected from the standard E12 values or be of any other manually entered value.

Let's follow a path through the "Design Filter" option:




Using the first option, you enter a cut-off frequency and a resistor. Second option for entering Frequency and capacitor. Going ahead with the first option:




Entering a frequency in Hz (1000Hz for our sample calculation)




Select a resistor. Use option 2 for non-standard values. Going ahead with option 1, you will be presented with a dialog to select a value. A matching dialog is used for capacitors as well.




We have selected 1000Ohms for the resistor. The program will give us the first results:




The calculated capacitor is 159 nF. The program then asks for two values which are used to create tables and graphs. The frequency step here is used for the phase shift and gain tables that follow.




The last frequency corresponds to the last value shown on the phase shift / gain graphs. Up to this point, the calculations for both Low Pass and High Pass filters are the same.




Before entering the tables/graphs section we must actually select the type of filter we are designing. Let's assume a low pass filter.




In this table, x represents frequency (using the step we entered previously). The column labeled "1" is the phase shift (notice -45 degrees in the -3db point for the LPF) and "2" is the gain (-3db=0.707). You can move around the table using the arrows and explore the values. When ready, just press enter.




You now have the option to see the table results as a series of graphs or just exit to the same menu. Assuming the first option, we get the phase shift graph:




After graphing is complete, the trace cursor appears and you may move through the graph and note the phase shift value for each frequency. You may also directly type the desired frequency. When ready, press enter for the next graph.




Again, use the same techniques to explore the Gain graph. When ready, press Enter to return to the main menu.

If the program crashes for some reason (or if you break out of it without using the Exit option) some modes on your calculator may be changed. To return your calculator to the state it was before running the program, type the following commands in the home screen:

setfold(electron)
rclGDB gdb
initg(0)
setmode("14",modevar)
setfold(main)

If you happen to have misplaced your TI, you can always run these programs in an emulator. Tiemu is an excellent one and works in all major operating systems.

I hope you find this useful!

Τρίτη 16 Σεπτεμβρίου 2014

MSP430/ARM Development on Linux: Installing CCS 6.0

Code Composer Studio (or CCS as it is widely known), is Texas Instruments' own development tool for their series of MCUs like the well known MSP430 and the Stellaris/Tiva (ARM based) series. Texas Instruments also provides a number of inexpensive development / demo boards known as launchpads. I am the happy owner of two of them: the MSP430 one (F5529) and the ARM based Stellaris launchpad (LM4F120).

Texas Instruments provides some instructions on installing CCS 6.0 on Linux. We will provide additional instructions for installing Tivaware (or stellarisware).

Basic Install

Install your favorite *buntu variant. I've chosen Xubuntu 14.04 since it is lightweight enough and the speed is tolerable on my tiny 2009 Acer Netbook. (While I have beefier machines for development, the netbook is very convenient as it fits perfectly on my rather small electronics workbench).
Make sure to install the updates, either during installation or immediately afterwards by running:

sudo apt-get update; sudo apt-get upgrade

Downloading CCS6.0

Download CCS6.0 for Linux from this page:


It is preferable to download the full version rather than the web based installer.

Installing Dependencies

Before running the installation program, some dependencies need to be installed. In general the instructions in TI's wiki apply:

sudo apt-get install libc6:i386 libx11-6:i386 libasound2:i386 libatk1.0-0:i386 libcairo2:i386 libcups2:i386 libdbus-glib-1-2:i386 libgconf-2-4:i386 libgdk-pixbuf2.0-0:i386 libgtk-3-0:i386 libice6:i386 libncurses5:i386 libsm6:i386 liborbit2:i386 libudev1:i386 libusb-0.1-4:i386 libstdc++6:i386 libxt6:i386 libxtst6:i386 libgnomeui-0:i386 libusb-1.0-0-dev:i386 libcanberra-gtk-module:i386

Some of these are already installed, apt-get will inform you about this.

Note the wiki refers to the 64bit version, but CCS runs without any problem in Ubuntu 32bit too (as you may have noticed, all the above dependencies are 32bits anyway). Create the required symbolic link:

sudo ln -s /lib/i386-linux-gnu/libudev.so.1 /lib/libudev.so.0

Running the Installation

Change to your Download folder (or wherever you placed the downloaded CCS6.0 archive):

cd ~/Downloads

Extract the files:

tar xvzf CCS6.0.1.00040_linux.tar.gz

(replace with actual filename of downloaded file, may differ if a new CCS version is released)

Change to the folder of the extracted files and run the installer:

cd CCS6.0.1.00040_linux

./ccs_setup_6.0.1.00040.bin


There is no need to run the installer as root (with sudo), unless you wish to install it for multiple users. Otherwise, just run it as a standard user and install CCS in a subdirectory of your home directory. After accepting the license, you will be prompted to select an installation directory. Assuming your username is 'user', install CCS to /home/user/ti (the location is automatically suggested by the installer).

You will then be greeted by the processor support dialog:



It is wise to select all the MCUs that you intend developing for. For this example we selected MSP and Tiva/Stellaris development:



You won't have to change the default emulators selected in the next dialog. In the App Center dialog, select at least the MSP430Ware:



Installing the Drivers

After the CCS installation is complete and before plugging in your USB launchpad, install the necessary drivers:

cd ~/ti/ccsv6/install_scripts

sudo ./install_drivers.sh

Running CCS for the First Time

The first time you run CCS, you will be asked to select a workspace (and maybe set it as default). We have chosen /home/user/tidev here but you are welcome to choose your own or accept the default. Make a note of this as you will need it later.



On the first run the App Center will automatically download any options you selected during install (like the MSP430ware) and will also update other components. If you only intend to develop for MSP430, your setup is now complete. For Stellaris/Tiva, read on.

Installing Tivaware (Stellarisware)

Developing for tiva or stellaris requires the Tivaware library and some additional settings in CCS. Note that you can use Tivaware to develop for stellaris (LM4F devices) and there is no need to install stellarisware:

Download Tivaware from TI:

Tivaware download page

If for some reason you prefer stellarisware (for example, developing for LM3S devices):


Tivaware is provided as an EXE file, but is actually a self extracting ZIP. Unzip to a subdirectory of your CCS6.0 install path:

cd ~/ti
mkdir tivaware
cd tivaware
unzip ~/Downloads/SW-TM4C-2.1.0.12573.exe


(the actual filename may vary)

There are several ways to include tivaware in your projects. In order to minimize required settings on each project, create a vars.ini file in your workspace. Remember this is /home/user/tidev in our example:

cd ~/tidev
vi vars.ini


(obviously, use your favorite editor instead of vi to create this file)

A single line is needed:

TIVAWARE_INSTALL=/home/user/ti/tivaware

 

Configuring Your Project for Tivaware

The vars.ini file creates an environment variable for the /home/user/tidev workspace (where the file is saved). To configure your project to use tivaware successfully:

  • Import the vars.ini file as source for CCS Build variables
  • Add an "include files" path to the compiler using the TIVAWARE_INSTALL variable
  • Add the driverlib.lib file to the project.

Go ahead and create a new project (File => New => CCS Project). Use the following screenshot as a guide:




When finished, select File => Import => Code Composer Studio => Build Variables:


Select the vars.ini file previously created:




Next, right click on your project name in the project explorer. Select properties, ARM Compiler, Include Options and add the following directory path:



Finally, add (actually, link) driverlib.lib to your project. Right click on your project name in the project explorer and select add files:




The full path for driverlib.lib as shown: /usr/ti/tivaware/driverlib/ccs/Debug. On the next dialog, select link to file:


We are done! Here is our BlinkTheLed project (from the Tiva Workshop workbook), successfully built:

 
Since the drivers for the in-circuit debug interface are installed as well, you can actually connect your launchpad and run a debug session on the device.
And now you can continue running your development environment on your lean and mean Linux machine. Happy coding!

Τετάρτη 10 Σεπτεμβρίου 2014

Καθρέπτες Ρεύματος - Πηγή Ρεύματος Widlar

Καθρέπτες Ρεύματος

Σχεδόν όπου και να ψάξετε στη βιβλιογραφία, θα βρείτε το παρακάτω χαρακτηριστικό κύκλωμα για τον καθρέπτη ρεύματος:



όπου στις επαφές P1 / P2  θα συνδέσετε το φορτίο σας. Το ρεύμα στο φορτίο θα είναι ακριβώς το ίδιο με το ρεύμα που διατρέχει το συλλέκτη του Q1. Γι'αυτό το λόγο άλλωστε το κύκλωμα ονομάζεται καθρέπτης ρεύματος.



Το transistor Q1 είναι συνδεδεμένο ως δίοδος (diode transistor) και έτσι ισχύει:




Υπολογίζοντας το ρεύμα που διέρχεται μέσα από την R1 (και θεωρώντας αμελητέο το ρεύμα βάσης):




Αν τα transistor είναι ακριβώς ίδια, το ρεύμα βάσης που θα κυκλοφορεί και στα δύο θα είναι ακριβώς το ίδιο. Αυτό φυσικά θα έχει  σαν αποτέλεσμα το ρεύμα συλλέκτη στο Q2 να είναι ακριβώς το ίδιο με το ρεύμα Ic του Q1 που υπολογίσαμε πριν.

Η μαγική φράση εδώ είναι "ακριβώς τα ίδια transistor". Όσο και αν ψάξετε δεν θα βρείτε δύο ακριβώς ίδια transistor. Μια μικρή διαφορά στο β και το παραπάνω κύκλωμα θα σας δίνει πολύ διαφορετικό ρεύμα. Ακριβώς ίδια transistor μπορείτε να βρείτε μέσα σε ένα ολοκληρωμένο κύκλωμα όπου και το παραπάνω μπορεί πράγματι να έχει πρακτική εφαρμογή. Μια και παίζουμε με διακριτά εξαρτήματα, χρειαζόμαστε κάτι πιο πρακτικό.

Ένας πιο Πρακτικός Καθρέπτης

Μπορούμε φυσικά να χρησιμοποιήσουμε το παλιό καλό κόλπο με την αντίσταση στον εκπομπό: θα δημιουργήσουμε έτσι μια αρνητική ανάδραση που θα σταθεροποιήσει το ρεύμα συλλέκτη, σχεδόν ανεξάρτητα από το β των transistors. Για παράδειγμα, ας δούμε το παρακάτω κύκλωμα:




Οι αντιστάσεις Re είναι ίδιες για τα Q1 και Q2. Όπως και πριν μπορούμε να υπολογίσουμε το ρεύμα συλλέκτη του Q1. Για να έχουμε ένα πιο πρακτικό παράδειγμα, ας θεωρήσουμε ότι έχουμε τάση τροφοδοσίας 9V και θέλουμε να έχουμε ρεύμα στο φορτίο 10 mA. Μπορούμε εύκολα να γράψουμε για το Q1 το παρακάτω (αγνοώντας ξανά επιδεικτικά το ρεύμα βάσης):




Επιλύοντας για Vcc =9V, Ic=10 mA και Vce=0.7V, θα πάρουμε:




Μπορούμε να επιλέξουμε Rc = 680 Ω, Re = 150 Ω. Βολικές τιμές, καθώς ανήκουν και οι δύο στην Ε12. Προσέξτε ότι ενώ υπάρχουν πολλοί πιθανοί συνδυασμοί, δεν θέλουμε να μεγαλώσουμε υπερβολικά την τιμή της Re γιατί μεγαλώνουμε την αντίσταση εξόδου του κυκλώματος. Σκοπός μας είναι απλά να σταθεροποιήσουμε το ρεύμα.

Μπορείτε να χρησιμοποιήσετε το παραπάνω κύκλωμα ως ένα πολύ ωραίο LED tester: 10 mA είναι συνήθως κατάλληλη τιμή για τα περισσότερα LED. Ότι χρώμα LED και να βάλετε για φορτίο, ο καθρέπτης μας θα του δώσει ακριβώς αυτό το ρεύμα. Για την τροφοδοσία μπορείτε να χρησιμοποιήσετε μια μπαταρία 9V. Μη ξεχνάτε όμως να την αποσυνδέετε όταν τελειώσετε: ακόμα και χωρίς φορτίο, το κύκλωμα μας διαρέεται από 10mA.

Καθρέπτης για Μικρά Ρεύματα

Από την άλλη, ίσως δεν θέλετε να φτιάξετε ένα LED tester αλλά ένα transistor tester. Ξέρετε, σαν εκείνα τα άχρηστα που έχουν τα πολύμετρα πάνω και μετράνε το β (hfe). Ξέρετε φυσικά ότι το β δεν είναι σταθερό: μεταβάλλεται π.χ. με την τάση συλλέκτη εκπομπού, το ρεύμα, τη θερμοκρασία κλπ. Άρα η μέτρηση του με ένα απλό κύκλωμα, σε ένα μοναδικό σημείο λειτουργίας έχει μόνο ακαδημαικό θα λέγαμε ενδιαφέρον. Ωστόσο μπορεί να αποτελέσει ένα ευχάριστο απόγευμα αν έχετε πολλά transistors στα συρταράκια σας :)

Για ένα τέτοιο κύκλωμα θα θέλετε να φτιάξετε το παραπάνω ώστε να παρέχει ρεύμα περίπου 10 μΑ. Αν κάνετε τους υπολογισμούς όπως προηγουμένως, θα καταλήξετε σε κάπως... μεγάλες αντιστάσεις:


Όπως είπαμε πριν, οι πηγές ρεύματος βρίσκουν αρκετές εφαρμογές στα ολοκληρωμένα κυκλώματα. Όμως μεγάλες τιμές αντιστάσεων δεν είναι δυνατόν να κατασκευαστούν (είναι είτε ασύμφορες ή απλά πολύ μεγάλες σε μέγεθος για να χωρέσουν) στα σύγχρονα ολοκληρωμένα. Αν πρόκειται λοιπόν για μικρά ρεύματα, μπορούμε να πάμε στην πηγή ρεύματος Widlar.

Πηγή Ρεύματος Widlar

Η πηγή ρεύματος Widlar μας επιτρέπει να παρέχουμε μικρό ρεύμα εξόδου με λογικές τιμές αντιστάσεων. Δεν πρόκειται ακριβώς για "καθρέπτη" αλλά μάλλον για φακό: το ρεύμα στο transistor Q1 είναι μεγαλύτερο από το Q2, ή αν προτιμάτε, το ρεύμα του Q2 είναι ένα μέρος (κλάσμα) του Q1. Το κύκλωμα είναι το παρακάτω:



Μοιάζει με το προηγούμενο μας κύκλωμα, μόνο που η Re υπάρχει μόνο στο κύκλωμα εξόδου. Αυτό προκαλεί και την ασυμμετρία του κυκλώματος και την διαφορά των ρευμάτων Q1 και Q2.

Ας υποθέσουμε ότι θέλουμε ρεύμα εξόδου 10 μΑ. Για ρεύμα εισόδου θα επιλέξουμε ένα αρκετά μεγαλύτερο, π.χ. 3 mA.  Για το Q1 μπορούμε και πάλι να γράψουμε:




Ίσως σας παραξενέψει λίγο ο τύπος για την Re:




Προφανώς Iq1 και Iq2 είναι τα ρεύματα συλλέκτη για τα transistors Q1 και Q2 αντίστοιχα. Αν αναρρωτιέστε πως προκύπτει ο τύπος, θα σας παραπέμψω στο ισοδύναμο Ebers-Moll που απαιτείται για τη συγκεκριμένη ανάλυση.

Κάνοντας ένα γρήγορο υπολογισμό:


Vt είναι η θερμική τάση (thermal voltage) και μπορείτε να θεωρήσετε ότι για τα περισσότερα transistors μικρού σήματος είναι 26mV σε συνήθη θερμοκρασία δωματίου. Μπορούμε να επιλέξουμε αντιστάσεις R1=2.7kΩ και Re=15kΩ. Στην Re καλό είναι να προσθέσουμε ένα trimmer για να ρυθμίσουμε το ρεύμα Iq2 ακριβώς στα 10 μΑ. Για να μετράμε NPN transistors, μπορούμε να φτιάξουμε το παρακάτω κύκλωμα:


(Για μέτρηση PNP transistors φτιάξτε το αντίστοιχο κύκλωμα με Q1, Q2 να είναι NPN) Στη θέση των Q1, Q2 μπορείτε να βάλετε όποιο γενικής χρήσης PNP θέλετε (π.χ. BC560C). Q3 είναι το NPN transistor που θα μετρήσετε. "Α" είναι το πολύμετρο σας ως μιλι-αμπερόμετρο στη κλίμακα των mA. Η τιμή που θα μετρήσετε, χωρίς την υποδιαστολή, είναι το β του transistor. (π.χ. αν μετρήσετε ρεύμα 2.35 mA, το transistor έχει β 235).

Για σωστή μέτρηση, βαθμονομήστε το κύκλωμα ως εξής: αφαιρέστε το Q3 και βάλτε το μικρο-αμπερόμετρο απευθείας στο συλλέκτη του Q2 προς γη. Χρησιμοποιήστε το trimmer ώστε το ρεύμα που μετράτε να είναι ακριβώς 10μA. 

Η αντίσταση 1 kΩ στο συλλέκτη του Q3 μπαίνει για προστασία από τυχόν βραχυκύκλωμα.  Καθώς το transistor λειτουργεί στην ενεργή περιοχή το ρεύμα συλλέκτη θα είναι πάντα β φορές το ρεύμα βάσης. Η αντίσταση φυσικά επηρεάζει τη μέτρηση καθώς κρατά κάποια τάση στα άκρα της και ξέρουμε ότι το β μεταβάλλεται με την τάση Vce.

Αλλάζοντας τιμές στην αντίσταση αυτή θα διαπιστώσετε και γιατί η μέτρηση β δεν έχει και τόσο νόημα.

Εξακολουθεί πάντως να επαρκεί για ένα ευχάριστο απόγευμα :)

Σάββατο 30 Αυγούστου 2014

Microcontrollers and LEDs

The (somewhat lengthy) Introduction

Arduino is breeding a new generation of hackers! Those of you old enough to remember the 80s will probably have fond memories of programming a home computer of that era: the ZX Spectrum, the Commodore 64, the TI-99 and all the other wonderful machines that wouldn't do much unless you could use their BASIC language (or learn machine code!) Sure, you could get ready made programs and games on cassette(!) but there were many who would buy them just for the fun of programming. The hardware had a lot of limitations, sure, but the mere joy of squeezing every single bit of power from the machine was unsurpassed.

Computers have evolved a lot since then, but also lost a lot: the interactiveness of the first computer languages, the simplicity of programming, the single point of entry: you would start learning BASIC. What about now? Where does a beginner start programming? Sure, there are many choices, in both programming languages and operating systems. Most of them are free. But here is the problem: the choice is too big. If that is not enough, the changes are rapid and most people won't be able to follow the latest trend or hype: first it's C++, then it's Java, tomorrow is Python and so on. All very nice,  but where does a beginner start? Confusion...

There are other problems too: your first (simple) programs will not be very impressive by today's standards. They will probably be command line and text driven (mind you, I've written a book on learning programming by writting games in Python and Pygame but it's in Greek only for the moment I'm afraid. You can get a PDF copy of it here). People get bored quickly writing the same old computational programs in modern PCs full of colors, sounds and multimedia. Trust me, I teach these subjects. Programming is now mostly viewed like a chore (we are teaching it the wrong way).

Microcontrollers to the rescue! Actually, they share a lot more than you think with the home computers of the 80s.

  • They are "underpowered" (by todays CPU standards)
  • Their programming is simple and has immediate (and spectacular) results
  • They are actually made to be "actively hacked" rather than "passively used"
They also combine tinkering with both hardware and software,  making them suitable for both computing and electronic engineering hackers!

Most people that are comfortable with programming don't necessarily have adequate knowledge of electronics: some basic stuff is required even at the beginner level: how do you connect a LED to a microcontroller output? Let's see.

Connecting a LED to a Microcontroller Output

You have to keep the following things in mind:
  • Some microcontrollers have 5V outputs (arduino), others have 3.3V or less (MSP430)
  • Some microcontrollers can provide enough current (in other words, they have adequately low output resistance) to drive a LED directly (with a suitable resistor). Arduino can drive LEDs with no problems.
  • Other microcontrollers like MSP430 have limited output current capability. You might be able to drive a LED from them directly but at risk of damaging the MCU.
  • No LED can operate directly from an MCU output: you will need a to connect a resistor in series. Typical LEDs operate at low voltages, depending on their color. For example, red leds need about 1.5V. Fancier colors like blue and white need higher voltages.
Assuming you are using a microcontroller like Arduino, all you need to connect your LED to an output is a suitable current limiting resistor.

Connecting a LED to an Arduino

Connecting a LED to an arduino  is very simple, since the outputs of the MCU can source enough current by themselves. With the following facts in mind it is easy to calculate the resistor:
  • An "active" arduino output provides 5V
  • A typical red LED needs 1.5V
  • About 10mA through the LED are usually sufficient for a good brightness.
Our circuit looks like this:


 Calculating R1 is simple:



We know Vout is 5V, Vled is 1.5V and we need 10mA for the LED. Solving for R1:


 Feel free to adjust this a bit up or down to use a value actually available in the E12 series of resistors (either 330Ω or 390Ω will do). Use this same calculation for other LEDs, adjusting for current and LED color (voltage drop) accordingly.

Connecting a LED to an MSP430 (or other low power MCU)

Low power MCU cannot (and should not) drive LEDs directly. Instead, use the output from the MCU to switch on a transistor that carries the load:


Although we show a separate 5V supply for the LED, if you are using a TI launchpad board, you can source this directly from its 5V output pin.

We now have two resistors to calculate, R1 and R2.

Starting with R2, the equation for the output part of our circuit is:





We will be operating our transistor as a switch. In this configuration the transistor is used in two states: cut off (where no current flows) or fully on (saturation) where the transistor shows almost no resistance (the collector-emitter voltage is zero).

In an ideal transistor (which exists only in... an ideal world) the Vcesat would be zero, but in practice it will be around 0.2V.  We still need about 10 mA for the LED. Solving the above equation for R2 yields:


Conveniently, this is an E12 value as well :)
You may adjust a bit up or down if you have happen to not have the exact value.

Since we know the collector current wil be 10mA, we can easily calculate the base current and resistor R1. Assuming the transistor has b (hfe) 300:



The equation for the input:


Solving for R1:

 

69K is not a value of the E12 series but feel free to replace it with either 68K (little more current) or even 56K (if your transistor has a lower hfe).

In any case, you will only be drawing a minute and absolutely safe amount of current from the MCU output: around 30 μΑ!