IAN LANG ELECTRONICS

Right then, let's delve in without further ado. First the transmitter:

More Low-Cost TX/RX

Go Back Previous Page>

Note that the data input comes from TX, not RX. Don't put it in the wrong hole and wonder for the next five minutes why it isn't working. I'm speaking from recent experience here, and you feel pretty stupid for about twenty minutes afterwards. The switch is set up for reliably low conditions on pin 7, so:

 

GND

5V

5V

Pin

Pin

There's not a deal to the construct here and so here's the code:

 

byte outgoing;

void setup(){

Serial.begin(4800);

}

void loop(){

if(digitalRead(7)==HIGH){outgoing=49;}

else{outgoing=126;}

Serial.println(outgoing);

 

}

 

There's not a deal of that either, so let's canter on through it :

 

 

byte outgoing;

 

a variable of type byte stores a positive number between 0 and 255. Why not use an int ?  Well, we could, but a byte takes one byte of memory and an int takes two. There's plenty of space, so we could use an int, but I thought it might be an appropriate moment as we're 31 chapters in to bang on about a byte type as it's been largely ignored in these pages and by now we're getting more advanced with physical computing terms and techniques.

In the setup all we do is set for serial communication:

 

void setup(){

Serial.begin(4800);

}

 

If the Baud rate is much lower, there'll be an appreciable lag between pressing the button and the time it takes the receiver to lag. I tried it at 2400- meh. I tried it at 1200-oh dear. At 300, an ice age came and went before anything happened. 4800 it was then.

So now we come to the loop. The first thing is:

 

void loop(){

if(digitalRead(7)==HIGH){outgoing=49;}

 

We set pin 7 of the Arduino to be low unless the button is pressed. Ergo, the only way it can be high is if the button is, well, pressed. Or broken. Let's hope not. So,  if(digitalRead(7)==HIGH) is exactly the same as saying "if the button is pressed" and if that's the case then {outgoing=49;}

 

BUT- if the button is not pressed:

 

else{outgoing=126;}

 

So, the variable outgoing can have a numerical value of 126 or 49. All we have to do now is chuck that through the serial port to which, you will remember, the transmitter is attached, so doing that the transmitter will grab it, and chuck it out over the air:

 

Serial.println(outgoing);

 

}

 

Why println and not just print?  I find after experiment that it better guarantees that what arrives at the receiver is the same as what left the transmitter. And speaking of that receiver:

 

 

 

 

We are going to use the receiver of the TX pair to influence the Arduino here, and the Arduino influences the actions of that LM386 chip. The LM386 is a popular power amplifier for low-power audio applications. It's not the best quality but it can be made to sound very well indeed. My home made portable FM radio has one in, and it does sound better than a lot of commercial ones as I'm making one chip run two speakers. I listen to Classic FM, Radio4 and Radio 3, so the poor thing has to cope with a very wide range of frequencies but does it admirably. The home made FM radio cost £6.73 to make.

I digress. For our purposes, we want it to drive the speaker when requested and isolate it when not, and that way there's no hum when the speaker is off. So, before diving into the code, let's look at the workings of the LM386 for those who don't know.

Here's the chip in diagram form. You'll see that 1 & 8 are the gain pins and you alter those by an RC combination. I've not used any resistors here, just a 10 uF capacitor. When you get this working, whip that cap out and see what happens. There are two inputs + which is non-inverting and - which is inverting. It does not actually matter which you use here; try both as an experiment. The audio out comes from pin 5, pin 7 stops howl. We won't use it here. That leaves us with Vs and GND. GND goes to GND as you'd expect, but Vs goes to pin 8 of the Arduino which you wouldn't. What is the meaning of this mysterious configuration?

This amplifier has an incredibly big mouth for its size. We need a way to mute it or it will hum when the button's not being pressed. We could mess about with the gain. Or we could just switch it on when it's needed and off when it's not. As you'll see in a moment, when the button's pressed on the transmitter, the receiver Arduino sets pin 8 high, and that switches the amplifier on. If we want it louder, we could use a larger voltage and let the larger voltage run through a transistor which the Arduino controls. For now this is good enough.

 

You must couple the speaker to the output of the amplifier by a capacitor, and I suggest 220 uF; but play about with that going up to 440 uF and down to 10 uF just to see what happens. Watch the polarity on electrolytics; they will work backwards but they don't like it and will degrade.

 

So, we know what the amp is for. We know that the transmitter kicks out a numerical value of either 49 or 126.

 

This output crosses the air in the form of a modulated radio wave. It reaches the receiver module, which detects and outputs the information, which is then dumped to the RX pin of the Arduino board. All that's left is to look at what the board does with this information. Here's a sketch:

 

boolean buzzon=false;

byte incomingByte;

void setup(){

  pinMode(8,OUTPUT);

 

Serial.begin(4800);

}

void loop(){

if (Serial.available() > 0) {

incomingByte = Serial.read();

//Serial.println(incomingByte,DEC);

if (incomingByte==49){buzzon=true;}

if (incomingByte==126){buzzon=false;}

if (buzzon==true){

digitalWrite (8,1);tone(11,320,25);}

if (buzzon==false){digitalWrite (8,0);}

}

 

}

 

 

There is a tad more to this one, but not much. There's a line in there that if you unmark as being a REM statement will let you see what's coming in on the serial monitor:

 

//Serial.println(incomingByte,DEC);

 

(delete the // at the front to unmark as a REM statement; bear in mind that if this line runs it'll slow the sketch down and you'll notice a delay inbetween pressing the button and the receiver reacting.)

 

Over the page then, to deconstruct this code line by line.

More>