查看: 1883|回复: 0

[教程] 【一网打尽】LEARN RASPBERRY PI (4 of 13)

[复制链接]
  • TA的每日心情
    开心
    2016-8-4 10:56
  • 签到天数: 242 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2015-3-30 17:04:20 | 显示全部楼层 |阅读模式
    分享到:
    Overview SIMON MONK

    One of the great things about the Raspberry Pi is that it has a GPIO connector to which you can attach external hardware.

    learn_raspberry_pi_gpio.jpg
    The GPIO connector actually has a number of different types of connection on them. There are:
    • True GPIO (General Purpose Input Output) pins that you can use to turn LEDs on and off etc.
    • I2C interface pins that allow you to connect hardware modules with just two control pins
    • SPI interface with SPI devices, a similar concept to I2C but a different standard
    • Serial Rx and Tx pins for communication with serial peripherals

    In addition, some of the pins can be used for PWM (pulse Width Modulation) for power control and another type of pulse generation for controlling servo motors called PPM (Pulse Position Modulation).
    In this tutorial, you are not actually build anything, but you will learn how to configure your Raspberry Pi and install useful libraries ready to start attaching some external electronics to it.

    The GPIO Connector SIMON MONK

    The diagram below show the pins on the GPIO connector for aRaspberry Pi Version 1 (which is what existed when this tutorial was released) Version 2 has pin 27 replacing pin 21 but it otherwise the same

    learn_raspberry_pi_gpio-srm.png
    As well as supplying power (GND, 3.3V and 5V) all the GPIO pins can be used as either digital inputs or outputs. The pins labelled SCL and SDA can be used for I2C. The pins labelled MOSI, MISO and SCKL can be used to connect to high speed SPI devices.
    All the pins have 3.3V logic levels and are not 5V-safe so the output levels are 0-3.3V and the inputs should not be higher than 3.3V. If you want to connect a 5V output to a Pi input,use a level shifter
    A popular way to actually make the connections to the Raspberry Pi is to use a Pi Cobbler.

    learn_raspberry_pi_cobbler.jpg



    This uses a ribbon cable to connect the GPIO connector to solderless breadboard, where you can add your own components.

    Adafruit Pi Code SIMON MONK

    To make life easy for those wishing to experiment with attaching electronics to their Pi, Adafruit have produced an extensive and extremely useful collection of code. This includes simple Python libraries for a large number of modules, including displays, sensors and PWM controllers etc.
    To fetch this code, you need to use some software called 'git'. This comes pre-installed on Occidentalis, but on Raspbian you must install it by entering the following commands into LX Terminal.  
    You will find the icon for LX Terminal on your desktop.

    learn_raspberry_pi_LXTerminal.png
    Before we go any further, issue the following command in LXTerminal. This will ensure your package can be found and that you get the latest version. It does not matter which directory you are in.

    Copy Code


    • sudo apt-get update

    learn_raspberry_pi_apt-get-updating.png
    The update may take a while, especially if this is the first time you have run it on your Pi. Eventually it should give you another command prompt '$' and it will be ready for you to type the next command which is:
    Copy Code


    • sudo apt-get install git

    Once git is installed (if its not already there) you can "check out" the Adafruit Pi Python repository onto your Pi using the following commands

    Copy Code


    • git clone http://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
    • cd Adafruit-Raspberry-Pi-Python-Code
    • ls

    If there is any problem during any of the steps above, you will see an error message. The most common reasons why something should fail to install are:
    • a problem with your Internet connections
    • a mis-typed command. Remember everything in Linux is case sensitive. It is best to open this page on your Raspberry Pi so you can just copy and paste the commands.


    You will find all sorts of goodies in here, many of which we will use in later tutorials.

    Configuring GPIO SIMON MONK

    The GPIO pins can be used as both digital outputs and digital inputs. As digital outputs, you can write programs that turn a particular pin HIGH or LOW. Setting it HIGH sets it to 3.3V setting it LOW sets it to 0V. To drive an LED from one of these pins, you need a 1kΩ resistor in series with the LED as the GPIO pins can only manage a small amount of power.
    If you use a pin as a digital input, then you can connect switches and simple sensors to a pin and then be able to check whether it is open or closed (that is, activated or not)
    Some Adafruit projects that use just GPIO.
    • http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds
    • http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi
    • http://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi


    learn_raspberry_pi_LEDBackpack_Setup_1000w.jpg
    To program the GPIO ports in Python, we need to install a very useful Python 2 library called Rpi.GPIO. This module gives us a simple to use Python library that will let us control the GPIO pins.
    The installation process for this is the same whether you are using Raspbian or Occidentalis. In actual fact, some versions of Raspbian include this library, but these instructions will also have the effect of updating to the latest version, which is worth doing.

    Copy Code


    • sudo apt-get update

    To install RPi.GPIO, you first need to install the Python Development toolkit that RPi.GPIO requires.
    To do this enter the following command into LXTerminal:

    Copy Code


    • sudo apt-get install python-dev

    Then to install Rpi.GPIO itself type:

    Copy Code


    • sudo apt-get install python-rpi.gpio

    You will probably be prompted to confirm by entering 'Y'.
    Thats all there is to it. You are ready to try some of the projects I mentioned at the top of this section.

    Configuring I2C SIMON MONK

    I2C is a very commonly used standard designed to allow one chip to talk to another. So, since the Raspberry Pi can talk I2C we can connect it to a variety of I2C capable chips and modules.
    Here are some of the Adafruit projects that make use of I2C devices and modules:
    • http://learn.adafruit.com/mcp230xx-gpio-expander-on-the-raspberry-pi
    • http://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi
    • http://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi
    • http://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-pi
    • http://learn.adafruit.com/mcp4725-12-bit-dac-with-raspberry-pi
    • http://learn.adafruit.com/adafruit-16-channel-servo-driver-with-raspberry-pi
    • http://learn.adafruit.com/using-the-bmp085-with-raspberry-pi


    The I2C bus allows multiple devices to be connected to your Raspberry Pi, each with a unique address, that can often be set by changing jumper settings on the module. It is very useful to be able to see which devices are connected to your Pi as a way of making sure everything is working.
    To do this, it is worth running the following commands in the Terminal to install the i2c-tools utility.

    Copy Code


    • sudo apt-get install python-smbus
    • sudo apt-get install i2c-tools

    Installing Kernel Support (with Raspi-Config)
    Run sudo raspi-config and follow the prompts to install i2c support for the ARM core and linux kernel

    learn_raspberry_pi_advancedopt.png
    learn_raspberry_pi_i2c.png
    learn_raspberry_pi_wouldyoukindly.png
    learn_raspberry_pi_i2ckernel.png
    Then reboot!
    We also recommend going through the steps below to manually check everything was added by raspi-config!

    Installing Kernel Support (Manually)
    If you are using Occidentalis, then your Pi is ready to go with I2C as far as enabling the hardware goes. However, if you are using Raspbian, you will need to open LXTerminal or console or ssh and enter the following command:

    Copy Code


    • sudo nano /etc/modules

    and add these two lines to the end of the file:

    Copy Code


    • i2c-bcm2708
    • i2c-dev

    like so:

    learn_raspberry_pi_editing_modules_file.png
    Then save the file with Control-X Y <return>

    Depending on your distribution, you may also have a file called /etc/modprobe.d/raspi-blacklist.conf

    If you do not have this file then there is nothing to do, however, if you do have this file, you need to edit it and comment out the lines below:


    Copy Code


    • blacklist spi-bcm2708
    • blacklist i2c-bcm2708

    .. by putting a # in front of them.

    Open an editor on the file by typing:
    Copy Code


    • sudo nano /etc/modprobe.d/raspi-blacklist.conf

    .. then edit the file so that it appears as below, and then save and exit the file using CTRL-x and Y.
    learn_raspberry_pi_blacklist.png
    If you are running a recent Raspberry Pi (3.18 kernel or higher) you will also need to update the /boot/config.txt file. Edit it with sudo nano /boot/config.txt and add the text
    dtparam=i2c1=on
    dtparam=i2c_arm=on
    at the bottom. note that the "1" in "i2c1" is a one not an L!

    learn_raspberry_pi_dtparami2c.png
    Once this is all done, reboot!
    sudo reboot

    Testing I2C
    Now when you log in you can type the following command to see all the connected devices (if you are running a 512MB Raspberry Pi Model B)

    Copy Code


    • sudo i2cdetect -y 1

    learn_raspberry_pi_i2c-detect.png
    This shows that two I2C addresses are in use – 0x40 and 0x70.

    Note that if you are using one of the very first Raspberry Pis (a 256MB Raspberry Pi Model B) then you will need to change the command to:

    Copy Code


    • sudo i2cdetect -y 0

    The Raspberry Pi designers swapped over I2C ports between board releases. Just remember: 512M Pi's use i2c port 1, 256M ones use i2c port 0!

    Configuring SPI LADY ADA

    Start by removing any blacklist for the spi module by running

    Copy Code


    • sudo nano /etc/modprobe.d/raspi-blacklist.conf

    And look for the line
    blacklist spi-bcm2708
    and put a # in front like so:

    learn_raspberry_pi_lsdevspi.png
    Now type in control-X Y and hit return to save the file.
    reboot with sudo reboot

    Next time you log in you can check that you can see the devices with
    ls -l /dev/spidev*
    you should see two 'devices' one for each SPI bus

    learn_raspberry_pi_blacklistspi.png
    Test and Configure SIMON MONK

    The best way to test what you have done is to pick one of the projects (perhaps a fairly simple one to start with) and give it a try.

    Here are some of the tutorials I would recommend as a first project:
    • http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds
    • http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi
    • http://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-pi
















    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 17:43 , Processed in 0.110378 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.