Ultrasonic Range Sensor

May 3, 2009

My second project using a Pic microcontroller was controlling the brightness of a LED using an ultrasonic range sensor.  when an object was close to the sensor the LED was dimly lit and as the object moved away the LED got brighter.

Link to code: Sensor

For this project I learned how pulse width modulation worked and how to operate the SRF05 ultrasonic range sensor.  I decided that since the pic18F1220 had a built in PWM module, I would learn how to use that.

In order to generate a pulse width modulation the pic microcontroller uses a timer and compares the timer value to two other registers.  One of the registers (PR2 in the 18F1220) is the value at which the timer resets.  This value is related to the period of the pwm waveform.  The other register (CCPR1L in the 18F1220) is the duty cycle.   When the timer is less than this value, the waveform is high.  Otherwise the waveform is low.  When CCPR1L is equal to PR2 the duty cycle is 100%.

The SRF05 creates a pulse who’s length is proportional to the distance an object is from the sensor.  In order to begin the distance measurement the SRF05 needs a 10us trigger pulse.  The SRF05 then measures the distance and outputs a pulse that is between 100us and 25ms.  Measuring the pulse can be done by waiting for the output line of the SRF05 to go high, then setting up a timer with an appropriate prescaler to run until the line goes low.  Once the pulse is over the timer value is read and can be mathematically manipulated  to get the distance. The SRF05 recommends waiting 50ms before getting another measurement so the previous ultrasonic pulse doesn’t interfere.  Documentation on the SRF05 can be found here

For this project I used a simple loop where I got the distance from the SRF05 then moved the timer value into CCPR1L.  I then waited for 50ms before getting the distance again.  The led was hooked up to the output of the pwm wave.  The pulse widht modulation controlled the brightness of the led.

Advertisements

7-Segment display

May 2, 2009

My first pic microcontroller project was counting on a 7 segment display. Technically I started with a flashing led but what kind of project is that? This was my first project that actually did something. I used a pic 16F84A, a 7 segment driver chip and a 7 segment display. The program was fairly simple. it needed to count to 9 then I could either have it count down or reset to zero. I also had to create some delay so that the number would actually be seen on the 7 segment display.

This program logically was not difficult but inexperience made the process slow. First I didn’t think of ordering an oscillator when I ordered the sample pic. After some reading in the data sheet I figured out there was an RC mode where I could use a resistor and capacitor. This seemed simple enough until I realized that I didn’t have the capacitor that I needed. I read online how someone used just a resistor and the stray capacitance from the breadboard. I decided to give this a try. This is the most unstable method you could use. It worked alright but sometimes the pic would be slow and other times it would fly through the program.

This was the project that I learned all the general information about programming a microcontroller. I learned how to configure ports for input and output and how to move and manipulate data in the microcontroller. The most valuable thing I learned was how to look up information in the data sheet. At first It was difficult to follow and understand how things were organized in the data sheet. After a while I got good at searching for key words.

Link to code:  7-segment


New Bicycle

May 15, 2008

yesterday I got my first road bike. Its a Giant OCR2 with an aluminum frame, carbon fiber fork & seat post.

I had a new bicycle in my plans for mid summer but after I started having spoke problems with my Gary Fisher Nirvana agan I decided to look into a new bicycle. While the spokes were being repaired I looked at road bikes at Scheels, the Spoke bike shop, and the Flying Penguin. Scheels had a good selection but they were all over priced. the spoke also had a good selection but they didn’t have the right size frame in the store for many of the entry level road bikes. I went to the flying penguin where I bought my skis two years ago and found that they carried the Giant bicycle line like the spoke and they had the right frame size. I was still debating if I really should get the bicycle so I went home to think about it.

I went out for a ride on the repaired wheel and had another spoke break within 5 miles. I decided that It was only going to get worse and I didn’t want to spend too much more when I was planning on a new bicycle this summer so I decided to go back to Mankato and get the Giant OCR2 from the flying penguin. I got a deal on it because I already had the clipless pedals and they also threw in a water bottle and holder.

So far I have put 17 miles on the new bike and the ride is really nice. Shifting with the break levers is a little bit different but it works really nice.