dimanche 24 mars 2013

Discovering the Raspberry Pi - Part A

Décembre 2018: Mes articles sur ce site date de 2013!
Je les ai tout de même laissés, ils ont peut-être encore une utilité.

Il vaut mieux consulter ce site: http://Mon livre sur Java, Python et le Raspberry Pi 3

A starting point with Raspberry Pi

>>>>>>>  D'autres articles disponibles sur http://www.boichat.ch

After I got this nice project running successfuly, I decided to write in a blog sufficient information to help beginners with Raspeberry Pi to become familiar with its hardware and software. There are already so many articles on the Web doing more or less the same thing .... but why not again and probably differently!

Being able to switch on or off a 220 Volt home appliance using the light level in the room

So many things have been done before reaching this first running project including:
  • Getting the hardware
  • Installing the software
  • Describing all sort of difficulties before getting my bedside lamp reacting on light level changes in the room
  • Adding features inside and outside to extend this project
The idea is also for the author to collect all possible information in order to be able for him, or other interested "players of this Rasperry Pi game", to find out the essential!

The different parts

This is the sensor part to get the light level:

Photo-resistance circuit

This is the relay to switch on a 220V light or anything else connected for instance to a light bulb cable:

220 Volt Relay

And both connected to the Raspberry Pi board:

The code

The following Python code to deal with this project is more or less some copy/paste from different projects, in particular from:

# Reading an analogue sensor with a single GPIO pin
# Set the relay on off according to the light level

import RPi.GPIO as GPIO, time

# GPIO library to use Broadcom mode

# Charge time measument
def RCtime (PiPin):
  value= 0
  # Discharge capacitor
  GPIO.setup(PiPin, GPIO.OUT)
  GPIO.output(PiPin, GPIO.LOW)

  GPIO.setup(PiPin, GPIO.IN)
  # Count loops until voltage across
  # capacitor reads high on GPIO
  while (GPIO.input(PiPin) == GPIO.LOW):
    value+= 1

  return value

GPIO.setup(14, GPIO.OUT)

# Main program loop
while True:
  lum = RCtime(4)
  print lum # Measure level using GPIO4
  if lum < 360:
     print 'High light'
     GPIO.output(14, False)
     print 'Low light'
     GPIO.output(14, True)

The relay

This is the part dealing with the 220V relay. On the picture, we see that 3 cables are connected (one of the 4 is not required):

·        Yellow - the GPIO pin
·        Red - the 5 Volt power
·        Black - the ground

The wire for the 220 Volt goes to the green connector. A 220V extension cord with 2 o 3 (with ground) wires can be prepared like this:

There is a very nice description of the pins in this Web site:

The pin 8, the fourth from the right on the external side:

correspond to the BCM GPIO pin 14 (see the Python code). The pin 2 is the 5 Volt required for the relay.

Testing the relay

This is actually recommended when building a project with different parts.
Here a small program for verifying our relay:

import RPi.GPIO as GPIO, time

print "Start"


PiPin = 14
GPIO.setup(PiPin, GPIO.OUT)

GPIO.output(PiPin, True)


GPIO.output(PiPin, False)

print "End"

There are many different ways to test this relay.
This could be done using C++ or directly accessing the GPIO from a Terminal session.

There is an interesting article with nice pictures here:

The sensor part

The cables required are:
·        Purple - the 3.3 Volt for the power
·        Black - the ground
·        Grey - the GPIO pin to measure the lightsensor

A complete and nice description here:

There is no need to copy/paste it here! 

A view from inside of the pins may help too:

The first pin is the 3.3 Volt and the fourth the GPIO4.

More articles:

And with the Gertboard:

Aucun commentaire:

Enregistrer un commentaire