You are on page 1of 6

计算机与通信工程学院

Experimental Report

Course Title: Interface Technology


Experimental project name: Change the function of the button control
Grade:
Class: Computer science& Technology (Fall2019)
Student ID: GJ201909011022
Name: Rahman Md Habibur
Group members:
Experiment
Date:
EXPERIMENT : 4 Change the function of the button control

1.Principle of this experiment:

The standard Raspberry Pi Model B, and B+ have a 2x20 pin connector that provides access to
connections you can use to turn on LEDs, read button presses, spin motors, and more! These pins
are often referred to as General Purpose Input \ Output (GPIO) Pins. The diagram above
shows the pin numbers. Not all pins are used and not all pins are in order.

To connect an LED to these pins, you can use a Male to Female jumper wire to connect to a
breadboard, but it's often difficult to count or recall which pin is which.

To make things easier, we've developed the Pi Wedge breakout board. This connects using a 40
pin ribbon cable directly to a breadboard. The pins are spaced to fit directly over the center-line
ditch of a standard breadboard and provide easy access to the GPIO pins on the Raspberry Pi.

On the right-hand side of the board is a six-pin header for connecting to the Raspberry Pi via
serial. This can be useful if you're using the Raspberry Pi 'headless' (without a monitor,
keyboard, or mouse).

2. Purpose of experiment :

The ribbon cable is keyed so that it can only be inserted in one direction. Connect one end of the
ribbon cable to the Pi Wedge so that the ribbon points up and the key lines up. Connect the other
end to the Raspberry Pi. The key should point toward the center of the Raspberry Pi.

The Pi Wedge will occupy the top 17 rows of a breadboard. Carefully insert the Pi Wedge into
the top-most rows of the breadboard. Because you'll probably use these, connect the 5V and
GND to the power rails on the breadboard.

Note: the power rails on the full-size breadboards are often split between the upper half and the
lower half. You may need to add a jumper for power and ground to make sure that the power
rails are connected throughout the board.
Using the breadboard:

Before we start building circuits and programming on the Raspberry Pi, let's first explore using
the solder less breadboard. The solder less breadboard is a prototyping tool that allows us to
connect wires together without solder and without physically twisting the wires together.

The breadboard is split in half, divided by a ditch - left and right. The left side is completely
separate from the right side. The breadboard consists of a bunch of holes where you can insert a
wire or a component. The center part of the breadboard is organized into rows of five holes each.
Behind each of these rows is a single metal clip that will connect together any wires that are
inserted into this row. On the sides of the breadboard are two vertical columns (sometimes
marked with a + and a -). These are called power rails. The vertical columns are connected
vertically. We often connect Power and Ground to these two columns to provide access to power
for building your circuit. These are similar to a power strip you might use in your house.

Building a simple LED circuit:


An LED is a special component that lights up when electricity is passed through it. An LED,
however, only works when electricity flows in a certain direction. It as what we call a polarity.
There are two legs - one is longer than the other. The shorter leg (cathode) is negative. And, if
you look carefully at the plastic part, you'll notice a flat edge on this side. The longer leg is called
the anode and is positive.

LEDs have very little resistance, and because of this, they use very little power and make really
great light sources. However, because there's little resistance, we need to include a current
limiting resistor when we use an LED in a circuit.

Here is a diagram for a circuit that you're going to build. The symbol for the LED is the triangle-
shape with a line and circle. It has arrows pointing away from it showing 'light' is emitted when
electricity flows through this device.

The squiggly symbol after that is called a resistor. Resistors come in various sizes that indicate
how much it slows down current flow. Resistance is measured in units called ohms. The resistor
we're going to use is a 330 ohm resistor. This limits the current to about 9 mA.

Let's build the circuit! Use the diagram on the right to build a simple LED circuit on the
breadboard. Make sure that the long leg of the LED is connected to the 5V side. Here, we're
using the ditch to separate the two legs of the LED.

Rinse & Repeat. Now see if you can add more LEDs to your board. The practice of using the
breadboard is essential to working with electronics. Pay careful attention to where the wires are
plugged in. If a wire is off by a single row, it may not be connected to your circuit.
3.The main code of the experiment :
#include <iostream> // Include all needed libraries here
#include <wiringPi.h>

using namespace std; // No need to keep using “std”

int main()
{
wiringPiSetup(); // Setup the library
pinMode(0, OUTPUT); // Configure GPIO0 as an output
pinMode(1, INPUT); // Configure GPIO1 as an input

// Main program loop


while(1)
{
// Button is pressed if digitalRead returns 0
if(digitalRead(1) == 1)
{
// Toggle the LED
digitalWrite(0, !digitalRead(0));
delay(500); // Delay 500ms
}
}
return 0;
}

4. Experimental test phenomenon and analysis :

In our circuit, we're going to use a momentary push-button switch. This device closes the circuit
when the button is pressed, and then springs back up when you release the button. These push
button switches have four legs and are designed to straddle the ditch of a breadboard. The left
and right legs are connected internally.

Sometimes this symbol is used to represent the push button switch. It shows the internal
connections of the legs on the push button.

You can also set the LEDs to a half brightness or mid-level brightness using a technique called PWM
(pulse-width-modulation). The Raspberry Pi is a digital device. It's only capable of turning pins ON and
OFF, but it can also do this really, really fast. It can turn an LED on and off so fast you don't see it blink.
The Raspberry Pi flashes the pin at 800 Hz - that's 800 times per second! By changing the duty cycle (the
amount of time the pulse is on compared to the amount of time the pulse is off), makes the LED look
either brighter or dimmer. The image to the right illustrates how this works.

To control the brightness of an LED, you need to set it up as an outputpwm pin.


To set the brightness, use the broadcast command: gpio[#]pwm[value] -- [#] indicates the GPIO
pin, [value] indicates the duty cycle of the PWM signal ranging from 0 to 255. These three
blocks set pin 5 off, half brightness, and then full brightness.

You might also like