Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hook up an LED to your RPi and make it blink Blinking an LED. See the details on

ID: 2082057 • Letter: H

Question

Hook up an LED to your RPi and make it blink Blinking an LED. See the details on the WiringPi site under module 9, and how to hook up an LED to your RPi and turn it on and off. Once you can do that, the rest is pretty easy. Really. For LED help see https://startingelectronics.org/beginners/components/LED/Push button switch input on your RPi Switch input: connecting a push button switch to your RPi. Once you've got an LED to blink using a GPIO0 pin in output mode, the next step is to implement a push button switch as an input device. The only tricky part is that switches bounce. When the physical only tricky electrical contacts of a mechanical switch collide, they bounce-for milliseconds, which is too fast for us slow homo sapiens to notice, but computers can respond to every bounce which can cause problems. So we have to de-bounce mechanical switch contacts by inserting a delay when the physical contacts open and close. For this part connect one side of the push button switch to GPIO1 on pin 12 of connector P1 on your RPi, the other side of the switch to on pin 14 Then connect GPIO1 pin 12 to one end of a resistor and the other end of Low or 0, and when is it GPI01 to ground and read as logic the button is not pressed, it will read as logic HIGH or 1. Write code that will toggle the LED. That means that pushing the switch will turn the LED ON if it is OFF, and when it is OFF, pushing the button will turn it ON. Simple, Binary goes a long way... While connecting digital input and output bits might seem trivial, you might be surprised by what you can do by the right hardware to your using a solid state relay controlled by internal LED, you could turn the power to an appliance or a whole building on or off. A simple magnetic reed switch can be connected input to an to sense the position of a magnet for determining when a door or window is opened or closed. With some finesse and sophistication, a simple digital output can be coaxed into generating analog signals in the audio range. With the right digital sensors, you can measure temperature, speed, light levels and more. With an analog to digital converted (A/D or ADC) you can measure the voltage output sensors that put out a voltage proportional to temperature,,humidity, pressure, position, rotation rates, and most else that can be receivers other sensors include and gyros and GPs receivers to measure geo-location. With simple digital output, you can control the position of the arm on a servo, control the speed of a motor or the brightness of an LED. With a digital-to-analog converter (D/A or DAC), you can output analog audio and other signals. Not even the sky is a limit RP is have been carried in weather balloons to the edge of space! What can you use your RPi for?

Explanation / Answer

Solution:

Setp1: Hook up an LED to RaspberryPi and make it blink

Using the breadboard which is hooked up to the Raspberry Pi and build the blinking LED circuit.

To test your circuit, use a simple Xojo app that will alternate between ON (HIGH) and OFF (LOW) on pin #4. When the pin is ON (HIGH), the LED will illuminate.

Xojo app:

GPIO.SetupGPIO

Const kLEDPin = 4                                           ' "#4" on the pinout

GPIO.PinMode(kLEDPin, GPIO.OUTPUT)           ' Set the pin to accept output

While True                                                        ' Blink LED every 1/2 second

GPIO.DigitalWrite(kLEDPin, GPIO.ON)             ' Turn the pin on (give it power)

App.DoEvents(500)                                              ' Turn the pin off (no power)

GPIO.DigitalWrite(kLEDPin, GPIO.OFF)

App.DoEvents(500)

Wend

Transfer and Run the xojo application:

cd LEDBlinker

Run the app:

sudo ./LEDBlinker

export WIRINGPI_GPIOMEM=1(Use local variable to run the app.

Step 2:                  Push Buton switch input on your RPi

Code:

#define BUTTON1_PIN 12                           

#define BUTTON2_PIN 17

#define DEFAULT_LONGPRESS_LEN 25

#define DELAY 20

enum { EV_NONE=0, EV_SHORTPRESS, EV_LONGPRESS };

class ButtonHandler {

public:

     ButtonHandler(int pin, int longpress_len=DEFAULT_LONGPRESS_LEN);

    void init();

     int handle();

protected:

     boolean was_pressed;  

     int pressed_counter;  

     const int pin;        

     const int longpress_len;

};

ButtonHandler::ButtonHandler(int p, int lp)

:pin(p), longpress_len(lp)

{

}

void ButtonHandler::init()

{

pinMode(pin, INPUT);

digitalWrite(pin, HIGH);                                % pull-up

was_pressed = false;

pressed_counter = 0;

}

int ButtonHandler::handle()

{

int event;

int now_pressed = !digitalRead(pin);

if (!now_pressed && was_pressed)

{

     if (pressed_counter < longpress_len)

       event = EV_SHORTPRESS;

     else

       event = EV_LONGPRESS;

}

else

     event = EV_NONE;

if (now_pressed)                                             % update press running duration

     ++pressed_counter;

else

     pressed_counter = 0;

was_pressed = now_pressed;

return event;

}

ButtonHandler button1(BUTTON1_PIN);             % Initiate button objects

ButtonHandler button2(BUTTON2_PIN, DEFAULT_LONGPRESS_LEN*2);

void setup()

{

Serial.begin(9600);

button1.init();

button2.init();

}

void print_event(const char* button_name, int event)

{

if (event)

Serial.print(button_name);

Serial.print(".SL"[event]);

}

void loop()

{

int event1 = button1.handle();

int event2 = button2.handle();

print_event("1", event1);

print_event("2", event2);

static int counter = 0;

if ((++counter & 0x1f) == 0)

Serial.println();

delay(DELAY);

}

Note: When Push button is press, current flowing will be Vcc/R. For Pull down, connect the Vcc instead of GND, and read High 1 upon Press.