[go: up one dir, main page]

0% found this document useful (0 votes)
138 views42 pages

Freenove Starter Kit For Raspberry Pi 4B

Uploaded by

bendy83.eliott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views42 pages

Freenove Starter Kit For Raspberry Pi 4B

Uploaded by

bendy83.eliott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Getting Started

Thank you for choosing Freenove products!


This tutorial has two parts.
The part 1 is for Freenove Raspberry Pi starter or case kit. It will introduce how to assemble case and setup
raspberry Pi.
The part 2 is for Raspberry Pi projects. If you have electronic components, you can learn our tutorial.

Get Support and Offer Input

Freenove provides free and responsive product and technical support, including but not limited to:
 Product quality issues
 Product use and build issues
 Questions regarding the technology employed in our products for learning and education
 Your input and opinions are always welcome
 We also encourage your ideas and suggestions for new products and product improvements
For any of the above, you may send us an email to:

support@freenove.com
Safety and Precautions

Please follow the following safety precautions when using or storing this product:
 Keep this product out of the reach of children under 6 years old.
 This product should be used only when there is adult supervision present as young children lack
necessary judgment regarding safety and the consequences of product misuse.
 This product contains small parts and parts, which are sharp. This product contains electrically conductive
parts. Use caution with electrically conductive parts near or around power supplies, batteries and
powered (live) circuits.
 When the product is turned ON, activated or tested, some parts will move or rotate. To avoid injuries to
hands and fingers, keep them away from any moving parts!
 It is possible that an improperly connected or shorted circuit may cause overheating. Should this happen,
immediately disconnect the power supply or remove the batteries and do not touch anything until it
cools down! When everything is safe and cool, review the product tutorial to identify the cause.
 Only operate the product in accordance with the instructions and guidelines of this tutorial, otherwise
parts may be damaged or you could be injured.
 Store the product in a cool dry place and avoid exposing the product to direct sunlight.
 After use, always turn the power OFF and remove or unplug the batteries before storing.
About Freenove

Freenove provides open source electronic products and services worldwide.

Freenove is committed to assist customers in their education of robotics, programming and electronic circuits
so that they may transform their creative ideas into prototypes and new and innovative products. To this end,
our services include but are not limited to:

 Educational and Entertaining Project Kits for Robots, Smart Cars and Drones
 Educational Kits to Learn Robotic Software Systems for Arduino, Raspberry Pi and micro: bit
 Electronic Component Assortments, Electronic Modules and Specialized Tools
 Product Development and Customization Services

You can find more about Freenove and get our latest news and updates through our website:

http://www.freenove.com

Copyright

All the files, materials and instructional guides provided are released under Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 Unported License. A copy of this license can be found in the folder containing
the Tutorial and software files associated with this product.

This means you can use these resource in your own derived works, in part or completely, but NOT for the
intent or purpose of commercial use.

Freenove brand and logo are copyright of Freenove Creative Technology Co., Ltd. and cannot be used without
written permission.


R

Free your innovation

Raspberry Pi® is a trademark of Raspberry Pi Foundation (https://www.raspberrypi.org/).


█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 3

Contents

Getting Started .......................................................................................... 1


Safety and Precautions .................................................................................... 1
About Freenove ................................................................................................. 2
Copyright ............................................................................................................. 2
Contents ...................................................................................................... 3
Part 1 Assemble Case and Set Up Raspberry Pi ............................. 4
List .......................................................................................................................... 5
Assemble Case ................................................................................................... 7
Raspberry Pi ......................................................................................................10
Install the System.............................................................................................11
Remote desktop & VNC ................................................................................19
What’s Next? .....................................................................................................30
Part 2 Raspberry Pi Projects............................................................... 31
Contents of Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi ...........32
Chapter 1 LED (Sample) ................................................................................34

support@freenove.com █
4 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Part 1 Assemble Case and Set Up Raspberry Pi

This part is for Freenove Starter Kit for Raspberry Pi and Freenove
Case Kit for Raspberry Pi. It will introduce how to assemble the case
and setup your Raspberry Pi to get it work.

You can get the products via below:


http://www.freenove.com/store.html

If you have any concerns, please feel free to contact us:


support@freenove.com

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 5

List

Acrylic and electronic parts

Acrylic

32 GB Samsung EVO (not contained in case kit) Card Reader (not contained in case kit)

5V/3A Power Adapter (you get only one of below, which is applied in your country) (not contained in
case kit)

support@freenove.com █
6 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

MINI-HDMI Cable (not contained in case kit) USB-Type-C cable 10cm

Power Fan board USB-Type-C cable 100cm (not contained in


case kit V1, contained in V2)

Fan & Screws and nuts for Fan Heat sink

Mechanical Parts

Screwdriver

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 7

Assemble Case

Note this case supports Raspberry Pi 4 B. It can be used for 3B and 3B + when without the side acryic board
for power port.
Step 1, paste heat sinks
Before After

Don’t make it contact


Tear the cover of sink and paste on Raspberry Pi
other components around.

The two small heat sinks are not necessary.

Step 2, assemble the case


Install one side of RPi. Bottom view.

Nut M2.5
Screw
Screw M2.5*10
M2.5*10

Acrylic block

These parts are spare:

support@freenove.com █
8 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Install another side of RPi. Raspberry pi is install to bottom board successfully.


Acrylic
cushion block

Screw
M2.5*10

Install sideboard. After installation.

Install standoff to power board. Connect fan cable to power board.

Power switch Fan knob

Screw
Standoff
M2.5*6
M2.5*8

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 9

Install power board to top acrylic board. Install fan to top Acrylic board.
1, Don’t reverse fan. Plug 4 screws first. Then install Nuts.
2, If you are using other heat sink, make sure
Screw
your heat sink is shorter than ours.
M3*16
Or it will influence fan.

Screw
M2.5*6

Nut M3

Install top acrylic borad. Install standoff to top acrylic board.


Screw
M2.5*6

Standoffs
M2.5*25

Install standoff to bottom acrylic board. Next, you need to connect cables as below.

Screw
M2.5*6

If you have any concerns (like broken parts or loud fan), please contact us:

support@freenove.com

support@freenove.com █
10 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Raspberry Pi

Raspberry Pi (RPi, RPI, RasPi, these words will be used later), is a micro-computer with size of a card. When
you learn it. You can take it as a computer. It is widely used in desktop workstation, media center, smart home,
robots, and even the servers, etc.

So far, Raspberry Pi has developed to the fourth generation. Changes in versions are accompanied by increase
and upgrades in hardware. A type and B type, the first generation of products, have been stopped due to
various reasons. Other versions are popular and active and the most important is that they are consistent in
the order and number of pins, which makes the compatibility of peripheral devices greatly enhanced between
different versions.

Hardware interface diagram of RPi 4B is shown below:

GPIO
Connector Ethernet
Connector

Display
Connector

USB
Power Connector x4
Connector

MINI HDMI Camera Audio


Connector x2 Connector Connector

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 11

Install the System

Firstly, install a system for your RPi.

Component List

Necessary Components for this case


Any Raspberry Pi 5V/3A Power Adapter. Choose the one applied
in your country.

USB-Type-C 10cm Micro SD Card(TF Card), Card Reader

USB-Type-C cable 100cm Power Fan board

If you get Freenove USB-Type-C cable 100cm, you


need use it. Or something may be wrong.

support@freenove.com █
12 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Power requirement of different versions of Raspberry Pi is shown in following table:


Product Recommended Maximum total USB Typical bare-board
PSU current peripheral current draw active current
capacity consumption
Raspberry Pi Model A 700mA 500mA 200mA
Raspberry Pi Model B 1.2A 500mA 500mA
Raspberry Pi Model A+ 700mA 500mA 180mA
Raspberry Pi Model B+ 1.8A 600mA/1.2A (switchable) 330mA
Raspberry Pi 2 Model B 1.8A 600mA/1.2A (switchable) 350mA
Raspberry Pi 3 Model B 2.5A 1.2A 400mA
Raspberry Pi 3 Model A+ 2.5A Limited by PSU, board, and 350mA
connector ratings only.
Raspberry Pi 3 Model B+ 2.5A 1.2A 500mA
Raspberry Pi 4 Model B 3.0A 1.2A 600mA
Raspberry Pi Zero W 1.2A Limited by PSU, board, and 150mA
connector ratings only.
Raspberry Pi Zero 1.2A Limited by PSU, board, and 100mA
connector ratings only
For more details, please refer to https://www.raspberrypi.org/help/faqs/#powerReqs

In addition, RPi also needs a network cable used to connect it to wide area network.

All of these components are necessary. Among them, the power supply is required at least 5V/2.5A, because
lack of power supply will lead to many abnormal problems, even damage to your RPi. So power supply with
5V/2.5A is highly recommend. SD Card Micro (recommended capacity 16GB or more) is a hard drive for RPi,
which is used to store the system and personal files. In later projects, the components list with a RPi will
contains these required components, using only RPi as a representative rather than presenting details.

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 13

Optional Components

Under normal circumstances, there are two ways to login to Raspberry Pi: using independent monitor, or
remote desktop to share a monitor with your PC.
Required Accessories for Monitor
If you want to use independent monitor, mouse and keyboard, you also need the following accessories.
1. Display with HDMI interface
2. Mouse and Keyboard with USB interface

As to Pi Zero and Pi Zero W, you also need the following accessories.


1. HDMI to HDMI converter wire.
2. Micro-USB to USB-A Receptacles converter wire (Micro USB OTG wire).
3. USB HUB.
4. USB transferring to Ethernet interface or USB Wi-Fi receiver.

For different Raspberry Pi, the optional items are slightly different. But all of their aims are to convert the
special interface to standard interface of standard Raspberry Pi.

Pi Zero Pi Zero W Pi A+ Pi 3A+ Pi B+/2B Pi 3B/3B+/4B


Monitor Yes Yes Yes Yes Yes Yes
Mouse Yes Yes Yes Yes Yes Yes
Keyboard Yes Yes Yes Yes Yes Yes
Mini-HDMI to
HDMI converter Yes Yes No No No No
wire
Micro-USB to USB-
A Receptacles
converter wire Yes Yes No No No No
(Micro USB OTG
wire)
USB HUB Yes Yes Yes Yes No No
USB transferring to select one select one optional Internal
optional
Ethernet interface from two from two Integration
Internal
USB Wi-Fi receiver or select or select Internal
Internal Integration
two from two from Integration optional
Integration
two two

support@freenove.com █
14 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Required Accessories for Remote Desktop


If you do not have an independent monitor, or you want to use a remote desktop, first you need to login to
Raspberry Pi through SSH, and then open the VNC or RDP service. So you need the following accessories.

Pi Zero Pi Zero W Pi A+ Pi 3A+ Pi B+/2B Pi 3B/3B+/4B


Micro-USB to USB-A Yes Yes No
Receptacles
converter wire
(Micro USB OTG NO
wire)
USB transferring to Yes Yes Yes
Ethernet interface

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 15

Raspbian System

If you are using MacBook, please refer to following page. Then jumper to next Remote desktop & VNC.
https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up

If you are using Windows system, please follow content below:


Tool and System image
Software Tool
A tool Disk Imager Win32 is required to write system. You can download and install it through visiting the
web site: https://sourceforge.net/projects/win32diskimager/

Selecting System
Visit RPi official website (https://www.RaspberryPi.org/), click “Downloads” and choose to download
“RASPBIAN”. RASPBIAN supported by RPI is an operating system based on Linux, which contains a number of
contents required for RPi. We recommended RASPBIAN system to beginners. All projects in this tutorial are
operated under the RASPBIAN system.

https://www.raspberrypi.org/downloads/raspbian/

support@freenove.com █
16 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

After download, extract file with suffix (.img). Preparation is ready to start making the system.

Write System to Micro SD Card

First, put your Micro SD card into card reader and connect card reader to USB port of your computer.

Then open Win32 disk imager, choose the correct letter of your Micro SD Card (here is “J”), open the extracted
“.img” file and then click the "Write".
Step2. open the extracted “.img” file Step1. choose the correct letter

Step3. Click Write to write the system

After the system is written. Create a new folder named “ssh” under generated boot disk.

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 17

If you have any concerns, please feel free to contact us: support@freenove.com

support@freenove.com █
18 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Start Raspberry Pi

If you have any concerns, please feel free to contact us: support@freenove.com

If you don’t have a spare display, you can jumper to next section “Remote desktop & VNC”.

If you have a spare monitor, please follow steps below.


After the system is written successfully, take out Micro SD Card and put it into the card slot of RPi. Then
connect RPi to screen through the HDMI-micro HDMI, to mouse and keyboard through the USB port, to
network cable through the network card interface and to the power supply. Then your RPi starts initially. Later,
you need to enter the user name and password to login. The default user name: pi; password: raspberry. Enter
and login. After login, you can enter the following interface.

Now, you have successfully installed the RASPBIAN operating system for your RPi.

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 19

Remote desktop & VNC

If you don't have a spare display, mouse and keyboard for your RPi, you can use a remote desktop to share
a display, keyboard, and mouse with your PC. Below is how to use remote desktop to control RPi under the
Windows operating system.

Under windows, Raspberry Pi can be generally accessed remotely through two applications. The first one is
the windows built-in application remote desktop, which corresponds to the Raspberry Pi xrdp service. The
second one is the free application VNC Viewer, which corresponds to the VNC interface of Raspberry Pi. Each
way has its own advantages. You can choose either one or two.

Windows Raspberry Pi
Remote Desktop Connection Xrdp
VNC Viewer VNC

VNC Viewer can not only run under Windows, but also under system MAC, Linux, IOS, Android and so on.

SSH

Under previous Raspbian system, SSH is opened by default. Under the latest version of Raspbian system, it is
closed by default. So you need to open it first.

Method: after the system is written. Create a folder named “ssh” under generated boot disk, then the
SSH connection will be opened.

Then, download the tool software Putty. Its official address: http://www.putty.org/
Or download it here: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

support@freenove.com █
20 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Then use cable to connect your RPi to the routers of your PC LAN, to ensure your PC and your RPi in the same
LAN. Then put the system Micro SD Card prepared before into the slot of the RPi and turn on the power
supply waiting for starting RPi. Later, enter control terminal of the router to inquiry IP address named
“raspberry pi”. For example, I have inquired to my RPi IP address, and it is “192.168.1.108". Then open Putty,
enter the address, select SSH, and then click "OPEN", as shown below:

Step1: enter Step2:


the IP address Select SSH

Step3:
Click “OPEN”

There will appear a security warning at first login. Just click “YES”.

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 21

Then there will be a login interface (RPi default user name: pi; the password: raspberry). When you enter the
password, there will be no display on the screen. This is normal. After the correct output, press “Enter” to
confirm.

Then enter the command line of RPi, which means that you have successfully login to RPi command line
mode.

support@freenove.com █
22 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Remote Desktop Connection & xrdp

If you want to use built-in Remote Desktop Connection under Windows, you need install xrdp service on
Raspberry Pi.

Next, install a xrdp service, an open source remote desktop protocol(rdp) server, for RPi. Type the following
command, then press enter to confirm:
sudo apt-get install xrdp

Later, the installation starts.

Enter "Y", press key “Enter” to confirm.


After the installation is completed, you can use Windows remote desktop applications to login to your RPi.

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 23

Login to Windows remote desktop

Use "WIN+R" or search function, open the remote desktop application "mstsc.exe" under Windows, enter the
IP address of RPi and then click “Connect”.

Later, there will be xrdp login screen. Enter the user name and password of RPi (RPi default user name: pi;
password: raspberry) and click “OK”.

support@freenove.com █
24 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Later, you can enter the RPi desktop system.

Here, you have successfully used the remote desktop login to RPi.
If you have any concerns, please feel free to contact us: support@freenove.com

You can set a proper resolution ratio according to your monitor.


And if VNC doesn’t work normal, you need adjust resolution to try.
sudo raspi-config
Select 7 Advanced OptionsA5 Resolutionproper resolution ratio (set by yourself)OK. If it needs restart,
just restart.

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 25

VNC Viewer & VNC

Type the following command. And select 5 Interfacing OptionsP3 VNC YesOKFinish. Here Raspberry
Pi may need be restarted, and choose ok. Then open VNC interface.
sudo raspi-config

support@freenove.com █
26 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

Then download and install VNC Viewer by click following link:


https://www.realvnc.com/en/connect/download/viewer/windows/
After installation is completed, open VNC Viewer. And click File  New Connection. Then the interface is
shown below.

Enter IP address of your Raspberry Pi and fill in a Name. And click OK.
Then on the VNC Viewer panel, double-click new connection you just created, and the following dialog box
pops up.

Enter username: pi and Password: raspberry. And click OK.

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 27

Here, you have logged in to Raspberry Pi successfully by using VNC Viewer


If the window does not work normal, as below.

You need login to windows remote desktop and change resolution ratio.

If you think resolution ratio is not OK, you can set a proper resolution ratio on set interface of Raspberry Pi.
sudo raspi-config
Select 7 Advanced OptionsA5 Resolutionproper resolution ratio (set by yourself)OK. If it needs restart,
just restart.

support@freenove.com █
28 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

In addition, your VNC Viewer window may zoom your Raspberry Pi desktop. You can change it. On your
VNC View control panel, click right key. And select Properties->Options label->Scaling. Then set proper
scaling.

█ support@freenove.com
█ www.freenove.com Part 1 Assemble Case and Set Up Raspberry Pi 29

Here, you have logged in to Raspberry Pi successfully by using VNC Viewer and operated proper setting.
Then continue to do some preparation work: install a GPIO library wiringPi for your RPi.

Wi-Fi

Raspberry Pi 4B/ 3B+/3B integrates a Wi-Fi adaptor. You can use it to connect to your Wi-Fi. Then you can
use the wireless remote desktop to control your RPi. This will be helpful for the following work. Raspberry Pi
of other models can use wireless remote desktop through accessing an external USB wireless card.

support@freenove.com █
30 Part 1 Assemble Case and Set Up Raspberry Pi www.freenove.com █

What’s Next?

If you do not have Freenove project kit, you can visit official website to learn how to use Raspberry Pi.
https://projects.raspberrypi.org/en/projects/raspberry-pi-using

If you have Freenove Ultimate Starter Kit for Raspberry Pi or some electronic components, you can continue
this tutorial to complete dozens of projects.

█ support@freenove.com
█ www.freenove.com Part 2 Raspberry Pi Projects 31

Part 2 Raspberry Pi Projects

This part is for Raspberry Pi projects.

1. If you already get Freenove Ultimate Starter Kit for Raspberry Pi or some

electronic components, you can also download and learn the whole resource.
https://github.com/Freenove/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/archive/master.zip

It will teach you how to use raspberry Pi to control electronic components (such

as LED, motor, servo, sensors) to create interesting projects. We provide code of

both C and Python for each project, and code in Processing.pdf is based on Java.

2. We also have a Raspberry Pi Car. You can download the whole resource here.
https://github.com/Freenove/Freenove_Three-wheeled_Smart_Car_Kit_for_Raspberry_Pi/archive/master.zip

You can get Freenove Ultimate Starter Kit for Raspberry Pi, Freenove Three-

wheeled Smart Car Kit for Raspberry Pi and other Freenove products here:

http://www.freenove.com/store.html

If you have any concerns, please feel free to contact us.

support@freenove.com
Following are tutorial examples of Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi.

support@freenove.com █
32 Part 2 Raspberry Pi Projects www.freenove.com █

Contents of Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi

Preface
Raspberry Pi
GPIO Extension Board
Breadboard Power Module
C code & Python code
Chapter 0 Preparation
Step 0.1 Install the System
Step 0.2 Install WiringPi
Step 0.3 Obtain the Experiment Code
Step 0.4 Code Editor
Next
Chapter 1 LED
Project 1.1 Blink
Chapter 2 Button & LED
Project 2.1 Button & LED
Project 2.2 MINI Table Lamp
Chapter 3 LED Bar Graph
Project 3.1 Flowing Water Light
Chapter 4 Analog & PWM
Project 4.1 Breathing LED
Chapter 5 RGB LED
Project 5.1 Colorful LED
Chapter 6 Buzzer
Project 6.1 Doorbell
Project 6.2 Alertor
Chapter 7 PCF8591
Project 7.1 Read the Voltage of Potentiometer
Chapter 8 Potentiometer & LED
Project 8.1 Soft Light
Chapter 9 Potentiometer & RGBLED
Project 9.1 Colorful Light
Chapter 10 Photoresistor & LED
Project 10.1 NightLamp
Chapter 11 Thermistor
Project 11.1 Thermometer
Chapter 12 Joystick
Project 12.1 Joystick
Chapter 13 Motor & Driver
Project 13.1 Control Motor with Potentiometer
Chapter 14 Relay & Motor
Project 14.1.1 Relay & Motor

█ support@freenove.com
█ www.freenove.com 33

Chapter 15 Servo
Project 15.1 Servo Sweep
Chapter 16 Stepping Motor
Project 16.1 Stepping Motor
Chapter 17 74HC595 & LEDBar Graph
Project 17.1 Flowing Water Light
Chapter 18 74HC595 & 7-segment display.
Project 18.1 7-segment display.
Project 18.2 4-Digit 7-segment display
Chapter 19 74HC595 & LED Matrix
Project 19.1 LED Matrix
Chapter 20 LCD1602
Project 20.1 I2C LCD1602
Chapter 21 Hygrothermograph DHT11
Project 21.1 Hygrothermograph
Chapter 22 Matrix Keypad
Project 22.1 Matrix Keypad
Chapter 23 Infrared Motion Sensor
Project 23.1 Sense LED
Chapter 24 Ultrasonic Ranging
Project 24.1 Ultrasonic Ranging
Chapter 25 Attitude Sensor MPU6050
Project 25.1 Read MPU6050
Chapter 26 WebIOPi & IOT
Project 26.1 Remote LED
Chapter 27 Solder Circuit Board
Project 27.1 Solder a Buzzer
Project 27.2 Solder a Flowing Water Light
What's next?

You can download the whole resource here:


https://github.com/Freenove/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/archive/master.zip

support@freenove.com █
34 www.freenove.com █

Chapter 1 LED (Sample)

This chapter is the starting point of the journey to explore RPi electronic projects. Let’s start with simple “Blink”.

Project 1.1 Blink

In this project, let’s try to use RPi to control LED blinking.


Component List
Raspberry Pi 3B x1 GPIO Extension Board & Wire x1

BreadBoard x1

LED x1 Resistor 220Ω x1 Jumper

█ support@freenove.com
█ www.freenove.com 35

In the components list, 3B GPIO, Extension Shield Raspberry and Breadboard are necessary for each project.
They will be listed only in text form later.
Component knowledge

LED

LED is a kind of diode. LED will shine only if the long pin of LED is connected to the positive electrode and the
short pin is connected to negative electrode.
This is also the features of the common diode. Diode works only if the voltage of its positive electrode is
higher than its negative electrode.

The LED can not be directly connected to power supply, which can damage component. A resistor with certain
resistance must be connected in series in the circuit of LED.

Resistor

The unit of resistance(R) is ohm(Ω). 1mΩ=1000kΩ, 1kΩ=1000Ω.


Resistor is an electrical component that limits or regulates the flow of current in an electronic circuit.
The left is the appearance of resistor, and the right is the symbol of resistor represented in circuit.

Color rings attached to the resistor is used to indicate its resistance. For more details of resistor color code,
please refer to the appendix of this tutorial.

support@freenove.com █
36 www.freenove.com █

With the same voltage there will be less current with more resistance. And the links among current, voltage
and resistance can be expressed by the formula below: I=U/R.
In the following diagram, the current through R1 is: I=U/R=5V/10kΩ=0.0005A=0.5mA.

Do not connect the two poles of power supply with low resistance, which will make the current too high to
damage electronic components.
And resistor has no poles.

Breadboard

Take a short breadboard as an example to introduce its feature, as below.


The left picture shows the way of connection of pins. The right picture shows the pratical internal structure.

█ support@freenove.com
█ www.freenove.com 37

Circuit

Disconnect RPi from GPIO Extension Shield first. Then build the circuit according to the circuit diagram and
the hardware connection diagram. After the circuit is built and confirmed, connect RPi to GPIO Extension
Shield. In addition, short circuit (especially 5V and GND, 3.3V and GND) should be avoid, because short circuit
may cause abnormal circuit work, or even damage to RPi.
Schematic diagram

Hardware connection. If you need any support, please feel free to contact us via: support@freenove.com

Note:
Do NOT rotate Raspberry Pi to change the way of this connection.
Please plug T extension fully into breadbard.

Because Numbering of GPIO Extension Shield is the same as RPi GPIO, later Hardware connection diagram
will only show the part of breadboard and GPIO Extension Shield.
Code
According to the circuit, when the GPIO17 of RPi output high level, LED is turned on. Conversely, when the
GPIO17 RPi output low level, LED is turned off. Therefore, we can let GPIO17 output high and low level in

support@freenove.com █
38 www.freenove.com █

cycle to make LED blink. We will use both C code and Python code to achieve the target.

C Code 1.1.1 Blink

First, execute command into the terminal one by one. Then observe the project result, and analyze the code.
1. Use cd command to enter 01.1.1_Blink directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/01.1.1_Blink
2. Use the following command to compile the code “Blink.c” and generate executable file “Blink”.
“l” of “lwiringPi” is low case of “L”.
gcc Blink.c -o Blink -lwiringPi
3. Then run the generated file “blink”.
sudo ./Blink
Now, LED start blink.

You can press “Ctrl+C” to end the program. The following is the program code:
1 #include <wiringPi.h>
2 #include <stdio.h>
3
4 #define ledPin 0
5
6 int main(void)
7 {
8 if(wiringPiSetup() == -1){ //when initialize wiring failed, print message to screen
9 printf("setup wiringPi failed !");
10 return 1;
11 }
12 //when initialize wiring successfully, print message to screen
13 printf("wiringPi initialize successfully, GPIO %d(wiringPi pin)\n",ledPin);
14
15 pinMode(ledPin, OUTPUT);
16
17 while(1){
18 digitalWrite(ledPin, HIGH); //led on
19 printf("led on...\n");
20 delay(1000);
21 digitalWrite(ledPin, LOW); //led off

█ support@freenove.com
█ www.freenove.com 39

22 printf("...led off\n");
23 delay(1000);
24 }
25
26 return 0;
27 }
GPIO connected to ledPin in the circuit is GPIO17. And GPIO17 is defined as 0 in the wiringPi numbering. So
ledPin should be defined as 0 pin. You can refer to the corresponding table in Chapter 0.
#define ledPin 0
In the main function main(), initialize wiringPi first, and then print out the initial results. Once the initialization
fails, exit the program.
if(wiringPiSetup() == -1){ //when initialize wiring failed, print message to screen
printf("setup wiringPi failed !");
return 1;
}
//when initialize wiring successfully, print message to screen
printf("wiringPi initialize successfully, GPIO %d(wiringPi pin)\n",ledPin);
After the wiringPi is initialized successfully, set the ledPin to output mode. And then enter the while cycle,
which is an endless loop. That is, the program will always be executed in this cycle, unless it is ended outside.
In this cycle, use digitalWrite (ledPin, HIGH) to make ledPin output high level, then LED is turned on. After a
period of time delay, use digitalWrite(ledPin, LOW) to make ledPin output low level, then LED is turned off,
which is followed by a delay. Repeat the cycle, then LED will start blinking.
pinMode(ledPin, OUTPUT);
while(1){
digitalWrite(ledPin, HIGH); //led is turned on
printf("led on...\n");
delay(1000);
digitalWrite(ledPin, LOW); //led is turned off
printf("...led off\n");
delay(1000);
}
Among them, the configuration function for GPIO is shown below as:
void pinMode(int pin, int mode);
This sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK. Note that only
wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only wiringPi pin 7 (BCM_GPIO 4) supports CLOCK
output modes.
This function has no effect when in Sys mode. If you need to change the pin mode, then you can do it with
the gpio program in a script before you start your program
void digitalWrite (int pin, int value);
Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as an output.
For more related functions, please refer to http://wiringpi.com/reference/

support@freenove.com █
40 www.freenove.com █

Python Code 1.1.1 Blink

Net, we will use Python language to make LED blink.


First, observe the project result, and then analyze the code.
1. Use cd command to enter 01.1.1_Blink directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/01.1.1_Blink
2. Use python command to execute python code blink.py.
python Blink.py
Now, LED start blinking.

You can press “Ctrl+C” to end the program. The following is the program code:
1 import RPi.GPIO as GPIO
2 import time
3
4 ledPin = 11 # RPI Board pin11
5
6 def setup():
7 GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
8 GPIO.setup(ledPin, GPIO.OUT) # Set ledPin's mode is output
9 GPIO.output(ledPin, GPIO.LOW) # Set ledPin low to off led
10 print ('using pin%d'%ledPin)
11
12 def loop():
13 while True:
14 GPIO.output(ledPin, GPIO.HIGH) # led on
15 print ('...led on')
16 time.sleep(1) # delay 1 second
17 GPIO.output(ledPin, GPIO.LOW) # led off
18 print ('led off...')
19 time.sleep(1)
20 def destroy():
21 GPIO.output(ledPin, GPIO.LOW) # led off
22 GPIO.cleanup() # Release resource
23
24 if __name__ == '__main__': # Program start from here
25 setup()
26 try:
27 loop()
28 except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the subprogram destroy() will
29 be executed.
30 destroy()
31
In subfunction setup(), GPIO.setmode (GPIO.BOARD) is used to set the serial number for GPIO based on

█ support@freenove.com
█ www.freenove.com 41

physical location of the pin. GPIO17 use pin 11 of the board, so define ledPin as 11 and set ledPin to output
mode (output low level).
ledPin = 11 # RPi Board pin11
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(ledPin, GPIO.OUT) # Set ledPin to output mode
GPIO.output(ledPin, GPIO.LOW) # Set ledPin to low level to turn off led
print ('using pin%d'%ledPin)
In loop(), there is a while cycle, which is an endless loop. That is, the program will always be executed in this
cycle, unless it is ended outside. In this cycle, set ledPin output high level, then LED is turned on. After a period
of time delay, set ledPin output low level, then LED is turned off, which is followed by a delay. Repeat the
cycle, then LED will start blinking.
def loop():
while True:
GPIO.output(ledPin, GPIO.HIGH) # led on
print ('...led on')
time.sleep(1)
GPIO.output(ledPin, GPIO.LOW) # led off
print ('led off...')
time.sleep(1)
Finally, when the program is terminated, subfunction will be executed, the LED will be turned off and then the
IO port will be released. If close the program terminal directly, the program will be terminated too, but destroy()
function will not be executed. So, GPIO resources won’t be released, in the warning message may appear next
time you use GPIO. So, it is not a good habit to close the program terminal directly.
def destroy():
GPIO.output(ledPin, GPIO.LOW) # led is turned off
GPIO.cleanup() # Release resource
About RPi.GPIO:
RPi.GPIO
This is a Python module to control the GPIO on a Raspberry Pi. It includes basic output function and input
function of GPIO, and function used to generate PWM.
GPIO.setmode(mode)
Set the mode for pin serial number of GPIO.
mode=GPIO.BOARD, which represents the GPIO pin serial number is based on physical location of RPi.
mode=GPIO.BCM, which represents the pin serial number is based on CPU of BCM chip.
GPIO.setup(pin,mode)
Set pin to input mode or output mode. “pin” for the GPIO pin, “mode” for INPUT or OUTPUT.
GPIO.output(pin,mode)
Set pin to output mode. “pin” for the GPIO pin, “mode” for HIGH (high level) or LOW (low level).
For more functions related to RPi.GPIO, please refer to:
https://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/
“import time” time is a module of python.
https://docs.python.org/2/library/time.html?highlight=time%20time#module-time

support@freenove.com █
42 www.freenove.com █

You can download the whole resource here:


https://github.com/Freenove/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/archive/master.zip

You can get Freenove Ultimate Starter Kit for Raspberry Pi, Freenove Three-

wheeled Smart Car Kit for Raspberry Pi and other Freenove products here:

http://www.freenove.com/store.html

If you have any concerns, please feel free to contact us.


support@freenove.com

█ support@freenove.com

You might also like