Raspberry PI weather station – Section two – GPIO




This is the second section of the raspberry pi weather station project. How to set up the Raspbian operating system and proxy settings are included in the section one. The main concern in section two is to give an idea about the General Purpose Input / Output ports (GPIO) in Raspberry pi. This section consists of –

  • How to interface an LED
  • How to interface a push button
  • How to interface a temperature sensor to the raspberry pi

Raspberry pi consists of several GPIO pins. The following diagram shows the power and GPIO pins on the Raspberry pi

GPIO pins can be configured as input pins and detect values or as output pins and set the value as high or low.  Basically there are 26 pin comprises of one 3V (3.3V) pin, one 5V pin, one GND (0V) pin, six DNC (do not connect) pins and seven GPIO pins.

Raspberry PI weather station – Section two – GPIOFirst and foremost, GPIO needs to be updated and upgraded. This process could take a bit of a time.  However since this has already accomplished under section one, all there is to do is to double check whether version of the GPIO is correct. Therefore it is recommended to run a version test.

The version test code is as follows

1
2
3
4
5
Imort RPi.GPIOasGPIO
Text = GPIO.VERSION
Print text

Once GPIO is been updated and upgraded the Raspberry pi is ready.

Interfacing a LED

Materials needed

  • A Breadboard and jumpers wires

Method

First the LED and safety resistor are connected on the breadboard.  Since pin GPIO 24 on Raspberry pi is used as the output pin which provides necessary voltage for the LED to switch on, it should be connected to the positive side on the LED using male/female jumper cable. When the status of GPIO 24 is high the LED will switch on and vice-versa. Then the remaining side of the safety resistor is connected to the GND pin on the Raspberry pi.

Code

The code is as follows –

1
2
3
4
5
6
7
Import RPi.GPIO as GPIO       # Import the relevant module
GPIO.setmode(GPIO.BOARD)      # Set the mode of numbering the pins
GPIO.setup(24, GPIO.OUT)      # Setting  GPIO 24 an output pin
GPIO.OUTPUT (24, True)        # Setting the status of GPIO 24 high</pre>

For the LED to blink the code is as follows –

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Import RPi.GPIO as GPIO
Import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(24, GPIO.OUT)
GPIO.OUTPUT (24, False)       # Setting the status of GPIO 24 low
While 1:
GPIO.output(24, True)
Time.sleep(1)
GPIO.output(24, False)
Time.sleep(1)

Interfacing a push button

Materials needed

  • A Breadboard and jumpers wires
  • Push button
  • 10K resistor

Method

First pin 4 of the push button is connected to the 10K resistor on the breadboard.  Then other side of  the resistor is connected to the GND pin on the Raspberry pi. Pin 2 of the push button is connected to the 3V3 pin on the Raspberry pi. Since pin 22 is set as an input and used to read the status of the push button (whether the push button is pressed or not), it is connected to the pin 4 of the push button using male/female jumper cables. When the push button is pressed the status of the pin 22 will be high and vice-versa..

To indicate the status of the push button a LED is used. Here the LED circuit is the same that has mentioned above. When the push button is pressed the LED switches on and when it is not pressed the LED remains off.

Code

1
2
3
4
5
6
7
8
9
10
11
12
Import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(22, GPIO.IN)   # Setting  GPIO 24 an input pin
while 1:
if GPIO.input(22):
 GPIO.output( 24, True)   # When the button switch is pressed, the LED is on
 else:
 GPIO.output( 24, False)  # When the button switch is not pressed, the LED is off

PWM with Raspberry pi

It is possible to dim a LED using PWM code.  The following PWM code is written for the LED circuit mentioned above.

Raspberry PI weather station – Section two – GPIO Diagram

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import time
 import RPi.GPIO as GPIO
 GPIO.setmode(GPIO.BOARD)
 GPIO.setup(24, GPIO.OUT)
 p = GPIO.PWM(24, 50)             # to create a PWM instance pin no 24 and frequency is 50Hz
 p.start(0)                      # value for the duty cycle (0.0 <= duty cycle <= 100.0)
 try: while 1: for dc in range(0, 101, 5):
 p.ChangeDutyCycle(dc)           # dc is the new duty cycle
 time.sleep(0.1) for dc in range(100, -1, -5):p.ChangeDutyCycle(dc) time.sleep(0.1)
 except KeyboardInterrupt: pass
 p.stop()                        # to stop PWM
 GPIO.cleanup()

Interfacing a temperature sensor

Under this part we built a circuit to connect the temperature sensor DS18B20 to the Raspberry Pi.

Materials needed

  • A Breadboard and jumpers wires
  • 4.7 K resistor

 

For more detail: Raspberry PI weather station – Section two – GPIO


© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top