Debouncing a digital input

Any switch is made of two conductive mechanical contacts that are either touching each other (closed circuit) or not (open circuit). When they make contact, the contact is not always perfect. For the fraction of s second before the contact is complete, the circuit may make contact and be broken several times. When you read this input on a microcontroller, you’ll see a rapid switching from 0 to 1 over a few milliseconds until the contact is final.

Continue reading “Debouncing a digital input”

DC Motor Control with a TIP120 Transistor

This example assumes you’re using a DC motor that runs on low voltage DC, in the 5-15V range. Connect leads to its terminals, and run if from a benchtop power supply if you have one. Try changing the voltage on it, and seeing what effect it has. Don’t go over the motor’s rated voltage. Connect a switch in series with the motor and use it to turn on the motor.

Continue reading “DC Motor Control with a TIP120 Transistor”

Simple PWM in PicBasic Pro

This is an example of pulsewidth modulation for the PIC. The value received from an analog input is used to dim an LED, using the PWM command. The LED is on RD2, and the analog in is on RA0.

The PWM command has three parameters: the pin, the duty cycle (a byte), and the number of times to pulse the pin (a word). The duty cycle is how long the pin is on for each cycle. If the duty cycle is 100% (255), then the pin is on all the time. A duty cycle of 50% turns the pin on for half of each cycle, and so forth.

At 4MHz, one on-off cycle is about 5 milliseconds. A higher number of cycles makes for smoother PWMing, but less interactivity, because the PIC does nothing else until it’s finished all the cycles for each PWM command.

Continue reading “Simple PWM in PicBasic Pro”

Lantronix Device Connection with serial feedback

This program waits for the Lantronix device to give an acknowledgment of a net connection before it starts to send data. The Lantronix Device has to have its connect mode set to verbose, so that it will send ASCII N when not connected, ASCII D when disconnecting, and ASCII C when a connection is made.

Technorati Tags: ,


Continue reading “Lantronix Device Connection with serial feedback”

Random Numbers and Physical Computing

Most microcontrollers don’t have a random function. Random functions are not truly random, they’re actually a complex mathematical formula that results in a number that “seems” random. That can take up lots of processing time, so it’s usually the first function to go when writing a microprocessor language.

In fact, most of what you do in programming physical computing projects is to figure out how to deal with the world’s natural randomness and make it look smooth. A photoresistor read through an analog-to-digital converter, for example, will never give you a nice steady number, it always fluctuates with tiny changes in lighting that your eye can’t see. Your consciousness is a great leveller for the sensors that are your eyes, ears, skin, nose, and taste buds When you move a photoresistor from one room to another, your readings will be totally different, and all of a sudden, you have to re-calculate what is “average” and what constitutes the lighting change that you want. And that’s just one of many examples. The fact is, data from sensors is filled with the noise of the real world. Plan for it in advance.

Technorati Tags: , ,


Continue reading “Random Numbers and Physical Computing”

PicBasic Pro Debug Statement

The debug statement in picBasic Pro is a limited version of asynchronous serial communication. You have to define the debug port and pin, the baud rate, and the mode at the top of your program, and you can’t change any of these settings within the progam. Once that’s done, though, sending messages to the PC is a snap. Connect the whatever pin you use on te PIC to debug to the serial input of a PC (pin 2 on a DB-9 serial connector), and connect pin 5 of the DB-9 connector to ground. Then open HyperTerminal, set the baud rate to 9600, and run this program.

Continue reading “PicBasic Pro Debug Statement”