Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices

blog

Resources

code, circuits, & construction

my del.icio.us links

 

Lab Assignment

 

 Variables and Analog Input

 
Minimum parts needed: (new parts in bold. see parts list for details)
  • Prototyping board (breadboard)
  • Power supply connector
  • 5-15VDC power supply
  • Assorted wires
  • 5V regulator
  • PIC 18F452 or BX-24
  • Serial cable
  • DB9 female serial connector & headers
  • LED's
  • Variable resistors (Flex sensor, potentiometer, photocell)
  • 1Kohm resistors
  • 10Kohm resistors
  • 220 ohm resistors

Step 1:

Connect a potentiometer up to pin RA0 of your PIC 18F452 or pin 13 of your BX-24 as shown.

PIC 18F452 with analog in

PIC with pot on RA0. Note the decoupling capacitor to smooth out the results.

BX24 with potentiometer and Monski

BX-24 with pot on pin 13

On the PIC, you'll also need to connect wires to pins RC6 and RC7 to a DB9 serial connector, and connect it to the computer's serial port. For more on this, see the debug notes.

Program the PIC to read the varying voltage coming through as follows:

' PicBasic Pro program to display result of
' 10-bit A/D conversion through serial at 9600 baud
'
' Connect analog input to channel-0 (RA0)
 
' Define ADCIN parameters
DEFINE  ADC_BITS        10     ' Set number of bits in result
DEFINE  ADC_CLOCK       3     	' Set clock source (3=rc)
DEFINE  ADC_SAMPLEUS    50    	' Set sampling time in uS

ADCvar  VAR WORD                ' Create variable to store result

TRISA = %11111111       ' Set PORTA to all input
ADCON1 = %10000010      ' Set PORTA analog and right justify result
Pause 500               ' Wait .5 second

main: 	
	ADCIN 0, ADCvar       ' Read channel 0 to adval
	serout2 PORTC.6, 16468, [DEC ADCvar, 13, 10]  ' print it to serial out, 
                               ' with linefeed and carriage return (10, 13)	
GoTo main                   ' Do it forever        

Program the BX-24 to read the varying voltage coming through the pot as follows:

dim potVar as integer

Sub main()
	call delay(0.5)  ' start  program with a half-second delay 

	do
		potVar = getADC(13)
		debug.print "potVar = " ; cstr(potVar)
	loop

end sub

Notice the range of numbers the pot returns. See what happens when you put a resistor in series with the power end or the ground end.

 
Step 2:

Connect another variable resistor RA0 or your PIC, or pin 13 of your BX-24 as shown:

PIC with flex sensor on RA0. Note the decoupling capacitor to smooth out the results.

BX24 with flex sensor and Monski

BX-24 with flex sensor on pin 13

Program the PIC or BX-24 to read the varying voltage coming through the sensor as shown above.

Try differing values for the fixed resistor in the circuit. Note how they change the value returned by getADC(). Try using a potentiometer instead of the fixed resistor (the center pin and either one of the side pins will do). Notice how the range changes when you vary the pot or the variable resistor.

 
Step 3:

Use an analog sensor (a pot or other variable resistor) in an application. Perhaps the changing value of a sensor affects how many lights or motors are turned on or off; perhaps a seven-segment LED display is used to generate numbers. Or a practical joke with the BX-24. Make a sensor that lights a series of LEDs only after you affect it beyond a certain threshold, or a step pad that lights up only after the user steps away. Use variables to keep track of the changes of the switches and analog sensors as the user interacts with the project.

Look for other digital output devices that will respond to the output of the BX-24: piezo buzzers are a start. See what else you can find to add some sensory stimulation to your project.

Once you have exhausted your creativity on this lab assignment, explore the analog output lab assignment for more possibilities.

 
Still not sure about variables?

The following steps are illustrations of how variables work. If you're still confused a bit about variables, these might help.

Step 4:

Wire a switch to pin RB0 of your PIC, or pin 5 of your BX-24. Use the same digital input circuit from the previous lab. Wire an LED to pin RB7 of your PIC or pin 20 of your BX-24. Write a program that stores the state of pin 5 in a variable, and changes the LED based on that variable.

PicBasic Pro:

switchVar var byte
input portb.0
output portb.7

pause 500     ' start program with half-second delay
main:
    switchVar = portb.0
    portb.7 = switchVar
goto main

BX-24:

dim switchVar as byte

Sub main()
	call delay(0.5)  ' start  program with a half-second delay 

do
	switchVar = getPin(5)
	call putPin(20, switchVar)
loop
end sub
Step 5:

Write a program that counts the number of times the switch has been switched on, and puts it in a variable:

PicBasic Pro:

switchedOnVar var byte
input portb.0

pause 500     ' start program with a half-second delay
main:
    if portb.0 = 1 then
        switchedOnVar = switchedOnVar + 1
    endif
    serout2 portc.6, 16468, [DEC switchedOnVar, 13,10]
goto main

BX-Basic:

dim switchedOnVar as byte

Sub main()
	call delay(0.5)  ' start  program with a half-second delay 

do
	if getPin(5) = 1 then
		switchedOnVar = switchedOnVar + 1
	end if
	debug.print cStr(switchedOnVar)
loop
end sub

Why does the counter run so high? How can you make the variable increment only once per switch press?

Step 6:

Connect 8 LED's to pins RB0 through RB7 of your PIC or pins 5-12 of your BX-24. Run the following code to see the binary contents of a memory register reflected in the LED's:

x var byte

trisb = %00000000

pause 500     ' start program with a half-second delay
main:
    for x = 0 to 255
        portb = x
        pause 1000
    next
    pause 3000
goto main

BX-24:

Sub main()
	call delay(0.5)  ' start  program with a half-second delay 

	dim counterVar as byte
	register.ddrc = bx1111_1111	
	register.portc = bx0000_0000

	do
		for counterVar = 0 to 255
			register.portc = counterVar
			call delay(1.0)
		next
		call delay(3.0)
	loop
end sub

The portb on the PIC, or register.portc on the BX-24, are special memory registers, connected directly to the output pins of the LEDs. When you increment the value in these memory registers, the change is seen in the pins. If you read all eight pins as an eight-bit number in base two, you will know what number is in the computer's memory at register.portc. See the BX-24 register example for more details.