Saturday, 14 November 2009

The H-bridge

Basic Theory-

An H-bridge is an electronic circuit which enables a voltage to be applied across a load in either direction. These circuits are often used in robotics snd other applications to allow DC motors to run forward and backwards. H-bridges are available as intergrated circuits, or can be built from discrete components.
H-bridge, sometimes called a full "full bridge" the H-bridge is so named because it has four switching elements at the "corners" of the H and the motor forms the cross bar. Here is a figure showing a basic gridge.


Structure of an H-bridge (highlighted in red)

An H-bridge is built with four switches (solid-state or mechanical). When the switches S1 and S4 are closed (and S2 and S3 are open) a positive voltage will be applied across the motor. By opening S1 and S4 switches and closing S2 and S3 switches, this voltage is reversed, allowing reverse operation of the motor.

The switches S1 and S2 should never be closed at the same time becayse this will cause a short circuit on the input voltage supply course. the same applies to the S3 and S4. This condition is known as shoot through.

Operation-
The H-Brifge arrangement is generally used to reverse the polarity of the motor, but can also be used to brake the motor, where the motor comes to a sudden stop, as the motors terminals are shortened, or to let the motor 'free run' to a stop, as the motor is effectively disconnected from the circuit.  The following table summarises operation.

S1     S2     S3     S4          Result
1        0        0      1     Motor moves right
0        1        1      0     Motor moves left
0        0        0      0     Motor free runs
0        1        0      1     Motor brakes
1        0        1      0     Motor brakes

Thats all about H-bridges.  Now hopefully I will be able to soon hack the RC car!!!

Monday, 2 November 2009

Research on how to hack a RC car - Transistor Circuits and Relays

This post will explain all about transistor circuits; types of transistors, the current in them, different uses and the Darlington pair.
First, I'm going to look at some of basics of transistors.

Types of Transistors:
There are two types of standard transistors, NPN and PNP, with different circuit symbols.  The letters refer to the layers of the semiconductor material used to make the transistor.
A Darlington pair is two transistors connected together to give a very high current gain.


Transistor currents:
The diagram shows the two current paths through a transistor.
The small base current controls the larger collector current.
When the switch is closed a small current flows into that base (B) of the transistor.  It is just enough to make the LED B glow dimly.  The transistor amplifies this small current to allow a larger current to flow through its collector (C) to its emitter (E).  This collector current is large enough to make the LED C light brightly.
When the switch is open no base current flows, so the transistor switches off the collector current. Both LEDs are off.
A transistor amplifies current and can be used as a switch.



Functional model of an NPN transistor:
  • The base-emitter junction behaves like a diode.
  • A base current I.B flows only when the voltage V.BE across the base-emitter junction is 0.7V or more.
  • The small base current I.B controls the large collector current Ic.
  • Ic = h.FE * I.B (unless the transistor is full on and saturated) h.FE is the current gain (strictly the DC current gain), a typical value for h.FE is 100 (it has no units because it is a ratio).
  • The collector-emitter resistance R.CE is controlled by the base current I.B:
         I.B = 0                 R.CE = infinity           transistor  off
         I.B small               R.CE  reduced          transistor partly on
         I.B increased        R.CE = 0                  transistor full on (saturated)



  • A resistor is often needed in series with the base connection to limit the base current I.B and prevent the transistor being damaged.
  • Transistors have a maximum collector current Ic rating.
  • A transistor that is full on (with R.CE = 0) is said to be saturated.
  • When a transistor is saturated the collector-emitter voltage V.CE is reduced to almost 0V.
  • The emitter current I.E = I.c + I.B, but I.c is much larger than I.B, so roughly I.E = I.c
Darlington Pair:
This is two transistors connected together so that the current amplified by the first is amplified further by the second transistor.  The overall current gain is equal to the individual gains multiplied together:
Darlington Pair current gain    -      h.FE = h.FE1   *   h.FE2
(h.FE1 and h.FE2 are the gains of the individual transistors)
This gives the Darlington pair a very high current gain, such as 10000, so that only a tiny base current is required to make the pair switch on.
A Darlington pair behaves like a single transistor with a very high current gain.  It has three leads (B, C and E) which are equivalent to the leads of a standard individual transistor.  To turn on there must be 0.7V across both the base-emitter junctions which are connected in series inside the Darlington pair, therefore it requires 1,4V to turn on.
A Darlington pair is sufficiently sensitive to respond to the small current passed by your skin and it can be used to make a touch-switch as shown in the diagram.  For this circuit which just lights a LED the two transistors can be any general purpose low power transistors.  The 100k Ohm resistor protects the transistors if the contacts are linked with a piece of wire.

 Using a transistor as a switch:
When a transistor is used as a switch it must be either OFF or fully ON.  In the fully ON state the voltage V.CE across the transistor is almost zero and the transistor is said to be saturated because it cannot pass anymore collector current Ic.  The output device switched by the transistor is usually called the 'load'.

 The power development in a switching transistor is very small:
  • In the OFF state: power = Ic * V.CE, but Ic = 0, so the power is zero.
  • In the full ON state: power = Ic * V.CE, but V.CE = (almost), so the power is very small.
This means that the transistor should not become hot in use and you should not need to consider its maximum power rating.

Protection diode:
If the load is a motor, relay or solenoid (or any other device with a coil) a diode must be connected across the load to protect the transistor from the brief high voltage produced when the load is switched off.
The picture shows how a protection diode is connected 'backwards' across the load.



Important:
Current flowing through a coil creates a magnetic field which collapses suddenly when the current is switched off.  The sudden collapse of the magnetic field induces a brief high voltage across the coil which is very likely to damage transistors or IC's.  The protection diode allows the induced voltage to drive a grief current through the coil (and diode) so the magnetic field dies away quickly rather than instantly.  This prevents the induced voltage becoming high enough to cause damage to transistors or IC's.

 Advantages of relays:
  • Relays can switch AC and DC, transistors can only switch DC.
  • Relays can switch high voltages, transistors cannot.
  • Relays are a better choice for switching large currents (>5A).
  • Relay can switch many contacts at once.
Disadvantages of relays:
  • Relays are bulkier than transistors for switching small currents.
  • Relays cannot switch rapidly, transistors can switch many times per second.
  • Relays use more power due to the current flowing through the coil.
  • Relays require more current than many IC's can provide, so a low power transistor may be needed to switch the current for the relay's coil.
Connecting a transistor to the output from an IC:
Most ICs cannot supply large output currents so it may be neccessery to use a transistor to switch the larger current required for output devisec such as lamps, motors and relays.  The 555 timer IC is unusual because it can supply a relatively large current of up to 200mA which is sufficient for some output devisec such as low current lamps, buzzers and many realy coils without needing to use a transistor.
A resistor R.B is required to limit the current flowing into the base of the transistor and prevent it being damaged.  However, R.B must be sufficiently low to ensure that the transistor is thoroughly saturated to prevent it overheating.  A safe rule is to make the base current I.B about five times larger than the value which should just saturate the transistor.

Choosing a suitable NPN transister:
The circuit diagram shows how to connect an NPN transister, this will switch on the load when the IC output is high.



This is how to choose a suitable switching transister:
  1. The transistors maximum collector current Ic (max) must be greater than the load current Ic.          load current Ic = supply voltage Vs / load resistance R.L
  2. The transisters minimum current gain h.FE (min) must be atleat five times the load current Ic devided by the maximim output current from the IC.     h.FE (min) > 5 * (load current Ic / max. IC current)
  3. Choose a transistor which meets these requirements and make a note of its properties: Ic (max) and h.FE (min).
  4. Calculate an appropiate value for the base resister:       R.B = (V.c * h.FE) / 5 * Ic   (where Vc = IC supply voltage).
  5. Then choose the nearest standard value for the base resister.
Choosing a suitable PNP transister:
The circuit diagram shows how to connect a PNP transistor, this will switch on the load when the IC output is low.


A transistor inverter (NOT gate):
 Inverters (NOT gate) are availabkle on logic ICs but if you only require one inverter it is usually better to use this circuit.  The output (voltage) is the inverse of the input signal:
  • When the input is high the output is low.
  • When the input is low the output is high.
Thats all about transistor circuits I need to know to be able to soon hack into my RC car.

Heres the link were I found all the information on transistors -
http://www.kpsec.freeuk.com/trancirc.htm

Sunday, 1 November 2009

The Circuit Holder

While going through the "What's a Microcontroller" book, I was looking as some electronics junk and was soldering out some components that I knew that would would need for the future, for example, some small motors from a cd player and some LED's.  However, it was quite difficult for me to take them out without having a device that would hold my circuit, while I was heating up the solder on the circuit and pulling out the actuall component out of the circuit on the other side.  So instead of just buying a circuit holder, I decided to make one out of all the electronic things I have around me.  So far I made the lamp that would make sure I have anouph light while I'm unsoldering or soldering components, and also a small compartement that would hold a wet sponge for me to cleen the soldering iron with.

I designed the circuit such way so that all the extra parts, such as the light and the sponge holder were detachable.

Side view of circuit holder, with a cup to show the scale.


Top view.


Top view. Light and sponge compartement detached.

Here is a vedeo that will explain everying and show you how it woks -


Thursday, 29 October 2009

Frequency and Sound Part 2

Storing and Retrieving sequence of Musical notes:
A good way of saving musical notes is to store them in the EEPROM. (Electrically Erasable Programmable Read-Only Memory). To do this use the DATA diractive.
Syntax for the DATA diractive -
     Symbol} DATA {Word} DataItem {, {Word} DataItem,...}
Here is an example of how to use the DATA diractive to store the characters that corespond to musical notes:
                 Notes DATA "C","C","G","G","A","A","G"

You can use the READ command to acces these characters.  The letter "C" is located at addreaa Notes + 0, and a second letter "C" is located at Notes + 1, and so on.  For example, if you want to load the last letter "G" into a byte variable called noteLetter, use the cammand:
         READ Notes + 6, noteLetter

You can also store lists of numbers using the DATA diractive.  Frequency and duration values that the BASIC Stamp uses for musical notes need to be stored in word variables because they are usually greater than 225.  This is how to do that with a DATA diractive.

  Frequencies DATA Word 2093, Word 2093, Word 3136, Word 3136,
                                 Word 3520, Word 3520, Word 3136

Because each of these values occupies two bytes, accesing them with the read command is different from accessing characters.  The first 2093 is at Frequencies + 0, but the second 2093 is located at Frequencies + 2. The first 3136 is located at Frequencies + 4, and the second 3136 is locaded at Frequencies + 6.
Here is a FOR...NEXT loop that places the Notes DATA into a variable called noteLetter, then it plasec the Frequencies DATA into a variable named noteFreq -
      FOR index = 0 to 6
           READ Notes + index, noteLetter
           READ Frequencies + (index * 2), Word noteFreq
           DEBUG noteLetter, "          ", DEC noteeq, CR
      NEXT

Here's an example program that demonstrates how to use the DATA directive to store lists and how to use the READ command to access the valuin that list.
Coding:





Video of TwinklTwinkle:


A better system for storing and retrieving music:
By using Bytes, instead of Words, you can write programs that store twice as much music in the BASIC Stamp.
Here's an eample that introduces how to store musical information in a way that relates to the concepts of notes, durations and rests.  Tempo is also introduced. Only the note characters are stored in the Notes DATA directive because LOOKUP and LOOKDOWN commands will be used to be match up letters to their corresponding friequencies.

         Notes                      DATA            "C","D","E","C","C","D","E","C","E","F","G","E","F","G","Q"
         Durations                 DATA             4,   4,   4,   4,   4,   4,   4,   4,   4,   4,    2,   4,   4,   2
         WholeNote             CON                2000


The first number in the Durations DATA directive tells the program how long the first note in the Notes Data directive should last.  The second duration is for the second note, and so on.
Here's a list of what each duration means:
  •   1 - whole note
  •   2 - half note
  •   4 - quarter note
  •   8 - eighth note
  •   16 - sixteenth note
  •   32 - thirty-second note
Ater each value is read from the Durations DATA  diractive, it is devided into the WholeNote value to get the Duration used in the FREQOUT command.
The "Q" in the Notes DATA is for quit, and a DO WHILE...LOOP checks foe "Q" each time through the loop.  You can insert a rest between notres by inserting a "P".
When you use the lower-case version of the note, it will play a flat note.  For example, if you want to play B-flat, use "b" instead of "B".
Example Program - Frere Jacques.
Coding:





Video:


How does it all work:
The Notes and Durations DATA directives combined with the WholeNote constant are used to store all of the musical data used by the program.
Even though a FOR...NEXT loop is no longer used to access the data, there still has to be a variable (index) that keeps track of which DATA entry is being read in Notes and Durations. The offset variable is used in the LOOKDOWN and LOOKUP commands to select a particular value.  The NoteLetter variable stores a character accessed by the READ command.  LOOKUP and LOOKDOWN commands are used to convert this character into a friequency value.  This value is stored in the NoteFreq variable and used as the FREQOUT command's Freq1 argument.  The noteDuration variable is used in a READ command to recieve a value from the Durations DATA.  It is also used to calculate the Duration used in the FREQOUT command.
The main loop keeps executing until the letter 'Q' is read from the Notes DATA.
A READ command gets a character from the Notes DATA, and stores it in the noteLetter variable. The noteLetter variable is then used in a LOOKDOWN command to set the value of the offset variable. Remeber that offset stores a 1 if "b" is detected and a 2 if "B" is detected and a 3 if "C: is detected and so on.  The offset value is then used in a LOOKUP command to figure out what the value of the noteFreq variable should be.  If offset is 1, noteFreq will be 1865, if offset is 2, noteFreq will be 1976 and so on.
The READ command uses the value of index to place a value from the Durations DATA into noteDuartion.
        READ Durations + index, noteDuration
Then, noteDuration is set equal to the WholeNote constant devided by the noteDuration.  If note duration starts out as 4 from a READ command, it becomes 2000 / 4 = 500. If noteDuration is 8, 1500 / 8 = 250.
       noteDuration = WholeNote / noteDuration
Now that noteDuration and noteFreq are determined, the FREQOUT command plays the note.
       FREQOUT 9, noteDuration, noteFreq
Each time through the main loop, the index value must be increased by one.  When the main loop gets back to the beggining, the first thing the probram does is read the next note, using the index variable.

       index = index + 1
   LOOP

I had a few problems with that one - I didnt really understand it at first. But after reading it a couple times, I'm starting to understand it more and know whats going on.
Entering musical data is much easier when all you have to do is record notes and durations.
I editted the last program to play Beethoven's Fifth Symphony, just by replacing the notes and durations DATA diractives.

Adding Musical Features:
Cell phones that play musi to let you know that someone is calling have three features that were not supported by the previouse section:
  • The play "dotted" notes.
  • They determine the whole note duration from a value called tempo
  • They play notes from more than one octave.
The term "dotted" refers to a dot used in sheet music to indicate that a note should be played 1.5 times as long  as its normal duration.  In this example, a zero means there is no dot white a 1 means there is a dot:
         Dots            DATA          0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0
Cell phones typically take the tempo for a song in beats per minute.  This is the same as saying quarter notes per minute.
         BeatsPerMinute         CON     200

CELL PHONE RINGTONES:
One of the most widely used way of composing, recording and posting notes is one that features strings of text that describe each note in the song. Haere is an example of how the first few notes from Beethoven's 5th look like in RTTTL format:
              Beethoven5:d=8,o=7,b=125:g,g,g2d#,p,f,f,f,2d
This format for storing musical data is called RTTTL, which stand for Ringing Tone Text Transfer Language. 

RTTTL Ringtone Player Application Program
This application program is pretty long; in the book they said I can download it from the parralax site, but I couldnt find it so I had to write the whole thing.
Reveille Video:


Coding:
















I changed the song in the coding by simply replacing the RTTTL_File DATA directive with different strings of coding.
JollyGoodFellow video:


OK thats done with Music, I have like two more chapters to post up.
Hope you enjoyed it!

Thursday, 22 October 2009

Frequency and Sound Part 1

To be honest, this was the hardest chapter so far and I think it would be a good idea to get back to it when I finish the book to the end.  I think it was so hard because there were so many new commands and rules introduced.

So I'll start from the basics. 
The rate of high/low signals sent to the speaker from the microcontroller is called the frequency - it determines the tone or pitch of the beep.
Each time a high/low repeats itself is called a cycle - Number of cycles per second (reffered to as Herts, Hz)

The Piezoelectric speaker:
In this activity I will be experimenting with sending some different signals to the Piezoelectric speaker.


 Piezoelectric Speaker Schematic symbol and drawing.

First of all, I built this simple circuit -



How does the Piezo Speaker work?
When a guital string vibrates it causes change in air pressure, and these changes in air pressure are what your ear detects as a tone.  The faster the change in air pressure the higher the pitch, and the slower the change, the lower the pitch.
When high/low signals are applied to the speakers positive terminal, the element inside the piezo speaker, called the piezoelectric element, it vibrates, and causes changes in air pressure.

Programming speaker control:
The FREQOUT command is a convenient way of sending high/low signals to a speaker to make sound.
FREQOUT command syntax -
       FREQOUT Pin, Duration, Freq1 {, Freq2}

Pin - Value you can use to choose what I/O pin to use.
Duration - Value that tells the FREQOUT command how long the tone should play for (in milliseconds).
Freq1 - Argument is used to set the frequency of the tone, in Herts.
Freq2 - An optional Freq2 argument that can be used to mix frequencies.

This is the first test that I did on a Piezoelectric Speaker -


And the coding -




Here are a fiew ActionTones -


Coding -






Mixing Tones:
When you mix 2000 Hz with 20001 Hz, the tone will fade in and out once every second, at a frequency of 1 Hz.  If you mix 2000 Hz with 2002 Hz, it will fade in and out twice a second, and so on.

Mixing Tones Video -


Coding for MixingTones -




I did some programming on that but whats coming up next is the real stuff so check it out -

Musical Notes and Songs:


This is basically an electronic piano.  Rightmost piano keys and their frequencies.
With this, you can create any song, using the FREQOUT command.
Here's some music for you -
DoReMiFaSolLaTiDo:


Coding -




This is the part that gets pretty complicated, but its pretty cool -
Storing and Retrieving sequence of Musical notes:
A good way of saving musical notes is to store them in the EEPROM. (Electrically Erasable Programmable Read-Only Memory).  To do this use the DATA diractive.
Syntax for the DATA diractive -
            {Symbol} DATA {Word} DataItem {, {Word} DataItem,...}

Here is an example of how to use the DATA diractive to store the characters that corespond to musical notes:
      Notes DATA "C","C","G","G","A","A","G"

This is what I put up so far today, I'll put up the rest later or another day.

Monday, 19 October 2009

Light Meter video

Oh yeah, here is a video to show you how the Light Meter works -

The Light Meter

Hi everyone!
Here I'm going to show you how I leared how to make a simple light meter with a 7-segment LED display, a photoresister, a capacitor and resistors.
On the way I learned some programming tricks that saves time and prevents typing error.  I learned how to use subroutines.  A DO...LOOP is usually called the main routine because all the main activity happens in it.
PBASIC has some commands that can be used to make the program jump out of the main routine, do a job, and then return right back to the same spot in the main routine.  This will allow me to keep each segment of code that does a particular job somewhere other than the main routine.  This process is called executing a subroutine.
Here's an example -

DO
   GOSUB Subroutine_Name
   DEBUG "Next command"
LOOP

       Subroutine_Name:
          DEBUG "This is a subroutine..."
          PAUSE 3000
       RETURN


The command GOSUB Subroutine_Name causes the program to jump to the Subroutine_Name: label.  When the program gets to that label, it keeps running and exicuting commands untill it gets to a RETURN statement.  Then the program goes back to the command that comes after the GOSUB command.

Now I'm going to show you how I programmed the light meter using subroutines.

Here is the programming text -




This is how it works -
Variable declaration:
I think its always best to declair variables (and constants) at the beggining of the program.

Initializing:
This is the section where things have to be done once at the beggining of the program.

Get_RC_Time:
This takes the RC time measurement on the photoresister circuit.  This subroutine has a PAUSE command that charges up the capacitor and the time period to charge up the capacitor is very small.  The RCTIME command sets the value of the time variable. This value is then used by the second subroutine.

Delay:
All this subroutine does is contain a PAUSE.

Update_Display:
The LOOKUP command in this subroutine contains a table with six bit patterns that are used to create the circular pattern on the display.  By adding one to the index variable each time the subroutine is called, it causes the next bit patern in the sequence to get placed in OUTH.


I could change the speed of the light spinning by either changing the programmin or replacing the capacitor with a smaller one.