background image

 

113 

increases along the

 

Ax

 

axis direction, so does

 

Y_OUT

 along the Ay axis. See the picture below. 

Thus, when you rotate the module, you can see the changes of 

X_OUT

Y_OUT

, and

 

Z_OUT

.

 

 

Relationship between output and gravity direction 

Pin Function of ADXL345 Module: 

Name 

Description 

VS 

Supply Voltage 

CS 

Chip Select; I2C mode is enabled if it's tie-high to VDD 
I/O  VDD I/O = 1.8V). 

SDO 

Serial Data Out, alternate I2C address select 

INT1 

Interrupt 1 Output 

INT2 

Interrupt 2 Output 

3.3V 

3.3V 

SDA 

Serial Data (I2C), Serial Data In (SPI 4-Wire), Serial Data 
In/Out (SPI 3-Wire) 

SCL 

Serial Communications Clock 

GND 

GND 

Содержание Super Kit 3.0

Страница 1: ...del B Pi 2 model B Pi 3 model B and Pi 3 model B It includes various components and chips that can show different interesting phenomena You can make it happen by following the experiment instructions and learn basic knowledge about them Also you can explore more application after mastering the principle and code Now get on the road In this book we will show you circuits with both realistic illustr...

Страница 2: ...sole 16 Using Console in GUI terminal recommended for beginners 16 Using Console Only 18 Introduction to Raspberry Pi 22 RAB Holder 22 Raspberry Pi 24 Raspberry Pi Pin Name 24 Extension Board 25 GPIO Libraries 28 WiringPi 28 Introduction 28 RPi GPIO 29 Introduction 29 Download the Code 30 Lesson 1 Blinking LED 31 Lesson 2 Controlling an LED by a Button 40 Lesson 3 Flowing LED Lights 44 ...

Страница 3: ...8 4N35 64 Lesson 9 Ne555 68 Lesson 10 Slide Switch 72 Lesson 11 How to Drive a DC Motor 76 Lesson 12 Rotary Encoder 81 Lesson 13 Driving LEDs by 74HC595 86 Lesson 14 Driving 7 Segment Display by 74HC595 91 Lesson 15 Driving Dot Matrix by 74HC595 98 Lesson 16 LCD1602 106 Lesson 17 ADXL345 112 ...

Страница 4: ...1 Components List No Name Quantity Component 1 555 Timer IC 1 2 Optocoupler 4N35 2 3 Shift Register 74HC595 2 4 L293D 1 5 Accelerometer ADXL345 1 6 Rotary Encoder 1 ...

Страница 5: ...own 9 Resistor 1kΩ 8 brown black black brown brown 10 Resistor 10kΩ 4 brown black black red brown 11 Resistor 100kΩ 4 brown black black orange brown 12 Resistor 1MΩ 1 brown black green gold 13 Resistor 5 1MΩ 1 green brown green gold 14 Diode Rectifier 4 15 Switch 1 ...

Страница 6: ...3 16 Button 5 17 Power Supply Module 1 18 LCD1602 1 19 Dot Matrix Display 8 8 1 20 7 Segment Display 2 21 DC Motor 1 ...

Страница 7: ...4 22 RGB LED 1 23 LED red 8 24 LED white 4 25 LED green 4 26 LED yellow 4 27 NPN Transistor S8050 2 ...

Страница 8: ...5 28 PNP Transistor S8550 2 29 Capacitor Ceramic 100nF 4 30 Capacitor Ceramic 10nF 4 31 Breadboard 1 32 Active Buzzer 1 33 Relay 1 ...

Страница 9: ...6 34 Fan 1 35 Male to Male Jumper Wire 65 36 Female to Male Dupont Wire 20 37 5 Pin Anti reverse Cable 2 38 9V Battery Buckle 1 39 M3 10 Screw 2 40 M2 5 6 Screw 4 41 M3 6 Screw 6 ...

Страница 10: ...7 42 RAB Holder 1 43 T Extension Board 1 44 40 Pin GPIO Cable 1 Notes After unpacking please check that the number of components is correct and that all components are in good condition ...

Страница 11: ... b and c show two generally used circuit symbols for resistor Normally the resistance is marked on it So if you see these symbols in a circuit it stands for a resistor a b c The resistance can be marked directly in color code and by character The resistors offered in this kit are marked by different colors Namely the bands on the resistor indicate the resistance When using a resistor we need to kn...

Страница 12: ... should be read from left to right The value should be in this format 1st Band 2nd Band 3rd Band x 10 Multiplier Ω and the permissible error is Tolerance So the resistance value of this resistor is 2 red 2 red 0 black x 10 0 black Ω 220 Ω and the permissible error is 1 brown One more example The resistance of the resistor below should be 1 brown 0 black 0 black x 10 1 brown Ω 100x10 Ω 1000 Ω 1KΩ a...

Страница 13: ... Potentiometer usually consists of resistor and movable brush When the brush is moving along the resistor there is a certain resistance or voltage output depending on the displacement Figure i is the potentiometer and figure j is the corresponding circuit symbol The middle pin in figure i represented by the arrow in Fig j is the movable brush i j The functions of the potentiometer in the circuit a...

Страница 14: ...and the cathode with a negative Thus the LED will light up In this kit LEDs of red green yellow and white are provided An LED has two pins The longer one is the anode and shorter one the cathode Pay attention not to connect them inversely There is fixed forward voltage drop in the LED so it cannot be connected with the circuit directly because the supply voltage can outweigh this drop and cause th...

Страница 15: ...ug in the three pins the LED will flash the corresponding color Its circuit symbol is shown as figure n m n An RGB LED has 4 pins the longest one is GND the others are Red Green and Blue Touch its plastic shell and you will find a cut The pin closest to the cut is the first pin marked as Red then GND Green and Blue in turn o Or you can distinguish them in another way As GND is the longest one and ...

Страница 16: ...t Male to Female is because it has the outstanding tip in one end as well as a sunk female end Male to Male means both side are male and Female to Female means both ends are female Male to Female Male to Male Female to Female p q r More than one type of them may be used in a project The colors of the jumper wires are different but it doesn t mean their functions are different accordingly it s just...

Страница 17: ...ough there are holes on the breadboard internally some of them are connected with metal strips Those holes are to insert pins of devices or wires As shown in the fig t below there are four long metal strips on the long sides the blue and red lines are marked just for clear observation But you can take the blue line as the GND and red one as VCC for convenience Every five holes in the middle are ve...

Страница 18: ...imple experiment with the breadboard Turn on an LED as shown in the figure below You can have a try and the LED will light up The breadboard makes it possible for you to plug and pull components at any time without welding which is very convenient for tests v ...

Страница 19: ...s to Raspberry Pi s Console In the subsequent tutorials the console will be used from time to time It is platform for interactions in Linux Therefore before starting the lessons you may need to know how to access to Raspberry Pi s console Using Console in GUI terminal recommended for beginners Using console in GUI is of great help for the beginners You can not only compile and run the code in term...

Страница 20: ...th the keyboard and press Enter and to always boot up to GUI type in sudo raspi config and go through Boot Options Desktop Desk Autologin and reboot Wait for a while and the GUI display will show up as below Note The screen monitor shown above is the 10 1 inch one we re using available on our website www sunfounder com and our Amazon store Check out now and use your Raspberry Pi in a most convenie...

Страница 21: ... need a mouse or you cannot use mouse 2 Connect the monitor to power Then connect it with the Raspberry Pi via the converter cable Connect the Ethernet cable or the USB Wi Fi dongle and keyboard At last connect a 5V 2A DC power to the RPi Power on the screen if needed Then you can see the console full screen If you boot into GUI instead Open a terminal type in sudo raspi config and press enter go ...

Страница 22: ...our supplier For Windows users you may use a ssh tool to log into Raspberry Pi remotely like PuTTY 1 Similarly plug the TF card into the Raspberry Pi RPi power the RPi with a 5V 2A DC power and connect the Ethernet cable Better not a USB Wi Fi dongle Now Now the Raspberry Pi is ready 2 Then you need to know the IP address of the RPi You can find it on the settings interface of your router For more...

Страница 23: ... Port by default it is 22 5 Click Open Note that when you first log in to the Raspberry Pi with the IP address you ll be prompted with a security reminder Just click Yes When the PuTTY window prompts login as type in the user name of the RPi pi and password raspberry the default one if you haven t changed it ...

Страница 24: ...nothing just null but you re in fact is typing things in So just focus on typing it right and press Enter After you log in the RPi successfully the window will display as follows Now no matter what method you take you can get in the console of your Raspberry Pi ...

Страница 25: ...he Raspberry Pi and the breadboard on the RAB Holder first Remove the sticker from the back of the breadboard first and fix the breadboard on the RAB Holder Pay attention to place it in such a position as shown in the figure below so that the 3 3V pin and 5V pin on the T Extension Board align with the bus strips besides the two red lines when we insert the T Extension Board later Then insert the T...

Страница 26: ... be a little difficult to fasten it be careful to operate Pay attention to the direction when plugging in the 40 pin GPIO Cable into the Raspberry Pi pins The triangle sign at the edge should be close to the TF card slot DO NOT connect them inversely or the Raspberry Pi will be short cut ...

Страница 27: ...ons You can apply it like a PC for spreadsheets making word processing and games or to play HD videos of up to 1080p It can t be better describing the Raspberry Pi as though small perfectly formed It has the same ability compared with the PC and carries ports for the USB Ethernet HDMI RCA and 3 5mm stereo jack Moreover it can control GPIOs while the PC cannot Through this kit you will learn how to...

Страница 28: ...in the C language its original name is GPIO 0 In Python code it s 17 BCM or 11 physical So you need to know the name physical wiringPi and BCM of a pin Extension Board The function of the extension board is to lead out pins of the Raspberry Pi to breadboard by GPIO Extension Board to avoid GPIO damage caused by frequent plugging in or out For plugging convenience we designed it in T shape and name...

Страница 29: ...el B 2 model B 3 model B and 3 model B For your better understanding of every pins we have drawn a table for you to know the Name BCM and wiring pi of each pin 26 pin GPIO Extension Board 26 pin GPIO Extension Board and GPIO cable for Raspberry Pi model B and Raspberry Pi model A ...

Страница 30: ...27 Also we have drawn a table of the corresponding BCM wiringPi and Name of each pins ...

Страница 31: ... easily Now the Raspbian system has wiringPi pre installed you can use it directly Test whether wiringPi is installed or not WiringPi includes lots of GPIO commands which enable you to control all kinds of interfaces on Raspberry Pi You can test whether the wiringPi library is installed successfully or not by the following instructions gpio v If the message above appears the wiringPi is installed ...

Страница 32: ... provides a class to control the GPIO on a Raspberry Pi For examples and documents visit http sourceforge net p raspberry gpio python wiki Home Now the Raspbian Jessie 2016 05 27 has RPi GPIO pre instalLED you can use it directly too Test whether RPi GPIO is installed or not Type in python to python CLI In Python CLI Type in import RPi GPIOimport RPi GPIO If no error prompts it means RPi GPIO is i...

Страница 33: ...om the current path Informally here is to go to the path home pi Clone the repository from GitHub git clone https github com sunfounder SunFounder_Super_Kit_V3 0_for_Raspberry_Pi git The advantage of this method is that you can update the latest code any time you want using git pull under the folder Method 2 Download the code Download the source code from github https github com sunfounder SunFoun...

Страница 34: ...Pin Cable 1 LED 1 Resistor 220Ω Jumper wires Principle In this experiment connect a 220Ω resistor to the anode the long pin of the LED then the resistor to 3 3 V and connect the cathode the short pin of the LED to B17 of Raspberry Pi We can see from the schematic diagram that the anode of LED connects to a current limiting resistor and then to 3 3V Therefore to turn on an LED we need to make B17 l...

Страница 35: ...01_blinkLed c In the lessons later we will show how to get into the folder of the code in command way not with the display You only need to find out the code file and right click Open in Terminal You can back to lesson 1 to check if you forgot Certainly the terminal can be opened if you re using display and then use cd command directly to go to the code path If you log into the Raspberry Pi remote...

Страница 36: ...ke the process quicker and more convenient make 01_blinkLed Note The make command will compile according to the rules in the Makefile Two files will be generated after compiling o and an executable file We use makefile in essence is to write the compilation method of gcc into the automated script If you have written your own program in C language you need to write and modify the makefile so as to ...

Страница 37: ...w the code 01_blinkLed c press Ctrl C to stop running the code Then type the following command to open it nano 01_blinkLed c Note nano is a text editor tool The command is to open the code file 01_edblinkLed c by this tool ...

Страница 38: ...UTPUT Set LedPin as output to write value to it digitalWrite LedPin LOW Set GPIO0 as 0V low level Since the cathode of LED is connected to GPIO0 thus the LED will light up if GPIO0 is set low On the contrary set GPIO0 as high level digitalWrite LedPin HIGH LED will go out Press Ctrl X to exit If you have modified the code there will be a prompt asking whether to save the changes or not Type in Y s...

Страница 39: ... F5 you need to save it first Then you can see the results If you want to log into the Raspberry Pi remotely type in the command cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi Python Run the code sudo python 01_blinkLed py Note Here sudo superuser do and python means to run the file by Python If you want to view the code 01_blinkLed py press Ctrl C to stop running the code Then type the fol...

Страница 40: ...mport RPI GPIO package thus python code control GPIO easily with it import time import time package for time delay function in the following program LedPin 17 LED connects to the B17 of the T shape extension board namely the GPIO 0 of the Raspberry Pi Define a setup function for some setup def setup GPIO setmode GPIO BCM Set the GPIO modes to BCM Numbering Set LedPin s mode to output and initial l...

Страница 41: ...g after the script finished def destroy Turn off LED GPIO output LedPin GPIO HIGH Release resource GPIO cleanup If run this script directly do if __name__ __main__ setup try main When Ctrl C is pressed the child program destroy will be executed except KeyboardInterrupt destroy Press Ctrl X to exit If you have modified the code there will be a prompt asking whether to save the changes or not Type i...

Страница 42: ...kages many low level detail designs which ease your way to explore your own apps Maybe that is the charm of Raspberry Pi Now you have already learnt how to use the Raspberry Pi GPIO to blink an LED Keep moving to the next contents FAQ If you haven t modified the code you do not need to run make 01_blinkLed again make 01_blinkLed Or a message will appear make 01_blinkLed is up to date It will not a...

Страница 43: ...nic devices They are usually used as switches to connect or disconnect circuits Although buttons come in a variety of sizes and shapes the one used here is a 6mm mini button as shown in the following pictures Pin 1 is connected to pin 2 and pin 3 to pin 4 So you just need to connect either of pin 1 and pin 2 to pin 3 or pin 4 The following is the internal structure of a button Since the pin 1 is c...

Страница 44: ...e B18 turns into low level it means the button is pressed You can run the corresponding code when the button is pressed and then the LED will light up Note The longer pin of the LED is the anode and the shorter one is the cathode Experimental Procedures Step 1 Build the circuit For C language users Step 2 Open the code file cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi C Note Change direct...

Страница 45: ...n PUD_UP Set the ButtonPin as pull up input When the button is not pressed the I O port is 3 3V When the button is pressed the I O port connects to GND OV You can judge the button status by reading the level value of the I O port while 1 indicate that button has pressed down if digitalRead ButtonPin 0 LED on digitalWrite LedPin LOW printf LED on n else LED off digitalWrite LedPin HIGH printf LED o...

Страница 46: ... to swled GPIO add_event_detect BtnPin GPIO FALLING callback swLED Define a callback function for button callback execute the function after the callback of the interrupt def swLed ev None global Led_status Switch Led status on off off on Led_status not Led_status GPIO output LedPin Led_status if Led_status print LED OFF else print LED ON Now press the button and the LED will light up press the bu...

Страница 47: ...in Cable Principle Principle Judging from the schematic diagram we can know that a LED and a current limiting resistor have been connected to B17 B18 B27 B22 B23 B24 B25 and B4 respectively The current limiting resistor has been connected to the 3 3V power supply on other side Therefore if we want to light up one LED we only need to set the GPIO of the LED as low level So in this experiment set B1...

Страница 48: ... the cd command to switch to the code path of this experiment Step 3 Compile the Code gcc 03_8Led c o 03_8Led lwiringPi or make 03_8Led Note gcc is a linux command which can realize compiling and generating the C language program file 03_8Led c to the executable file 03_8Led make is a linux command which can compiling and generating the executable file according to the rule inside the makefile Ste...

Страница 49: ...spectively corresponding to 0 1 2 3 4 5 6 7 It is to set the 8 LEDs as output separately Use for loop to make it more concise and efficient for i 0 i 8 i make LED on from left to right Led_on i turn the LED i on delay 100 keep the LED i lighting for 100ms Led_off i Turn the LED i off Light up and turn off the LEDs in GPIO0 7 successively i increases progressively from 0 to 7 LED0 to LED7 changes a...

Страница 50: ...int out the status of the 8 LEDs for pin in LedPins Assign the element in pins list to pin variable one by one In GPIO setup pin GPIO OUT set the pins in list as output one by one GPIO output pin GPIO LOW Set each element in the pins list as low level to light up the LEDs leds LedPins index pin 0 Show which LED is on time sleep 0 1 wait for 0 1s GPIO output pin GPIO HIGH After delaying set it as h...

Страница 51: ...n versus the time that the signal spends off The duration of on time is called pulse width To get varying analog values you change or modulate that width If you repeat this on off pattern fast enough with some device an LED for example the result would be like this the signal is a steady voltage between 0 and 3 3v controlling the brightness of the LED Duty Cycle A duty cycle is the percentage of o...

Страница 52: ...is technology to make the LED brighten and dim slowly so it looks like our breath Experimental Procedures Step 1 Build the circuit For C language users Step 2 Open the code file cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi C ...

Страница 53: ... 2 the value of pwm minus 1 every 2ms when the value of pwm decreases the luminance of the LED decreases For Python users Step 2 Open the code file cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi Python Step 3 Run sudo python 04_breathLed py Code Explanation GPIO setup LedPin GPIO OUT initial GPIO LOW Set LedPin as OUTPUT initialize the pin as low level pLED GPIO PWM LedPin 1000 use PWM in t...

Страница 54: ... time sleep 1 decrease duty cycle from 100 to 0 for dc in range 100 1 step the luminance of the LED decreases with each cycle Change duty cycle to dc pLED ChangeDutyCycle dc same as the last for loop print Duty cycle s dc time sleep delay Now you will see the gradual change of the LED luminance between bright and dim Summary Through this experiment you should have mastered the principle of PWM and...

Страница 55: ...D in Components Introduction The three primary colors of the RGB LED can be mixed into various colors by brightness The brightness of LED can be adjusted with PWM Raspberry Pi has only one channel for hardware PWM output but it needs three channels to control the RGB LED which means it is difficult to control the RGB LED with the hardware PWM of Raspberry Pi Fortunately the softPwm library simulat...

Страница 56: ...rs to one pin 0 is the minimum value and 100 is the maximum as a percentage The function is to use software to create a PWM pin set its value between 0 100 softPwmCreate LedPinRed 0 100 softPwmCreate LedPinGreen 0 100 softPwmCreate LedPinBlue 0 100 void ledColorSet uchar r_val uchar g_val uchar b_val This function is to set the colors of the LED Using RGB the formal parameter r_val represents the ...

Страница 57: ...FF Set pins channels with dictionary pins Red 17 Green 18 Blue 27 p_R GPIO PWM pins Red 2000 the same as the last lesson here we configure the channels and frequencies of the 3 PWM p_G GPIO PWM pins Green 2000 p_B GPIO PWM pins Blue 2000 p_R start 0 the same as the last lesson the PWM of the 3 LEDs begin with 0 p_G start 0 p_B start 0 Define a MAP function for mapping values Like from 0 255 to 0 1...

Страница 58: ... luminance p_G ChangeDutyCycle G_val p_B ChangeDutyCycle B_val for color in COLOR Assign every item in the COLOR list to the color respectively and change the color of the RGB LED via the setColor function setColor color change the color of the RGB LED time sleep 0 5 set delay for 0 5s after each color changing Modify this parameter will changed the LED s color changing rate Here you should see th...

Страница 59: ...rn the pins of two buzzers face up and the one with a green circuit board is a passive buzzer while the other enclosed with a black tape is an active one The difference between an active buzzer and a passive buzzer is An active buzzer has a built in oscillating source so it will make sounds when electrified But a passive buzzer does not have such source so it will not beep if DC signals are used i...

Страница 60: ...ntense majority carriers is the emitter region when the other one is the collector region This composition enables the transistor to be an amplifier From these three regions three poles are generated respectively which are base b emitter e and collector c They form two P N junctions namely the emitter junction and collection junction The arrow in the circuit symbol indicates the direction of emitt...

Страница 61: ...on and the buzzer will make sounds But when high level is supplied to the IO of Raspberry Pi the transistor will be cut off and the buzzer will not make sounds Experimental Procedures Step 1 Build the circuit Pay attention to poles of the buzzer The one with label is the positive pole and the other is the negative For C language users Step 2 Open the code file cd home pi SunFounder_Super_Kit_V3 0_...

Страница 62: ...der_Super_Kit_V3 0_for_Raspberry_Pi Python Step 3 Run sudo python 06_beep py Code Explanation GPIO output BeepPin GPIO LOW Set the buzzer pin as low level time sleep 0 1 Wait for 0 1 second Change the switching frequency by changing this parameter Note Not the sound frequency Active Buzzer cannot change sound frequency GPIO output BeepPin GPIO HIGH close the buzzer time sleep 0 1 Now you should he...

Страница 63: ...per wires 1 T Extension Board 1 40 Pin GPIO Cable Principle Relay There are 5 parts in every relay 1 Electromagnet It consists of an iron core wounded by coil of wires When electricity is passed through it becomes magnetic Therefore it is calLED electromagnet 2 Armature The movable magnetic strip is known as armature When current flows through them the coil is it energized thus producing a magneti...

Страница 64: ...ntrol coil as a result the electromagnet starts energizing Then the armature is attracted to the coil pulling down the moving contact together thus connecting with the normally open contacts So the circuit with the load is energized Then breaking the circuit would a similar case as the moving contact will be pulled up to the normally closed contacts under the force of the spring In this way the sw...

Страница 65: ...e the transistor The coil of the relay is powered and generate electromagnetic force and the relay closes digitalWrite relayPin HIGH Set the I O port as high level 5V thus the transistor is not energized and the coil is not powered There is no electromagnetic force so the relay opens For Python users Step 2 Open the code file cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi Python Step 3 Run ...

Страница 66: ... under high frequency using GPIO output relayPin GPIO HIGH Set the pins of the transistor as low level to let the relay open time sleep 1 Now connect a device of high voltage and the relay will close and the LED will light up connect one of low voltage and it will open and the LED will go out In addition you can hear a ticktock caused by breaking normally close contact and closing normally open co...

Страница 67: ...ion meaning the input terminal and the output one are isolated electrically this process is also be known as electrical isolation Components 1 Raspberry Pi 1 4N35 1 LED 1 220 Ohm Resistor 1 1k Ohm Resistor Some jump wires 1 T Extension Board 1 40 Pin GPIO Cable Principle 4N35 The 4N35 is an optocoupler for general purpose application It consists of gallium arsenide infrared LED and a silicon NPN p...

Страница 68: ...ion The schematic diagram Principle In this experiment use an LED as the load connected to the NPN phototransistor Connect pin 2 of 4N35 to pin B17 pin 1 connects a 1K current limiting resistor and then a 3 3V Connect pin 4 to GND and pin 5 to the cathode of the LED Then hook the anode of the LED to 3 3V after connecting with a 220 Ohm resistor When in program a LOW level is given to pin B17 the i...

Страница 69: ...nergized and the pin connected to LED cannot conduct to the 0V Then the LED goes out For Python users Step 2 Open the code file cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi Python Step 3 Run sudo python 08_4N35 py Code Explanation GPIO output Pin_4N35 GPIO LOW set the pins of optocoupler as low level thus the optocoupler is energized and the pin connected to LED conducts to the 0V Then th...

Страница 70: ...tocoupler that usually used for driving relay as well as motor circuits As there is no direct connection between the input and output even if a short circuit at the output end occurs the control board will not be burnt Have a try ...

Страница 71: ...4 ceramic capacitor 1 Potentiometer 50KΩ 1 Resistor 10KΩ 1 USB cable Jumper wires 1 T Extension Board 1 40 Pin GPIO Cable Principle The 555 IC was originally used as a timer hence the name 555 time base circuit It is now widely used in various electronic products because of its reliability convenience and low price The 555 is a complex hybrid circuit with dozens of components such as a divider com...

Страница 72: ...in 7 DISCHARGE having two states of suspension and ground connection also decided by input and the output of the internal discharge tube Pin 8 VCC the power supply The schematic diagram Experimental Procedures Step 1 Build the circuit For C language users Step 2 Go to the folder of the code cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi C Step 3 Compile make 09_ne555 ...

Страница 73: ... is falling edge for Pin 0 When the interrupt happens execute the function exInt0_ISR and the pulse count will add 1 while 1 if no interrupt happens the pulse count will stay and just print it printf Current pulse number is d n globalCounter For Python users Step 2 Get into the folder of the code cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi Python Step 3 Run sudo python 09_ne555 py Now yo...

Страница 74: ...ction will change the value of the global variable g_count thus here we add global before it g_count 1 GPIO add_event_detect SigPin GPIO RISING callback count set an interrupt here and the interrupt signal is a rising edge for Pin Sig It will run the function count accordingly while True wait for the interrupt print g_count d g_count print the information time sleep 0 001 ...

Страница 75: ...Slide Switch A slide switch just as its name implies is to slide the switch bar to connect or break the circuit and further switch circuits The common used types are SPDT SPTT DPDT DPTT etc The Slide Switch is commonly used in low voltage circuit It features flexibility and stability and widely applies in electric instruments and electric toys How it works Use the middle pin as the fixed one When ...

Страница 76: ... Then when you pull the slide you can see the two LEDs light up alternately Experimental Procedures Step 1 Build the circuit For C language users Step 2 Go to the folder of the code cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi C Step 3 Compile make 10_slideSwitch Step 4 Run the executable file above ...

Страница 77: ...tf LED2 on n For Python users Step 2 Get into the folder of the code cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi Python Step 3 Run sudo python 10_slideSwitch py Code Explanation When the slide is pulled to the left the middle pin and left one are connected the Raspberry Pi reads a high level at the middle pin so the LED1 is on and LED2 off if GPIO input slidePin 1 print LED1 ON High Valu...

Страница 78: ...75 Now pull the slide and you can see the two LEDs light up alternately ...

Страница 79: ...c level and drive inductive loads such as relay coils DC stepping motors and power switching transistors etc DC Motors are devices that turn DC electrical energy into mechanical energy They are widely used in electrical drive for their superior speed regulation performance See the figure of pins below L293D has two pins Vcc1 and Vcc2 for power supply Vcc2 is used to supply power for the motor whil...

Страница 80: ...nce you can weld the pins to it Size 25 20 15MM Operation Voltage 1 6V Free run current 3V 70m A Free run speed 3V 13000RPM Stall current 3V 800mA Shaft diameter 2mm Power Supply Module In this experiment it needs large currents to drive the motor especially when it starts and stops which will severely interfere with the normal work of Raspberry Pi Therefore we separately supply power for the moto...

Страница 81: ...f 5V Connect pin 1 of L293D to B27 and set it as high level Connect pin2 to B18 and pin7 to B27 then set one pin high while the other low Thus you can change the motor s rotation direction Experimental Procedures Step 1 Build the circuit Since the power supply module and T cable are incompatible we will not use the T Cable in this experiment ...

Страница 82: ...n 1 is in high level 2Y will output high level digitalWrite MotorPin2 LOW Set a low level for 1A then 1Y will output low level and the motor will rotate for i 0 i 3 i delay 1000 this loop is to delay for 3 1000ms digitalWrite MotorEnable LOW If 1 2EN pin1 is in low level L293D does not work Motor stops rotating digitalWrite MotorPin1 LOW digitalWrite MotorPin2 HIGH Reverse the current flow of the ...

Страница 83: ... pin2 in low level GPIO output MotorPin2 GPIO LOW Make the motor rotate clockwise time sleep 5 rotate for 5 seconds GPIO output MotorEnable GPIO LOW Stop the motor time sleep 5 wait for 5 seconds Code for motor counter clockwise rotation is similar to sketch above Now you should see the motor blade rotating Further Exploration You can use buttons to control the clockwise and counterclockwise rotat...

Страница 84: ... anti reverse cable 1 T Extension Board 1 40 Pin GPIO Cable Experimental Principle A rotary encoder is an electronic switch with a set of regular pulses with strictly timing sequence When used with IC it can achieve increment decrement page turning and other operations such as mouse scrolling menu selection and so on There are mainly two types of rotary encoders absolute and incremental relative e...

Страница 85: ...l B converts from low level to high it indicates the Rotary Encoder has spun counter clockwise CCW Thus when channel A is in low level we can know the direction that Rotary Encoder spun by channel B The schematic diagram of the Rotary Encoder is shown as below We can see that pin 3 on the Rotary Encoder is CLK of the module while pin 5 is DT Then we can know the Rotary s rotating direction by the ...

Страница 86: ...in 1 DT connects to GPIO1 define B18 as 1 in wiring Pi define SWPin 2 SW connects to GPIO2 void rotaryDeal void Pi detects the pulse when spinning the rotary encoder and judge the spinning direction then increase or decrease the value of globalCounter to record the angular displacement Last_RoB_Status digitalRead RoBPin Read the value of DT while digitalRead RoAPin If CLK is low run the program be...

Страница 87: ...tep 3 Run sudo python 12_rotaryEncoder py Code Explanation globalCounter 0 Set a global variable to count flag 0 Set a flag for reverse spinning Last_RoB_Status 0 Set a variable to store the previous state of pinB Current_RoB_Status 0 Set a variable to store the present state of pinB Define a function to deal with rotary encoder def rotaryDeal global counter global Last_RoB_Status Current_RoB_Stat...

Страница 88: ... counter Define a callback function on switch to clean counter def clear ev None global counter counter 0 Now gently rotate the encoder to change the value of the variable in the above program and you will see the value printed on the screen Rotate the encoder clockwise the value will increase or rotate it counterclockwise the value will decrease ...

Страница 89: ...595 is widely used to indicate multipath LEDs and drive multi bit segment displays Three state mentioned above refers to the fact that you can set the output pins as either high low or high impedance With data latching the instant output will not be affected during the shifting with data output you can cascade 74HC595s more easily Compatible with low voltage TTL circuit 74HC595 can transform seria...

Страница 90: ... low level here connected to GND to keep 74HC595 in output enable state DS Serial data input pin VCC Positive supply voltage GND Ground The schematic diagram is shown as below Principle In this experiment connect 74HC595 s ST_CP to Raspberry Pi s B18 SH_CP to B27 and DS to B17 connect a current limit resistor and then a LED to Q0 Q7 respectively connect MR and VCC to 5V CE and GND to GND Input dat...

Страница 91: ... 13_74HC595_LED Code Explanation unsigned char LED 8 0x01 0x02 0x04 0x08 0x10 0x20 0x40 0x80 This array is to store the output values of Q0 Q7 For example 0x01 in binary format is 0000 0001 thus Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 are 0 0 0 0 0 0 0 1 respectively that is Q0 1 and the LED connected to Q0 will light up Thus we can light up the eight LEDs separately in this way void pulse int pin generate a risi...

Страница 92: ... sequence that is to light up the LEDs connected to Q0 Q7 in turn Sketch in later part not explained here is to light up 8 LEDs together and dim them then light up LEDs connected to Q7 Q0 one by one and all 8 LEDs light up dim in the end Thus a cycle completes You can observe the LEDs state For Python users Step 2 Get into the folder of the code cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_P...

Страница 93: ...ode list hc595_shift onoff leds mode index onoff 1 Show which led is on print leds time sleep sleeptime leds mode index onoff Show the led is off for loops in later part work similarly lighting up LED by list Input a 2 bit hexadecimal parameter dat via hc595_in dat to control 8 LEDs state and hc595_out will output state to 8 LEDs In While True the for loop will shift the LED blinking list to the h...

Страница 94: ...used within the same package thus allowing the indication of a decimal point DP when two or more 7 segment displays are connected together to display numbers greater than ten Each of the LEDs in the display is given a positional segment with one of its connection pins led out from the rectangular plastic package These LED pins are labeled from a through to g representing each individual LED The ot...

Страница 95: ...al via a current limiting resistor to forward bias the anode of the segment Common Anode 7 Segment Display In a common anode display the anodes of all the LED segments are connected to the logic 1 Then an individual segment a g is energized by a ground logic 0 or LOW signal via a current limiting resistor to the cathode of the segment In this experiment a common cathode 7 segment display is use It...

Страница 96: ...shift register when SH_CP the clock input of the shift register is at the rising edge and to the memory register when ST_CP the clock input of the memory is at the rising edge Then you can control the states of SH_CP and ST_CP via the Raspberry Pi GPIOs to transform serial data input into parallel data output so as to save Raspberry Pi GPIOs and drive the display Experimental Procedures Step 1 Bui...

Страница 97: ...bits Here we assume dat 0x3f 0011 1111 when i 0 0x3f will shift right 0 bits 0x3f 0x80 1000 0000 digitalWrite SRCLK 1 SH_CP will convert from low to high and generate a rising edge pulse then shift the DS date to shift register delay 1 digitalWrite SRCLK 0 to assign 8 bit value to 74HC595 s shift register digitalWrite RCLK 1 ST_CP converts from low to high and generate a rising edge then shift dat...

Страница 98: ...RCLK GPIO HIGH time sleep 0 001 GPIO output RCLK GPIO LOW for code in segCode Input item in segCode list to hc595_shift function to display the character hc595_shift code If you want to display a number use the hc595_shift function segCode list and decimal value x in the sketch hc595_shift segCode x x is a number needs to be displayed ranging from 0 15 and it will be coverted and displayed by 0 F ...

Страница 99: ...en the interrupt happens flag 1 flag represents the state of the button if wiringPiISR TouchPin INT_EDGE_FALLING randomISR Set an interrupt here as the falling edge for TouchPin When the interrupt happens execute the function randomISR printf Unable to setup ISR s n strerror errno return 1 srand time NULL num rand 6 Two functions here One is the srand function which is used before calling function...

Страница 100: ...hPin GPIO RISING callback randomISR bouncetime 20 Set an interrupt and the rising edge for TouchPin When the interrupt happens execute the function randomISR Set bouncetime for button to 20ms def randomISR channel Interrupt calling the function global flag flag 1 num random randint 1 6 Generate a random number from 1 6 hc595_shift SegCode num 1 Output the hexadecimal values in list by 74HC595 Now ...

Страница 101: ...es 1 T Extension Board 1 40 Pin GPIO Cable Principle Dot Matrix Generally dot matrix can be categorized into two types common cathode CC and common anode CA They look much alike but internally the difference lies You can tell by test A CA one is used in this kit You can see 788BS labeled at the side See the figure below The pins are arranged at the two ends at the back Take the label side for refe...

Страница 102: ...3 as Low and ROW 9 14 8 12 1 7 2 and 5 as High when for CC set pin 13 as High and ROW 9 14 8 12 1 7 2 and 5 as Low Consider the following figures for better understanding The internal view Common Anode Common Cathode Pin numbering corresponding to the above rows and columns COL 1 2 3 4 5 6 7 8 Pin No 13 3 4 10 6 11 15 16 ROW 1 2 3 4 5 6 7 8 Pin No 9 14 8 12 1 7 2 5 In this experiment a CA dot matr...

Страница 103: ...nect the 5V and GND of the T Cobbler to holes on the two sides of the board then hook up pin16 and 10 of the two 74HC595 chips to VCC and pin 13 respectively and pin 8 to GND Step 2 Connect pin 11 of the two 74HC595 together and then to GPIO27 then pin 12 of the two chips and to GPIO18 next pin 14 of the 74HC595 on the left side to GPIO17 and pin 9 to pin 14 of the other 74HC595 ...

Страница 104: ...C595 are mapped with pin 13 3 4 10 6 11 15 and 16 respectively COL 1 2 3 4 5 6 7 8 Pin No 13 3 4 10 6 11 15 16 Step 4 Now connect the ROWs of the dot matrix The 74HC595 on the left controls ROW of the matrix See the table below for the mapping We can see Q0 Q7 of the 74HC595 on the left are mapped with pin 9 14 8 12 1 7 2 and 5 respectively ROW 1 2 3 4 5 6 7 8 Pin No 9 14 8 12 1 7 2 5 ...

Страница 105: ...code cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi C Step 3 Compile make 15_dotMatrix Step 4 Run sudo 15_dotMatrix Code Explanation void hc595_in unsigned char dat Write an 8 bit data to the shift register of the 74HC595 int i for i 0 i 8 i digitalWrite SDI 0x80 dat i Write the value of dat to pin SDI of the HC595 bit by bit digitalWrite SRCLK 1 Everytime SRCLK adds one the shift register ...

Страница 106: ...nd COL table for the matrix decreases by 1 each time hc595_in code_L i Write to the first data of the Row table hc595_in code_H i Write to the first data of the COL table and the ROW data previously goes to the other HC595 hc595_out Update the output of the 74HC595 output the data controlled by both two HC595 and the dot matrix will show the pattern delay 100 For Python users Step 2 Get into the f...

Страница 107: ...l_buffer col_num 0x01 for Common Anode type matrix when row is High and column is low the LED will light up matrix_msg row_num col_num 1 To turn on an LED at a certain row and column assign 1 to the corresponding elements in the 2D array print_matrix matrix_msg Print the 2D array on the CLI matrix_msg 0 for i in range max_row for i in range max_col Reset the array after one print def hc595_shift d...

Страница 108: ... 1 1 Assign elements of the column table in inverse order hc595_shift code_L i hc595_shift code_H i get_matrix code_L i code_H i time sleep 0 1 You should see LEDs light up as you control Summary Through this lesson you have got the basic principle of LED dot matrix and how to program the Raspberry Pi to drive an LED dot matrix based on 74HC595 cascade With the knowledge learnt try more fascinatin...

Страница 109: ...used here for better application LCD1602 uses the standard 16 pin port among which Pin 1 GND connected to Ground Pin 2 Vcc connected to 5V power supply Pin 3 Vo used to adjust the contrast of LCD1602 the level is lowest when it s connected to a positive power supply and highest when connected to ground you can connect a 10K potentiometer to adjust its contrast when using LCD1602 Pin 4 RS register ...

Страница 110: ...1602 has two operation modes 4 bit and 8 bit When the IOs of the MCU are insufficient you can choose the 4 bit mode under which only pins D4 D7 are used After connecting the circuit you can operate LCD1602 by the Raspberry Pi Experimental Procedures Step 1 Build the circuit please be sure the pins are connected correctly Otherwise characters will not be displayed properly LCD1602 T Extension Board...

Страница 111: ...older of code cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi C Step 3 Compile make 16_lcd1602 Step 4 Run sudo 16_lcd1602 Code Explanation include lcd h includes the lcd library containing some functions for the LCD1602 display for convenient use const unsigned char Buf SUNFOUNDER An array to store the characters to be displayed on the LCD1602 const unsigned char myBuf sunfounder com Another...

Страница 112: ...ace the cursor at the first row and moves left to right from the first character lcdPutchar fd myBuf i myBuf i is a pointer that points to contents in the myBuf array and output the pointed data to lcd delay 100 for i 0 i sizeof Buf 1 i lcdPosition fd i 1 Place the cursor at the second row moves from the first character lcdPutchar fd Buf i A pointer that points to data in the Buf array output it t...

Страница 113: ...self displaymode self LCD_ENTRYLEFT self LCD_ENTRYSHIFTDECREMENT self write4bits self LCD_ENTRYMODESET self displaymode Set the entry mode def begin self cols lines Start the LCD def setCursor self col row Set the cursor location def message self text Send strings to the LCD The new line wraps to the second line def destroy self Clean up the used gpio lcd LCD 0 2 Create an lcd object lcd clear Cle...

Страница 114: ...111 Further Exploration In this experiment the LCD1602 is driven in the 4 bit mode You can try programming by yourself to drive it in the 8 bit mode ...

Страница 115: ... or 4 wire or I2C digital interface The ADXL345 is well suited to measure the static acceleration of gravity in tilt sensing applications as well as dynamic acceleration resulting from motion or shock Its high resolution 4 mg LSB enables the inclination change measurement by less than 1 0 And the excellent sensitivity 3 9mg LSB 2g provides a high precision output of up to 16g In this experiment I2...

Страница 116: ...en output and gravity direction Pin Function of ADXL345 Module Name Description VS Supply Voltage CS Chip Select I2C mode is enabled if it s tie high to VDD I O VDD I O 1 8V SDO Serial Data Out alternate I2C address select INT1 Interrupt 1 Output INT2 Interrupt 2 Output 3 3V 3 3V SDA Serial Data I2C Serial Data In SPI 4 Wire Serial Data In Out SPI 3 Wire SCL Serial Communications Clock GND GND ...

Страница 117: ...he following program Before running the program please make sure the I2C driver module of Raspberry Pi has loaded normally For C language users Step 2 Get into the folder of code cd home pi SunFounder_Super_Kit_V3 0_for_Raspberry_Pi C Step 3 Compile the Code make 17_adxl345 Step 4 Run ...

Страница 118: ... a struct function returning a struct value char x0 y0 z0 x1 y1 z1 struct acc_dat acc_xyz x0 0xff wiringPiI2CReadReg8 fd 0x32 These read an 8 or 16 bit value from the device register indicated x1 0xff wiringPiI2CReadReg8 fd 0x33 Read an 8 bit data from the 0x33 register of the I2C device fd assign to x1 y0 0xff wiringPiI2CReadReg8 fd 0x34 y1 0xff wiringPiI2CReadReg8 fd 0x35 z0 0xff wiringPiI2CRead...

Страница 119: ...lanation class ADXL345 I2C Define a class ADXL345 and the class inheritance is I2C def __init__ self busnum 1 debug False The initialize function of the class which is run when an instance is created of the class def setRange self range Read the data format register to preserve bits Update the data rate make sure that the FULL RES bit is enabled for range scaling def getRange self Read an 8 bit da...

Страница 120: ...hould see the values printed on the screen change Copyright Notice All contents including but not limited to texts images and code in this manual are owned by the SunFounder Company You should only use it for personal study investigation enjoyment or other non commercial or nonprofit purposes under the related regulations and copyrights laws without infringing the legal rights of the author and re...

Отзывы: