IAN LANG ELECTRONICS

Now to test it I put an LED and it's resistor between pin 3 and ground. There's a particular reason for picking pin 3, and you'll see what it is in a moment. Here's the sketch:

 

int dimlevel=0;

boolean checkflag=false;

void setup(){}

void loop(){

if (analogRead(A0)>1000&&checkflag==false){

  checkflag=true;

  redim();}

if (analogRead(A0)<700){checkflag=false;}

  analogWrite(3,dimlevel);

}

 

void redim(){

switch (dimlevel){

case 0:

dimlevel=43;break;

case 43:

dimlevel=85;break;

case 85:

dimlevel=127;break;

case 127:

dimlevel=169;break;

case 169:

dimlevel=211;break;

case 211:

dimlevel=253;break;

case 253:

dimlevel=0;break;}

}

 

Cantering through it, we set up two variables, one an int type, which is dimlevel and controls the brightness of the LED on pin 3, and the other a boolean, which is checkflag and locks/unlocks sections of the code as we need to. Nothing at all happens in  setup, so it's straight to loop where we find an if statement with a boolean argument.

 

if (analogRead(A0)>1000&&checkflag==false){

 

checkflag at this point is false because we set it so, so that half of the condition is satisfied. Pin A0 is attached to the output pin of the 555 chip. Now, this pin has the handy property that if no current is coming out of it, it sinks any current going in. So the pin A0 will remain as low as it can get until something happens to make current come out of pin 3 of the 555 chip. That something is a finger touching the trigger. Once that happens, current will come out of pin 3 and A0 will go as high as it can get. When that happens, both conditions are satisfied and the code within the conditional is satisfied:

 

  checkflag=true;

  redim();}

 

Setting checkflag to true locks this if statement out as the second conditional &&checkflag==false can't be satisfied any longer. After that, we call a function called redim:

 

void redim(){

switch (dimlevel){

case 0:

dimlevel=43;break;

case 43:

dimlevel=85;break;

case 85:

dimlevel=127;break;

case 127:

dimlevel=169;break;

case 169:

dimlevel=211;break;

case 211:

dimlevel=253;break;

case 253:

dimlevel=0;break;}

}

 

This is an easy spot, all we are doing is looking at the level the variable is at and moving it to the next one. If it's at the top (253), we knock it to the bottom again (0). The run now goes back to the loop:

 

if (analogRead(A0)<700){checkflag=false;}

  analogWrite(3,dimlevel);

}

The value of the read of Pin A0 will go immediately back to zero the millisecond the current stops coming out of pin 3 of the 555. So at the end of the cycle the code unlocks the first if statement ready for the next input and locks the second. This produces the delay that stops the cycling. All that's left to do is to write the LED at the required level, and that's what analogWrite does.

 

The trigger of the 555 when not connected to the supply voltage rail is incredibly sensitive because it has a number of Darlington pairs inside. This means that it takes very little current indeed to set it off. As you've seen, it goes even at the touch of a finger. The delay is caused by the resistor/capacitor pair at the discharge and threshold pins.  The time of the output pulse is given by the equation:

 

T = 1.1 × R1 × C1

 

and if you do the maths you'll find this comes to 2.42 seconds.

 

The Arduino is acting as a detector and a modulator. The detection part comes from pin A0, which is either low or high depending on the state of pin 3 of the 555, but the modulator part is a somewhat more esoteric concept. What we are actually doing is using a technique called pulse width modulation.

 

Pulse Width Modulation or PWM is a technique by which we can drive, using digital signals, an analogue device at varying levels of voltage (and as we vary the voltage so do we vary the current as the resistance remains the same and I = V/R). An analogue waveform looks like this:

 

 

 

 

 

 

Below  you'll see a video of me (well, the tip of my finger at least) playing with a touch switch operating from my Arduino Uno. In the video the touch switch switches on the LED at the first touch and off at the second. My finger is acting as a conductor and allowing charge to escape from my body, rather like a giant capacitor, and for this reason the device is known as a capacitive touch switch. Now if you look on the internet you'll find all sorts of capacitive touch switches, some of which are quite complex and involve all sorts of logic, and Sparkfun make a part to add to your Arduino to let you have a remarkably sensitive device. This DIY solution uses three components, one Analogue and one digital pin and a sketch of 1236 bytes. The sketch below lets you have six levels of illumination from off to full. The physical construct of the circuit allows for a little more than two seconds delay to eliminate too quick cycling.

Touch Switch for Arduino

Go Back

Here's the circuit:

The value of the analogue wave could be at any time at any value between the minimum and maximum. We aren't suppling a negative component and so it'll be somewhere between o and 90 degrees at any given time. As we are supplying 5V max, that'll be the value at 90 degrees.

But the Arduino is not an analogue device. It is entirely digital. Nevertheless, we can simulate an analogue waveform digitally. A digital wave form looks like the diagram below:

This is known as a square wave form and it is in fact what the 555 puts out at the output. The square wave is either full on or full off and it returns instantly to one or the other states.

The green lines above mark a time window and in the case of the Arduino it is about ten milliseconds long. You'll notice that within that time window, the current is high for most of the time and low for the rest. Where it is high, it is known as the mark, where low the space and the two together are known as the duty cycle or simply the cycle. Above, it is high for about 75% of the time. An analogue device seeing this will rapidly switch on and off and will see it as only 75% of the true value of the voltage; that is 3.75V. If the mark and space were equal, it would see 2.5V and if the space were 75% (and thus the mark 25%) it would see 1.25V.

The output voltage is not expressed in terms of the value of the voltage but as a digital step in multiples of about 20 mV. So 5V (5000 mV) would be step 255 and 0V would be 0. 2.5V would be 128.

To use PWM we express the term analogWrite as above.  analogWrite(3,dimlevel); means use a PWM output at pin 3 to generate every 10mS the appropriate width of pulse high for the current value of the variable dimlevel. And the practical upshot of that as you have seen is that we can control the output voltage to the device, in this case an LED. This is the principle of a digital potentiometer.

 

On your Arduino the pins marked with a tilde  ~   are the ones capable of doing PWM. On the UNO they are 3, 5, 6, 9, 10 and 11. If you try it on any other pins, this won't work.

 

That about wraps it up for this lesson, and so let's draw a veil over it. The 555 is a very common and cheaply acquired chip and  for those of you interested in studying the properties of the 555 a little more deeply, it is dicussed in the link to the left.

 

 

 

Ian Lang November 2011

555