Get C Programming On Raspberry Pi Develop Innovative Hardware Based Projects in C 1st Edition Dogan Ibrahim Free All Chapters
Get C Programming On Raspberry Pi Develop Innovative Hardware Based Projects in C 1st Edition Dogan Ibrahim Free All Chapters
com
https://ebookmeta.com/product/c-programming-on-raspberry-pi-
develop-innovative-hardware-based-projects-in-c-1st-edition-
dogan-ibrahim/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/raspberry-pi-for-radio-amateurs-1st-
edition-dogan-ibrahim/
ebookmeta.com
https://ebookmeta.com/product/raspberry-pi-iot-in-c-2nd-edition-harry-
fairhead/
ebookmeta.com
https://ebookmeta.com/product/raspberry-pi-projects-for-kids-1st-
edition-daniel-bates/
ebookmeta.com
https://ebookmeta.com/product/razor-components-succinctly-1st-edition-
ed-freitas/
ebookmeta.com
Designing the Patient Room A New Approach to Healthcare
Interiors 1st Edition Sylvia Leydecker
https://ebookmeta.com/product/designing-the-patient-room-a-new-
approach-to-healthcare-interiors-1st-edition-sylvia-leydecker/
ebookmeta.com
When the Smoke Cleared Attica Prison Poems and Journal 1st
Edition Celes Tisdale
https://ebookmeta.com/product/when-the-smoke-cleared-attica-prison-
poems-and-journal-1st-edition-celes-tisdale/
ebookmeta.com
https://ebookmeta.com/product/making-sense-of-media-and-politics-five-
principles-in-political-communication-2nd-edition-gadi-wolfsfeld/
ebookmeta.com
https://ebookmeta.com/product/recovering-history-through-fact-and-
fiction-forgotten-lives-1st-edition-dallas-john-baker/
ebookmeta.com
https://ebookmeta.com/product/practical-esd-protection-design-1st-
edition-wang-albert/
ebookmeta.com
Introduction to Internet of Things in Management Science
and Operations Research Implemented Studies International
Series in Operations Research Management Science 311
https://ebookmeta.com/product/introduction-to-internet-of-things-in-
management-science-and-operations-research-implemented-studies-
international-series-in-operations-research-management-science-311/
ebookmeta.com
books
books books
C Programming on
C Programming
Dogan Ibrahim
Dogan Ibrahim
an Elektor Publication
● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
78 York Street
London W1H 1DP, UK
Phone: (+44) (0)20 7692 8344
© Elektor International Media BV 2021
First published in the United Kingdom 2021
● All rights reserved. No part of this book may be reproduced in any material form, including
photocopying, or storing in any medium by electronic means and whether or not transiently or incidentally
to some other use of this publication, without the written permission of the copyright holder except in
accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a
licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England W1P 9HE.
Applications for the copyright holder's written permission to reproduce any part of this publication should be
addressed to the publishers. The publishers have used their best efforts in ensuring the correctness of the
information contained in this book. They do not assume, and hereby disclaim, any liability to any party for
any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from
negligence, accident or any other cause.
● ISBN: 978-3-89576-431-8
● EISBN: 978-3-89576-432-5
● EPUB: 978-3-89576-433-2
● Preface
The Raspberry Pi 4 is the latest credit-card sized computer that can be used in many
applications, such as audiovisual media centers, desktop computers, industrial control,
robotics, and many more domestic and commercial applications. In addition to the many
features found in other versions of Raspberry Pi, The Pi 4 also offers Wi-Fi and Bluetooth,
making it highly desirable in remote and internet-based control and monitoring applications.
The Raspberry Pi has traditionally been programmed using Python. Although Python is
a very powerful language, many programmers may not be familiar with using it. The C
language is probably the most commonly used programming languages. All embedded
microcontrollers can be programmed using the C language these days. The C language
is taught in all technical colleges and universities - almost all engineering students are
familiar with the use of this language in their projects.
This book is about using C with Raspberry Pi to develop various hardware-based projects.
Two of the most popular C libraries, wiringPi and pigpio are used.
The book starts with an introduction to the C language and most students and newcomers
will find this chapter invaluable. Many projects are provided in the book, including using
Wi-Fi and Bluetooth to establish communication with smartphones.
The book includes many sensors and hardware-based projects. Both wiringPi and pigpio
libraries are used in all projects. Complete program listings are given with full explanations.
All projects given in the book have been fully tested and work. The following sub-headings
are used in the projects where applicable:
• Project title
• Project description
• Aim of the project
• Block diagram
• Circuit diagram
• Program listing
wiringPi and pigpio program listings of all Raspberry Pi projects developed in the book are
available on the Elektor website. Readers can download and use these programs in their
projects. Alternatively, they can modify the supplied programs to suit their applications.
I hope readers find this book helpful and enjoy reading it.
●6
Table of Contents
Table of Contents
● Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
●7
C Programming on Raspberry Pi
3.2.17 ● Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.2.18 ● Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.3 ● Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
●8
Table of Contents
Chapter 7 ● I2C Bus Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
7.1 ● Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
7.2 ● The I2C Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
7.3 ● Project 1 – Port expander . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
7.4 ● Project 2 – EEPROM memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
7.5 ● Project 3 – TMP102 temperature display . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
7.6 ● Project 4 – I2C LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
7.7 ● Project 5 – Using the pigpio library with I2C – TMP102 temperature display . . . 237
7.8 ● Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
●9
C Programming on Raspberry Pi
12.13 ● Support for other chips and add-on boards . . . . . . . . . . . . . . . . . . . . . . . 310
12.14 ● Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
● 10
Table of Contents
● Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
● 11
C Programming on Raspberry Pi
1.1 ● Overview
In this chapter, we will learn how to install the latest operating system (Raspbian Buster)
on the Raspberry Pi 4. We will also learn the different ways that Python can be used to
develop applications. Notice the installation process given below applies to all Raspberry Pi
models unless otherwise specified.
Raspbian Buster is the latest operating system for the Raspberry Pi. This section provides
the steps necessary for installing this operating system on a new blank SD card, ready to
use with Raspberry Pi 4. You will need a micro SD card with a capacity of at least 8GB (16
GB is preferable) before installing the new operating system.
The steps to install the Raspbian Buster operating system are as follows:
• Download the Buster image to a folder on your PC (e.g. C:\RPIBuster) from the
following link by clicking the Download ZIP under section Raspbian Buster with
desktop and recommended software (see Figure 1.1). At the time of writing this
book, the file was called: 2020-02-13-raspbian-buster-full.img. You may have to
use the Windows 7Zip software to unzip the download due to some features not being
supported by older zip software.
https://www.raspberrypi.org/downloads/raspbian/
● 12
Chapter 1 ● Installing the Operating System on Raspberry Pi
• Put the blank micro SD card into the card slot of your computer. You may need an
adapter to do this.
• Download Etcher to your PC to flash the disk image. The link is (see Figure 1.2):
https://www.balena.io/etcher/
• Double click to open Etcher and then click Select image. Select the Raspbian Buster
file you downloaded and unzipped.
• Click Select target and select the micro SD card.
• Click Flash (see Figure 1.3). This may take several minutes, wait until it is finished.
The program will then validate and unmount the micro SD card. You can remove your
micro SD card after it is unmounted.
● 13
C Programming on Raspberry Pi
Your micro SD card now has been loaded with the Raspberry Pi operating system. The
various options now are as follows:
If you are making a direct connection to your Raspberry Pi using a monitor and keyboard,
just insert the SD card into the card slot and power-up your Raspberry Pi. After a short
while, you will be prompted to enter the login details. The default values are username: pi,
password: raspberry.
You can now start using your Raspberry Pi either in command mode or in desktop mode. If
you are in command mode, enter the following command to start the GUI mode:
pi@raspberrypi:~ $ startx
• Move down to Boot Options and press Enter to select (Figure 1.4).
• Select Desktop / CLI and then select Desktop Autologin to boot automatically into
GUI mode.
• Click OK and accept to reboot the system. The system will be in GUI mode next time
it reboots.
• You can change your selections to boot in command mode if you wish by selecting
Console in Boot Options.
You may now want to connect your Raspberry Pi to the internet either to access it remotely
from a PC or to use the internet. If your Raspberry Pi is equipped with an ethernet port
(e.g. Raspberry Pi 2/3/4), you can directly connect to your Wi-Fi router using an ethernet
cable. You can find the IP address of your connection by entering the command: ifconfig in
command mode.
Alternatively, you may want to connect your Raspberry Pi to Wi-Fi and access it remotely.
● 14
Chapter 1 ● Installing the Operating System on Raspberry Pi
• Move down to Interface Options and select SSH and enable it.
• If you are in GUI mode, click the Wi-Fi icon at the top right hand of the screen and
enable Wi-Fi. Note the IP address allocated automatically to your Raspberry Pi.
• You can now remotely access your Raspberry Pi using terminal emulation software,
such as Putty (see Section 1.4 and 1.5).
If you do not have a suitable monitor and keyboard to directly connect to your Raspberry Pi,
you will have to use a networked connection and remotely access your Raspberry Pi using a
PC. There are two options: connection using an Ethernet cable, and connection over
Wi-Fi.
https://notepad-plus-plus.org/downloads/v7.8.5/
https://www.advanced-ip-scanner.com
• Run the software and look for your Raspberry Pi. You do not have to install the software
to run it. Click Run portable version, and then Scan. As shown in Figure 1.5, the IP
address of the author’s Raspberry Pi was 191.168.1.202.
● 15
C Programming on Raspberry Pi
• You can now use Putty to log in to your Raspberry Pi (see Section 1.4 and 1.5)
Alternatively, you can find the IP address of your Raspberry Pi by opening the command
prompt on your PC with administrator privilege (by right-clicking to accepting to run as an
administrator) and then inputting the command: ping raspberrypi.home as shown in
Figure 1.6.
It is also possible to find the IP address of your Raspberry Pi using your smartphone. Many
apps can be used to find out who is currently using your Wi-Fi router. e.g. Who’s On My
Wi-Fi – Network Scanner by Magdalm.
Connection using Wi-Fi: This is the preferred method to access your Raspberry Pi and is
the one used by the author. Here, as described in Chapter 1, the Raspberry Pi can be placed
anywhere you like within the range of the Wi-Fi router and is easily accessed from your PC
using Putty (see Section 1.4 and 1.5).
https://notepad-plus-plus.org/downloads/v7.8.5/
● 16
Another Random Scribd Document
with Unrelated Content
[Contents]
See Grimm 68, The Thief and his Master, Bolte u. Polívka 2: 60–69;
and compare Tremearne, 223–224; Mason and Espinosa, New
Mexico, JAFL 24: 423–424.
[Contents]
See Grimm 17, The White Snake, Bolte u. Polívka 1: 131–134; and
Aarne’s study, Der Tiersprachen verstehende Mann, in FF
Communications No. 15. Compare Koelle, 143–145; Basset 2: 119–
124; Junod, 314–317; Chatelain, 219–223; Smith, 565.
[Contents]
[Contents]
[Contents]
[Contents]
126. A Misunderstanding. [Story]
[Contents]
The story is very common in Jamaica. See Grimm 18, The Straw, the
Coal and the Bean, Bolte u. Polívka 1:135–137, and compare
Parsons, Andros Island, 147.
[Contents]
[Contents]
The familiar episode of the Wolf’s head which occurs early in the
Reynard cycle (see Percy Society Publications 12, Introduction,
pages xxxiii–xxxiv) is, in African stories, often combined with that of
the Goat in the Lion’s den (or the Hyena’s). Compare Rattray,
Chinyanje, 149–152; Tremearne, 227–229; FL 22:63–65.
[Contents]
[Contents]
See Grimm 110, The Jew among Thorns, Bolte u. Polívka 2:490–
503; and compare Bundy, JAFL 32:412–413.
[Contents]
See note to number 106, and Grimm 104, Wise Folks, Bolte u.
Polívka, 2:440–451. One version from Parsons, Andros Island,
[286]93–94, connects this episode with those of the frightened
robbers and the tongue-cutting in number 109.
[Contents]
[Contents]
[Contents]
See Boas, JAFL 25: 223–226, where the Devil is the rival sorcerer.
[Contents]
[Contents]
For the story of “The Fifer,” six actors were required, one to
represent the boy, one the father, and four others the “wild beasts.”
“Anansi,” “Dry-head,” “Tacoomah” and “Tiger” were the “beasts.” Roe
said that “the one who takes the son’s part tells the story.” The
dramatization went on much like a school exercise performed by
grown men, with improvised action and (probably) extemporized
dialogue. It ended in a dance in which all six joined.
[Contents]
[Contents]
The songs of this medley at first follow the story of Goat’s escape
from the dance, number 40, combined with the parallel story of Rat’s
escape from Puss’s dance. The song is taken from a popular game
in which one player represents the cat, another the rat; all the others
form a line with clasped hands, and Puss tries to catch Rat through
the line, while all sing the song. The bull’s song belongs to number
88 or 89. Anansi’s fifing is possibly taken from 139.
[Contents]
Old Mary Roden was bed-ridden and lived in a one-roomed hut, the
floor of which was falling in. The little grandchild, when prompted to
“make a figure,” danced quite spontaneously to the rhythm of the
grandmother’s quavering song. The same is true of the next two
numbers. Songs sung to be danced to in this fashion have rather the
monotonous rhythm of a drum-beat than any melody in our sense of
the word. For the story, see number 23. [288]
[Contents]
[Contents]
Sarah Findley was an old-time negress who lived in a little hut far out
in the bush. She danced to the song with a queer jumping motion
like boys playing leap-frog and with all the agility of a young girl. The
dance as a wake game is performed upon two parallel bars held by
four men. One informant called it dancing “Calimbe.”
[Contents]
[Contents]
Witticisms.
I & II. These old-fashioned slave stories are from old Vassel Edwards
at Retirement, in the Cock-Pit country. They belong to the “nager-
trick” stories quoted by Lewis.
III. The Congo negro is said to be duller-witted than negroes from the
Gold Coast. To call a man a “Congo” is hence a term of ridicule.
IV. This witticism is common. In one version, the man was said to be
“walking in Kingston.” Mrs. Elizabeth Hilton gave me a version she
learned from Henry Roe, school-master at Retirement, which bears
the marks of having been put together by some literary entertainer.
“From the day me leave school me never see Marse Peter any more till
one day we buck up. A glad to see him till a couldn’t glad any more. Marse
Peter went a tell me somet’ing, a laugh till me belly nearly pop.
“Marse Peter was the sort of boy used to go out after hours. Him ma tell
him if him (she) been dead before him, she will show him token (frighten
him). [289]But Marse Peter never will believe her. One night, Marse Peter
go out. When him coming back, he catch right at the cross-road where
dem Taylor boy used to sit down a day-time, an’ smell somet’ing funny, but
he never know wha’. He been ’fraid, but afterward he no ’fraid again. An’
see one man come wid litt’e fire. He say, ‘I beg you a light, sah!’ The man
give him a light. The man has some teeth a his mouth, they long like a
Jack-ass a laugh a sun-hot. Marse Peter pass the man. He meet up
another man. He say, ‘Look here, me frien’, I meet a man jus’ roun’ the
turning, have teeth long like a Jack-ass a laugh a sun-hot.’ The man said,
‘Teeth like these do they long?’ Marse Peter run an’ he run an’ never stop
runnin’ till he meet up a mother bed. From that, Marse Peter never go af’er
no girl again. Marse Peter behave a good buckra massa af’er this.”
In Koelle, 174–177, Toad and Rat have a wager to see if one can do
what the other cannot. Toad passes a crowd with a whole skin; Rat is
pursued with sticks and stones.
XVI. This and the next two witticisms were written out by some
young lads in Bethlehem, Santa Cruz Mountains.
[Contents]
[Contents]
A Jamaica negro proverb runs, “Darg say befo’ him plant yam fe look
like masquita’ foot, him satisfy fe tun beggar.” See Cundall’s
collection (Kingston, 1910), 211.
[Contents]
[Contents]
[Contents]
An old man over eighty who was present at the recital of this story
remembered hearing it when he was a little boy. Hauling lumber was
in old days accompanied by song. The story turns upon a theme
common in American Indian hero cycles, that of a trickster’s claim to
magical powers which he does not possess. [291]
[Contents]
INDEX TO INFORMANTS.
See 1b.
4. Baker, Maud, aged 21. She called upon me with her stories, which
she had from her father, a native of Dry River, though she herself
had been educated in Kingston.
5. Barrett, Eliza, aged 30. She was one of a group of women who
were friends of the colored housekeeper at Harmony Hall.
7. Brown, Arthur, aged 23. He was a friend of the chauffeur for the
hotel at St. Ann’s Bay and took me down to his mother’s house at
Steeretown, where he gathered a group for story-telling, each one
reciting one or two stories in turn.
See 127a.
See 47a.
10. Brown, T., another contingent, a Claremont lad who had picked
up a quantity of stories but recited them in a slovenly way, without wit
or point.
See 117.
See 11a.
See 132.
14. Dodd, Emiline, under 30. She visited the house at Lacovia where
I was staying.
See 86b.
15. Doran, Grace, very old. She was from Whitehall, near Harmony
Hall. She interpolated her stories with songs in the old style, but
talked so rapidly I was unable to follow except in snatches.
See 27a.
16. Edwards, Vassel, over 80. His father and mother had been
slaves in the same district, one at Retirement, where he himself had
lived all his life and was now deacon in the Scotch Presbyterian
church.
18. Findley, Sarah, “over 50.” She was mother to one of the house-
girls at Bethlehem, in the Santa Cruz Mountains, an old-time woman
and quite illiterate.
See 146.
19. Forbes, William, over 75. He came from Dry River and was one
of my best story-tellers. He had been song-leader and “Tea-meeting
chairman” for his district and was much respected for his intelligence
and sense of order. He came at several different times and sang or
told stories with equal ease and with a freshness and delight which
was contagious, never failing to bring a small gift from his garden
and never arguing over the pay. He had a very long-shaped head
and beaming eyes.
See 2a, 3, 7, 8, 11b, 24, 25b, 46, 70, 85b, 86a, 101, 140, 141,
142.
20. Ford, May, a young girl. She was of the better class, daughter of
the lodging-house keeper at Newmarket, in Westmoreland.
See 44.
See 88.
22. Gentle, Julia, over 70. She came to me twice at Bethlehem in the
Santa Cruz mountains and recited the stories with great rapidity as if
she knew them by heart in a fixed form, among them some English
ballads of second rate interest.
See 73b.
25. Hendricks, Moses, over 60. He was a white man, but lived with
his negro family like one of the race; an excellent story-teller,
dictating some fifteen stories to me at three different visits to his
house.
See 13b, 15, 25a, 26, 29, 35, 48, 69a, 99, 138.
See 5.
28. Hilton, James Anderson, aged 33, one of the Maroon men.
See 149.
29. Iron, Adolphus, about 50. A reputed humorist from Golden Grove
near Claremont, but disappointing in frock coat at the lodging house.