Make Code Hardware

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

\

1|Page
Let’s Make and Code Hardware
Episode 1
This weekly eBook episode “Let’s Make and Code Hardware” is part of CloudX initiatives
(cloudx.ng), which has been instigated in the hope of inspiring young minds with technology
development. It aims at imparting pragmatic knowledge through various hands on projects
and tutorials. It provides tutorials in the realm of the latest technologies, for example:
Displaying Text or Video on Big Clown LCD Module over USB or Building a Sumo Robot
(Fighting and gaming robots). Most electronics projects are opensource for makers to
contribute and can be found on various hardware platforms like Hackster, Instructables,
SparkFun, Adafruit, CloudX and many more!

Technology prototyping has become so facile and swift today that term like “Rapid
Prototyping” is used instead for it. New and powerful open source development platforms
like “CloudX”, “Arduino” and “Raspberry Pi” make it easy for even school children to work
on robotics and other fascinating technologies. These technologies have become an
inevitable step to gaining profound knowledge about microcontrollers and microprocessors
and hence should even be made a part of the current academic curriculum.

There are prolific number of communities and forums surrounding these platforms which
allow for quick and easy troubleshooting. Furthermore, beginner tutorials in YouTube are
also very helpful in gaining a better perspective about it. Today, with the advent of 3D
printers, now you can even create a custom casing for your project or print your own PCB.

This eBook will act like a beginner’s guide for you to start prototyping using tools like the
CloudX, Arduino and Raspberry Pi. Moreover, this eBook has been designed in such a way to
promote self-learning and ingenuity. So, have fun with this episode 1 and good luck on being
a CloudX Maker!

2|Page
Contents
What you will be learning

Introduction
What are Microcontrollers and Microcontroller Boards?
About CloudX
What is Physical Computing?
Prototyping
Product Development
Terminology
C Basics

Hardware Overview
CloudX Microcontroller Board
Hardware Specifications
Controller
SoftCard

CloudX Software
Downloading and Installing the IDE
Importing CloudX Library
Understanding the IDE
Installing the CloudX Driver
Writing, Compiling and Uploading codes
How to find errors (bugs)
CloudX Proteus Simulations
CloudX Visual Block Programming

Programming in CloudX
Light Emitting Diode (LED) Blinking
LED Chaser
Writing to PORTs
Sound an alarm
Controlling a Relay
Powering your CloudX Board
3|Page
Introduction
What is Microcontroller?

A microcontroller is a programmable chip, a small computer on a single integrated


circuit(IC); it contains the CPU (processor), memory, input and output (I/O) peripherals. It
saves space, external peripheral timing, design time and project compatibility issues.
It is sometimes called an embedded controller or microcontroller unit (MCU), they are
widely used in all our electronic gadgets and everyday life such as televisions, radios,
washing machines, mobile phones, airplanes, cars, robotics etc.

Microcontrollers can be used in different application based on its processor such as 4-bit, 8-
bit, 16-bit and complex processors such as 32-bit or 64-bit which are common in advanced
technology.

When microcontrollers were first designed, they only use assembly language, but now the C
programming language is the most frequently used language. Most common
microcontrollers (MCU) includes 8051(by Intel) which was developed in 1985; the AVR (by
Atmel) developed in 1996; the PIC (by Microchip technology).
Microcontrollers come with different kinds of memory such as Data Memory or Random
Access Memory (RAM), Program Memory or Read-Only Memory (ROM) and the EEPROM.

Microcontroller Boards
MCU boards are microcontrollers built on a single printed circuit board, this boards provides
easy access to interface other hardware components and simplifies user’s interaction with
the MCU through pre-built libraries. Microcontroller boards contain on-board voltage
regulators, filters, oscillators, pin sockets and also comes bootloaded (pre-loaded codes)
which allows users to load programs into the microcontroller via serial pins. Examples of
microcontroller boards include Raspberry Pi, CloudX, Arduino, Beaglebone etc.

4|Page
About CloudX

CloudX is a series of small single-board


computer that comes with hundreds of
pre-built libraries that allow users to
learn embedded system designs and
programming through fun and practical
projects. It is an open-source platform
developed to promote Do-It-Yourself
(DIY) projects and to share hacks; it was
developed in Nigeria at ByteHub
Embedded studio to promote the
teaching of basic electronics and
computer engineering in schools across Africa.

The name CloudX means “a state of happiness when you do or create something you’ve
never imagined could be possible” which was derived from Cloud9 and Cloud10.
It was invented to change the way people see and use technology, to spread and support
free and open-source hardware rapid prototyping from project into product. The purpose is
to promote and expand the hardware maker community in Africa and to give people an
easy platform to sense, interact and control objects in the physical world.

Prototyping

Prototyping is at the heart of the CloudX: we make things and build objects that interact
with other objects, people, and networks. We strive to find a simpler and faster way to
prototype in the cheapest possible way.
A lot of beginners approaching electronics for the first time think that they have to learn
how to build everything from scratch. This is a waste of energy: what you want is to be able

5|Page
to confirm that something’s working very quickly so that you can motivate yourself to take
the next step or maybe even motivate somebody else to give you a lot of cash to do it.
This is why we developed “opportunistic prototyping”: why spend time and energy building
from scratch, a process that requires time and in-depth technical knowledge, when we can
take ready-made devices and hack them in order to exploit the hard work done by large
companies and good engineers?

What is Physical Computing?


Physical Computing uses electronics to prototype new materials for designers and artists.
It involves the design of interactive objects that can communicate with humans using
sensors and actuators controlled by a behaviour implemented as software running inside a
microcontroller (a small computer on a single chip).
In the past, using electronics meant having to deal with engineers all the time, and building
circuits one small component at the time; these issues kept creative people from playing
around with the medium directly. Most of the tools were meant for engineers and required
extensive knowledge.
In recent years, microcontrollers have become cheaper and easier to use, allowing the
creation of better tools. The progress that we have made with CloudX is to bring these tools
one step closer to the novice, allowing people to start building stuff after only two or three
days of a workshop. With CloudX, a designer or artist can get to know the basics of
electronics and sensors very quickly and can start building prototypes with very little
investment.

Product Development

Product development is a combination of experience and luck. Deep thinking is the most
important thing when coming up with a product especially in hardware industry.
Ideas comes like flashes, don’t discard it, you need to always write them down no matter
how simple, Mark Zuckerberg, the founder of Facebook once said that ideas don’t come out
fully formed, they only become clearer as you work on them, you just have to get started.
Once you’ve discover a problem you want to solve, think on the mode of operation, draw a
flow chart, block diagram, circuit diagram or any other drawing needed to start the design.
Gather all the necessary components and build up a prototype board for your product.

If this is the first time you’re learning about embedded systems –THEN START FROM
SCRATCH- try switching an LED on and off from a pushbutton to understand and get familiar

6|Page
with embedded programming and hardware connections before attempting a bigger or
advanced project.
Build up the code in simple stages – testing as you work. Rework your flowchart to keep it
up to date.

Terminology

Let’s start with some basic terminology used in this eBook.

Microcontroller Are small single-chip or integrated circuit which cannot do anything on their
own without software program written into them.

I/O Are pins that connects the microcontroller to the outside world which can be configured
as input or output. It is needed in most cases to allow the microcontroller to communicate,
control or read information.

Program These are sequence or series of instructions which tells microcontrollers what to
do.

Software These are programs that the Microcontroller needs for it operate or run. This
needs to be free of bugs and errors for a successful application or project. Software can be
written in a variety of languages such as Assembly, C, Python or Basic etc.

Hardware The Microcontroller, memory, interface components, signal conditioning circuits,


power supplies and all the components – connected to it to make it work and interface to
the outside world.

Simulator This is an environment that allows you to run both your software and hardware
project on the computer before building the project in the outside world. The simulating
software used in this book is the Proteus Design Suite; it is a complete software solution for
circuit simulation, debugging and PCB design. It comprises several modules for schematic
capture, firmware IDE and PCB layout that appear as tabs inside a single, integrated
application. This provides a smooth AGILE workflow for the design engineer and helps
products get to market faster. You can always find free versions of Proteus on the internet.

SoftCard A breakout cloudx board that enables programs (known as machine language) to
be loaded into the microcontroller through serial communication.

CodeLoader An application embedded in the CloudX IDE that allows the Hex File (machine
language) generated by the compiler to be loaded serially into bootloaded microcontrollers.

7|Page
Bootloader is a program that runs in the microcontroller to be programmed. It receives new
program information externally through specific communication means and writes that
information to the program memory of the processor.

Source File A program written in a language the compiler and you understand. The source
file has to be processed in machine language before the microcontroller will understand it.

Compiler A software package which converts the Source file into an Object file. Error
checking is built in, a heavily used feature in debugging a program as errors are flagged up
during the building process.

Hex File This is the file produced by the compiler, the file extension is .OBJ or .HEX
depending on the compiler directive.

List File This is a file created by the assembler / compiler and contains all the instructions
from the Source file together with their hexadecimal values alongside and comments you
have written. This is the most useful file to examine when trying to debug the program as
you have a greater chance of following what is happening within the software than the
Source file listing. The file extension is .LST

Other Files The error file (.ERR) contains a list of errors but does not give any indication as to
their origin. The .COD file is used by the emulator.

Bugs Errors created free of charge by you. These include simple typing errors to incorrect
use of the software language syntax errors. Most of these bugs will be found by the
compiler and shown up in .LST file, others will have to be sought and corrected by trial and
error.

C Basics

C is a general purpose high level computer programming language developed by Dennis


Ritchie to create the Unix operating system at Bell labs, it was first implemented on the DEC
PDP-11 computer in 1972. It was a successor of B language that has been around early
1970s. The C language was formally accepted by the American National Standards Institute
(ANSI) in 1988, which is now often referred to as ANSI C or Standard C.

Embedded C is an extended C programming language that provides support for developing


efficient programs for embedded devices; it is widely used for embedded systems because it
lies between a high level language and low level (assembly) language. C is generally used for
desktop computers, while embedded C is used for microcontroller based applications.

8|Page
Embedded system is an application that contains a microcontroller (programmable chip) or
any digital signal processor (DSP) chip that are designed to perform a specific operation;
often referred to as the combination of software programs and hardware to perform a task.
There are varieties of different Embedded C compilers, manufactured by different
companies which popular ones are mikroC for PIC and ARM, Kiel C, CCS C, MPLAB-XC from
microchip technology etc.

Structure of C Programs All C program contains pre-processor directives, declarations,


definitions, expressions, statements and functions.

Pre-processor A pre-processor directive is a command to the C pre-processor (which is


automatically invoked as the first step in compiling a program). The two most common pre-
processor directives are the #define directive, which substitutes text for the specified
identifier, and the #include directive, which includes the text of an external file into a
program.

Declaration A declaration set up the names and attributes of variables, functions, and types
used in the program. Global variables are declared outside functions and are visible from
the end of the declaration to the end of the file. A local variable is declared inside a function
and is visible from the end of the declaration to the end of the function.

Definition A definition set up the contents of a variable or function. A definition also


allocates the storage needed for variables and functions.

Expression An expression is a combination of operators and operands that yields a single


value.

Statement Statements control the flow or order of program execution in a C program.

Function A function is a collection of declarations, definitions, expressions, and statements


that performs a specific task. Braces enclose the body of a function. Functions may not be
nested in C.

Variables A variable simply means a storage place which has some memory allocated to it;
each variable in C has a specific data type, which determines the size and layout of the
variable’s memory.

Keywords These are specific reserved words in C for special purposes, which each of them
have a specific feature associated with it, all the words that enhances our functionality of
the C language are included in the list of total 32 keywords.

9|Page
Data Types refers to the type and size of data associated with variables and functions.

Sample C Programs

/* My first C program */
#include <M633.h> /* preprocessor directive */
#define PI 3.142 /* include standard C header file */
float area; /* global declaration */
int square (int r); /* prototype declaration */
void main() { /* beginning of main function */
int length; /* local declaration */
int breadth = 3; /* declaration and initialization */
length = square (breadth); /* pass a value to a function */
area = PI * length; /* assignment statement */
}

10 | P a g e
main() Function
Every C program must contain a function named main where program execution begins. The
braces that enclose the main function define the beginning and ending point of the
program.

void main() { // beginning of main function


}

All C programs contain one or more functions. Functions are subroutines, each of which
contain one or more statements and can be called upon by other parts of the program.
When writing programs, indentations, blank lines and comments, improve the readability.
The extension .h stands for header file which contains C function declarations and macro
definitions to be shared between several source files. The header file M633.h which
contains CloudX pre-processor configurations, contains most of the input and output
functions. It is necessary to use only the include files that pertain to the standard library
functions in your program.

/* My first C program */ is a comment in C, they are preceded by a /* and end with a */.
Newer style comments begin with // and go to the end of the line. Comments are ignored
by the compiler and therefore do not affect the speed or length of the compiled code.
Comments provide clarity to the C source code allowing others to better understand what
the code was intended to accomplish and greatly helping in debugging the code. Comments
are especially important in large projects containing hundreds or thousands of lines of
source code or in projects in which many contributors are working on the source code.

1. Using // this is used to write a single line comment.


2. Using /* */ the statements enclosed within /* and */ are used to write multi-line
comments.

Every C programs must have a main() function. This is the starting point into the program.
All functions have the same format which is:

FunctionName() {
codes here…
}

Statements within a function are executed sequentially, beginning with the open curly brace
and ending with the closed curly brace.
The curly braces { and } show the beginning and ending of blocks of code in C.
Semicolon (;) is used to mark the end or terminate a statement and beginning another
statement. Absence of semicolon at the end of any statement will mislead the compiler to
think the statement is not yet finished which may lead to compilation error. Therefore, all
statement must end with a semicolon.

11 | P a g e
Hardware Overview
CloudX Microcontroller Board

CloudX is an opensource single-board microcontroller bootloaded by the manufacturer to


allow instructions to be loaded into the board through serial port. They are inexpensive
computers with the ability to store and run programs. The boards are equipped with
input/output (I/O), analog, timers and communication pins which can be interfaced with all
kinds of digital sensors, keypads, integrated circuits (IC), port expanders, memory chip etc.;
the board uses the PIC microcontroller chip.

The microcontroller boards are based on PIC16, PIC18, PIC24 and PIC32 series; programmers
can select which of the board to buy based on the memory size, numbers of I/O pins, clock
speed, physical size etc., the cloudx series as at the time of writing this first eBook are
M633, Micro, Priscilla and the development board V1.0.

Unlike the other microcontroller boards in the market, the complete cloudx hardware is
made up of the controller and the softcard. The board that connects the microcontroller to
the outside world is called controller while the breakout board that is used to upload
compiled programs (Hex file) from the computer into the controller board is referred to as
softcard. The softcard can also be referred to as “hardware programmer” and they are used
to program and debug microcontrollers through serial communications (UART) port. One
interesting thing about the softcard is that users only need to buy it once because you only
need just one softcard to program different series of the controller at any time.

In this episode, we will only focus on the CloudX M633 which is the easiest for a novice to
learn with and it is widely used in education for prototyping. The M633 board is based on
the PIC16F877A chip which datasheet can be downloaded from the manufacturer’s website,
the M633 has 16 digital input/output pins, 6 analog inputs, 4 PWM outputs that can also be
used as timers and serial pins( UART, I2C and SPI), this microcontroller board runs on 20MHz
quartz crystal.

12 | P a g e
Hardware Specification

The CloudX is made up of the controller and the softcard;


a. Controller is a board that has the microcontroller chip, the regulated power with
necessary filters, digital input and output, analog, timers and communication pins. The
board also has an ICSP male header pin which the manufacturer or user can use to load
firmware on the board.

Controller board part description

1. Reset Button this is a terminal intentional left for push button, when the button is
pressed or the two holes bridged then the microcontroller will restart its operation.
2. Voltage Regulator this controls the amount of voltage that goes into the
microcontroller, the regulator chip used regulates incoming voltage to 5v suitable for
the MCU.
3. Pin LED the Lighting Emitting Diode (LED) is connected to pin1 and it is intentional
placed there by the manufacturer for quick program test.
4. Power LED this lights up anytime the board is connected to power source.
5. Power Socket this terminal consists of RESET, VIN, 5V, 3.3V and GND pin.

The Reset pin has an inbuilt pull-up resistor, the board restarts when this pin is connected
to ground (GND). Push buttons are always connected to this pin in order the reset this
whole system.
The VIN accepts recommended voltage from 7 – 12V.
The 5V pins can serve as sinking or sourcing regulated power supply.

13 | P a g e
The 3.3V is generated when the softcard is connected to
the controller board.
The GND pins are the negative input for the board.

6. Softcard connector this connects the softcard to


the controller board for code uploading.
7. Analog Pins these pins are used to read signals
from analog sensors or objects and convert it to
digital for processing; it’s often referred to as
Analog-Digital converter (ADC) pins.
8. Digital Pins are used for digital input and output
operations. The cloudx M633 contains 16 digital I/O pins.
9. PWM this pins are used to generate analog output signals in waveform, they can also
be used as timers.
10. Communications are used to interface with other chips (memory, RTC etc.) serially.
This protocol includes UART, I2C and SPI.
11. PIC Microcontroller this is the brain of the board and it is where the programs
uploaded are stored.
12. ICSP Pin this is referred to as In Circuit Serial Programming pin used by the
manufacturer to bootload the microcontroller on the board.

b. Softcard this USB to Serial breakout board serves as hardware programmer used to
upload codes into the microcontrollers. The softcard was specially developed to be
compatible with all series of cloudx microcontroller board. The softcard can also be
used as a standalone USB to serial device. When connected to the computer (with
available serial terminal), it can be used to communicate with all kinds of serial
modules like Bluetooth, WI-FI, SIM800 etc.

14 | P a g e
Softcard board part description

1. RX LED an indicator that shows data receiving from the


computer
2. TX LED an indicator that shows data transmitted to the
computer.
3. Power LED this indicates that softcard is connected to
power source.
4. USB Port is a connector for USB cable.
5. CH340G is a USB bus convert chip and it can realize
USB convert to serial interface.
6. Connector it connects the softcard to the controller
board

The CH340G requires a specific driver. You can find and download
the driver on the cloudx download page website. However,
installation is very straight-forward (on a Windows 7 (64 bit)
machine, at least.
The CH340 series of chips do exactly what you would expect them
to do: a full-speed USB device that emulates a standard serial
interface, with speeds from 50bps to 2Mpbs. The chip supports 5V
and 3.3V, and all the weird modem lines are supported. This chip
even has an IrDA mode, because wireless communication in the 90s
was exactly as rad as you remember.

15 | P a g e
CloudX Software
Downloading and Installing the IDE
CloudX Integrated Development Environment (IDE) is a very powerful Windows software
that comes with many other features and applications such as C text editor, compiler, code
loader, serial monitor terminal, visual block, LCD custom character generator and so on; it
has many other applications embedded in the IDE.
The early versions of the cloudx software has only the text editor and the code loader while
it uses MPLABX IDE as a third party software for libraries importations and compilations. In
June 2 2018, ByteHub Embedded announces the release of the first standalone cloudx IDE
platform v4.0 and later versions.
The newest version 7.0 is only compatible with the windows 32-bit and 64-bit operating
system while other operating system like the Mac is still in working progress.

The cloudx IDE, code library, softcard driver, Proteus simulating library, documentations and
every other material can be downloaded from cloudx.ng. The cloudx website is an online
maker community where people can learn, collaborate, make and share project, it’s also an
online component store that makes all electronics kits and tools for all kinds of project
accessible.

16 | P a g e
The cloudx software v7.0 is about 131MB or any later version are very easy to install, All the
IDE versions comes with comprehensive documentations and hardware specifications. Click
on the link to buy your own complete cloudx microcontroller board or the complete starter
kit.

Installing the CloudX IDE


The installation process of the cloudx software is very fast and easy, no special setting is
required. After installation, if you are opening the software for the first time then the
software will request to auto install the inbuilt compiler.

The software is built around the MPLAB-XC compiler.

Step1: Setup-MPLAB XC8 C Compiler, just click Next


Step2: Choose I accept the agreement
Step3: License Type, select Free
Step4: Installation directory, copy and paste or type
this new directory C:\Users\Public\CloudX\Compiler
to replace the existing one
Step5: Compiler setting, just click Next
Step6: Ready to install compiler
Then wait patiently till installation process is
complete
Setp7: installation complete – Licensing information, click Next and then Next.

17 | P a g e
Boom – EUREKA – we have successfully installed the full cloudx IDE, you can now launch the
software to get yourself acquainted with every
icon and their functions on the IDE. The software
comes with the full library documentation so that
users can easily understand and use the examples
in their projects. Creating a project with this
software is the simplest thing to do and it is one of
the easiest compilers for programming
microcontrollers. The manufacturer of this cloudx
software promised to launch better versions with
the best features and quality graphic user
interface (GUI).

Importing CloudX Library

CloudX software installation is not complete without the cloudx library because the whole
compiler operation depends on this library; the library is what makes programming on this
platform very easy and understandable unlike using other existing PIC compiler.

The current library v3.10 contains over 500 function libraries; users are therefore advised to
always check for library updates on the manufacturer’s website or follow their social media
platforms to know when there’s a new release. Libraries are pre-written set of codes
developed to reduce the number of lines used by the programmer, they simplifies and
optimizes the programmers code. I remember the first time I got a job to design a
microcontroller based facial recognition system; after several weeks on research works and
developing the GUI with MATLAB, I found it a lot more easier and faster to develop using
the OpenCV library created by Intel which enabled me to finish up the project within few
days, if not, the project would have been very difficult, frustrating and probably taken

18 | P a g e
months to program. Libraries serve a very big role in the area of programming and that’s
what makes the cloudx and other microcontroller boards with IDE unique.

After downloading the library, unzip it or copy out the


“CloudX” folder inside the zip file to any location on your
computer that you can easily access. Open the cloudx IDE and
click on “Tools” menu on the upper part of the software, click
on “Import Library” and choose the library folder. Now double
click on “Add Library” menu to check if library is successfully
added. If added, you will see all the libraries that came with
the version you downloaded. Now we’ve done the complete
software installation.

Understanding the Software

For you to use any software effectively, you will have to understand all the parts and their
functions. Now let’s take a look at the software more closely.

19 | P a g e
Menus

1. File is a menu that allows you to perform various operations like New, Open, Save
and Quit projects.
2. Library this menu contains include library, add library and import library to your
project.
3. Tools this menu allows you to perform few operations like launching other internal
application like Serial, Code Loader, Project Examples, Blockly, Custom LCD character,
PIC configuration.
4. Help this menu contains information about the developers team, sample projects,
user documentation, hardware specification and any other necessary information.

Button icons

5. Build used to save and build your code


6. Upload opens an interface that allows you to choose compiled project to upload into
the microcontroller board.
7. Serial Monitor this opens an interface that allows you to transmit and receive data
through serial port.
8. Visual block this launches an offline browser platform for embedded block
programming, the blocks are automatically converted to code for compilation.
9. LCD custom character enables users to design any custom character on an LCD.
10. Select device it’s a dropdown menu that allows you to choose a microcontroller, for
the compiler to know the controller you are working with.
11. Find is used to search for any text in the workspace or editor.
12. New this when clicked creates a new project
13. Open used to open saved projects on the computer
14. Save to save current project
15. Text Editor an environment for writing your codes, it’s also known as the workspace.
16. Compilation Messages this environment shows the compilation messages, enables
the programmer to know whether the codes are compiled successfully or not. If
there are errors, it gives hint on what the error might be.
17. Zoom this allows you to zoom in or out of the text editor.

20 | P a g e
Installing the Softcard Driver

Installing the softcard driver is as important as the IDE itself because this allows us to upload
our compiled code in the microcontroller board. The driver creates a terminal for the cloudx
to interact with the computer, if this driver is not properly installed, then your computer
won’t be able see the COM port that your softcard is connected to.

Browse to the cloudx website to download the ‘CloudX SoftCard Driver’. Unzip it and run
the file inside to install. If you are having difficulties installing this driver, please ensure that
your computer antivirus software is disabled. In case you want to know the COM port that
your computer is connected to. Search for ‘Device Manager’ on your computer, then click
on ‘Ports (COM &LPT)’. You will see the USB-SERIAL CH340 and the COM port it is
connected to.

21 | P a g e
Writing, Compiling and Uploading codes
The following are the stuff you need to have with you in order to start working on your
cloudx project:
1. CloudX board.
2. SoftCard.
3. USB programming cable.
4. Laptop/PC.
5. CloudX IDE (Integrated Development Environment).

To write a code, click on the New button icon, a text editor will open for you to type in your
code. Let’s consider a simple ‘hello world’ project to blink the on-board LED connected to
pin 1 on the controller. Just type or copy and paste the code below.

setup(){
//Setup here
pinMode (1,OUTPUT);

loop(){
//Program here
pin1 = HIGH;
delay(1000);
pin1 = LOW;
delay(1000);
}
}

The double slash // as discussed in the introduction means comment, the compiler doesn’t
see any text or code written after the double slash.
The //setup here and //Program here are only comments. As we won’t be going too deep to
explain the code here, we will learn in details in later chapters because our primary aim here
is to learn how to write, compile and upload into the microcontroller.
Now that we have the code on the text editor, the next thing to do is to save our project by
clicking on the Save button icon. Choose a desired location folder (you can either create a
new folder or use an existing folder) to save your project into, your code will be saved in ‘.x’
format. Also remember to ‘Select device’ type, in this project we’ve selected ‘M633’ since
that’s the controller board we are using.

Note: To open an existing project in a folder, just double click on the X file project since all
cloudx projects uses .x extension.

22 | P a g e
Now let’s compile our project, to do this, click on the ‘Build’ button icon. You will get some
compilation messages on the ‘Messages’ environment, the message will tell you if code was
successfully built or not.

23 | P a g e
If project was successfully built, then the compiler automatically generates some files like
COFF, Hex, and LST etc. inside the folder where you saved your project. In case you get
‘Build Failed’ then you can scroll up the message environment to get hints of possible
errors. Hints for errors are always in red texts.

At this stage, we have successfully written


and compiled our first project; we can now
load our hex file (machine language) into the
microcontroller board.

Click on the ‘Upload’ button icon, an


application called the ‘CodeLoader’ will pop-
up, use the Select COM Port drop down
menu to select the COM port where your
cloudx board is connected to, if you are not
sure of the port, you can start selecting the
available ports one after the other in order to
get the correct COM port. If the port selected
is correct, you will get information in the
message box in green, if not, it will appear in
red with error notification sound.
Now click on the Browse button to select the .HEX file of your project and then click the
Upload button to load the .hex file into the board. Immediately after uploading, we either
hear the error or success notification sound. If success, then –GBAM- you will see the LED on
the board blinking (ON and OFF every one second), so congratulation, we have just created
our first project.

How to find bugs (errors)

Finding errors is very important in programming, it is referred to as debugging, and the


compiler has been designed to give likely hints on what the error might be. Errors are illegal
operation performed by the user which results in abnormal working of the program.
Programming errors often remain undetected until the program is compiled or executed. If
you experience any error, just try to scroll around the Messages environment to find any
text in red. Any line you find a red text is likely to be giving hints on the particular error that
occurred. The developers of this IDE have been able to add the ability for the compiler to
detect and highlight the exact line of code that has the error in your text editor and they are
working very hard to ensure they make this feature smarter for the users.

24 | P a g e
CloudX Proteus Simulations
Many hardware programmers prefer to simulate their projects before implementing it in
the real world; you can download the Proteus software by Labcenter Electronics from their
website or any free cracked version on the internet. The cloudx Proteus library is compatible
with either the Proteus 7 or 8 versions.

Download the ‘CloudX Proteus Library’ from the cloudx web page, after downloading, unzip
it and copy the two files inside the folder to the ‘LIBRARY’ folder inside Labcenter
Electronics installation path in your computer, you can now reopen the Proteus application
and search for cloudx in the component list.

CloudX Visual Block Programming


This interface was initially developed for users who doesn’t want to write codes, but was
later restructured so that experts too can use it for their projects. The visual interface seems
to be fun because it allows you to select and join blocks inside the workspace. This visual
programming platform was developed on Blockly to support embedded C programming.

25 | P a g e
Blockly is a client-side JavaScript library for creating visual block programming languages
and editors. It is a project of Google and is open-source under the Apache 2.0 License. It
typically runs in a web browser, and
visually resembles Scratch. Blockly is
also being implemented for Android
and iOS; not all web browser based
features are available for Android/iOS.
Blockly uses visual blocks that link
together to make writing code easier,
and can generate JavaScript, Python,
PHP or Dart code. It can also be
customised to generate code in any
textual computer language.
The visual programming environment
supports the cloudx libraries and can be
used with all kinds of sensors and
electronics kits.

Click on the visual block button icon,


then it will automatically launch up with
your default browser, please ensure that your default browser is either ‘Google Chrome’ or
‘Mozilla Firefox’ or ‘Windows Edge’ as this eBook episode won’t focus on how to use this
platform but you can learn more on the manufacturer’s website. The below picture shows
the generated code ready to be copied into the text editor for compilation.

26 | P a g e
Programming in CloudX
Digital Input and Output
Now, we’ll try to blink an LED using cloudx. The Light Emitting Diode (LED) is one of the most
popular electronic components; it is widely used as an indicator. LEDs has two terminals
which are the Anode (+) and the Cathode (-), the longer pin is always the anode while the
shorter pin is always the cathode.

Our first project tutorial we will focus on blinking an LED. The cloudx microcontroller can
sink (absorb) and source (give out) a small amount of
current, so we can connect the LED directly to the digital
pins. We will use a resistor of about 220R to limit the
flow of current across the LED.

Materials Needed:
 CloudX M633
 SoftCard
 USB Cable
 Breadboard
 220R
 LED
 Jumper Wires

You can find all kinds of electronics components needed to make your projects on the
cloudx online store and they make free delivery for orders worth N30,000 in Nigeria
currency.

Before we start our first breadboard project, there’s an on-board green LED on the
microcontroller that is connected to pin1, it was intentionally placed on the board so that
users can test the microcontroller board by blinking the LED.

Now, to blink the on-board green LED, Connect your Softcard and the Controller to your
computer. Open the cloudx IDE and create a new project, you can check the previous
chapter again to understand how to create a project and writing to pin1.

You can also design the circuit below on a breadboard; you can learn more on how to use a
breadboard at Science Buddies.

27 | P a g e
Circuit Diagram

The above circuit shows how to connect an LED through resistor 220R to the pin1 of the
controller. Let’s see ways to communicate with the digital pins.

Method 1:

setup(){
//setup here
pin1Mode = OUTPUT;

loop(){
//Program here
pin1 = HIGH;
delay(1000);
pin1 = LOW;
delay(1000);

}
}

The setup() is a place where the program first get to when powered up before doing any
other thing in the project. Programmers are meant to do their settings in this place before
entering the loop function.
The loop () is a place where your program stays forever, this can also be liken to as while(1)
or for(;;) which means an endless loop. It keeps repeating every statement inside it forever.
pinxMode can be used to configure a digital pin either as output or input, where x
represents digits from 1 to 16. In the example above, I configured pin1 as output and that’s
why I wrote this statement pin1Mode = OUTPUT; while pin1 = HIGH means that we are
loading a positive 5V into pin1, delay(x) is used to delay the program where x is in
millisecond. Now having delay(1000) means we have delayed the ON time (5V) on pin1 for
1000ms (1second) before another step of instruction, the pin1 = LOW is used to load 0v into

28 | P a g e
the pin1 (OFF the pin) and delay for another 1second. The program keeps repeating the loop
forever which makes the LED to appear as if it is blinking.

Method 2:

setup(){
//setup here
pinMode(1, OUTPUT);

loop(){
//Program here
digitalWrite(1, HIGH);
delay(1000);
digitalWrite(1, LOW);
delay(1000);

}
}

PinMode(x, OUTPUT) function can also be used to configure a digital pin either as output or
input, where x represents digits from 1 to 16. In the example above, we still configured pin1
as output like this pinMode(1, OUTPUT); then using digitalWrite(x, HIGH) where x can be
from 1-16 and HIGH to load a positive 5V into pin1 and LOW to load 0V respectively. Please
note that you can also use PinSelect to replace the digitalWrite function.

LED Chaser
Now let’s create a chaser project where the LED lights up one after the other at a particular
time interval. You can make use of multiple LEDs to create an effect like the displays you do
see in barber’s shop or any restaurant. In this tutorial, we will be using four LEDs.

The LED chaser is one of the simplest microcontroller projects where you can change or vary
the speed of chasing LEDs by adjusting the delays or with the use of potentiometer. The
programming logic is to turn on one single LED at a time while the other goes off.

The cathode (-) of the LEDs are connected together and the anode (+) of each LED is
connected through a resistor to the P1, P2, P3, P4 pin respectively.

In my code, I want the LEDs to chase after each other at every 300 milliseconds but you can
vary this to your best interest and also modify the code to perform more operations.
Actually there are faster and more efficient way to write this code using loops, you will get
to write it yourself when you get to other episodes in this eBook.

29 | P a g e
Materials Needed:

 CloudX Controller
 SoftCard
 USB Cable
 Breadboard
 220R x4
 LED x4
 Jumper Wires

Circuit Diagram

The method below shows an easy way to write and understand how the chaser project
works. We try to turn ON one LED at a time while others are OFF at a very fast time interval
of 300ms (0.3s).

setup(){
//setup here
pinMode(1, OUTPUT); //P1 setup as Output
pinMode(2, OUTPUT); //P2 setup as Output
pinMode(3, OUTPUT); //P3 setup as Output
pinMode(4, OUTPUT); //P4 setup as Output

loop(){
//Program here
digitalWrite(1, HIGH); // ON pin1
digitalWrite(2, LOW); // OFF pin2
digitalWrite(3, LOW); // OFF pin3
digitalWrite(4, LOW); // OFF pin4

30 | P a g e
delay(300); // wait 0.3sec

digitalWrite(1, LOW); // OFF pin1


digitalWrite(2, HIGH); // ON pin2
digitalWrite(3, LOW); // OFF pin3
digitalWrite(4, LOW); // OFF pin4
delay(300); // wait 0.3sec

digitalWrite(1, LOW); // OFF pin1


digitalWrite(2, LOW); // OFF pin2
digitalWrite(3, HIGH); // ON pin3
digitalWrite(4, LOW); // OFF pin4
delay(300); // wait 0.3sec

digitalWrite(1, LOW); // OFF pin1


digitalWrite(2, LOW); // OFF pin2
digitalWrite(3, LOW); // OFF pin3
digitalWrite(4, HIGH); // ON pin4
delay(300); // wait 0.3sec

}
}

31 | P a g e
Writing to PORTs

A port is a group of pins associated to the same register of a microcontroller, port registers
stores the current values of I/O (data) lines. A port can be 8, 16 or 32-bit, a group of pins
that can be controlled at the same time using a register. The cloudx M633 microcontroller
board has 2 ports:

PORT1 and PORT2 registers, pin1 to pin8 are associated to PORT1


while pin9 to pin16 are asscoaited to PORT2, meaning you can
write to the eight pins at the same time.

In other to write to Ports, we have to configure portMode which


allows us to set a pin either as output or input. We will use
portWrite to either High or Low a pin. I will advice readers to
check the cloudx documentation for more understanding.

For example, portMode(1, 0b01001001). The 1 represents the


port you are communicating to, while 0b01001001 represents the
data you are writing to the port. The above sample code shows
that we are loading the data value 0b01001001 into Port1.

In this project, we will write to pins using port, what I want to


achieve is to turn ON four LEDs and OFF four LEDs at the same
time.

32 | P a g e
setup(){
//setup here
portMode(1, 0b00000000); //set all pins on port1 as output

loop(){
//Program here

portWrite(1, 0b11110000); // ON P8 - P5 and OFF P4 - P1


}
}

In the code, we set all Port1 pins as output using portMode and set the High and Low pins
using portWrite. You can tweak your code by flashing or chasing LEDs using any port.

Sound an Alarm (Buzzer)


Buzzers are electrical signalling device that are used to make sound alerts in our
projects/systems. It is a very small (though comes in different sizes) 2-pin component that
can fit in on a breadboard.

It has a wide application in electronics such as alarming circuits,


automobiles, communication equipment, and portable devices due to
its compact size.

33 | P a g e
A buzzer can be powered from a DC source of about 4v – 12v, which means, it can be used
directly with a 12v battery but it is recommended to use a 5V most times except for bigger
buzzer sizes. The buzzer actually gives a sound when HIGH signal is applied and the sound
stops when LOW.

The project above shows the buzzer positive (+) terminal connected to P1 on the
microcontroller board and the negative terminal (-) is connected to GND. Now, we will write
a code to turn ON the alarm every one second.

In the program below, the buzzer will continue to give an ON and OFF sound forever
because it’s written inside a loop. The sound will stop only when power is disconnected, the
beauty of this project is that it has a very wide application in technology. In the later
episodes of this eBook, we will design a tone generator for music using the buzzer.

setup(){
//setup here
pinMode(1, OUTPUT); //set pin1 as output

loop(){
//Program here
digitalWrite(1, HIGH); // ON alarm connected to pin1
delay(1000); // wait for 1 second
digitalWrite(1, LOW); // OFF alarm connected to pin1
delay(1000); // wait for 1 second

}
}

34 | P a g e
Controlling a Relay
A relay is an electrical controlled switch, they are switches that open and close circuits
electronically or mechanically. They are most times referred to as electromagnetic switch
operated by a small electric current that can turn on or off a much larger electric current.
One beautiful thing about a relay is that it helps to isolate a circuit. For example, controlling
electrical AC appliances from a DC circuit and it serves a very big role in the Internet of
Things (IoT) technology, every microcontroller doesn’t have enough power to switch a relay,
so you will have to use a transistor to trigger it, and you can find samples of how to switch a
relay using transistors on the internet. The relay module used in this tutorial has an on-
board transistor which makes it a lot easier to use.

There are relay modules that have up to 16 relays on the board.


It makes our project faster to make as most of this relay
modules operates with just 5V.

The relay module used here has the VCC (5V), GND and IN
(control pin), it has Normally Close (NC), Normally Open (NO)
and the Common (COM). You can learn more on the internet
about relay if you don’t understand those terms.

Note: Some relay modules are energised with a low signal and
some with a high signal and the particular relay used in this
tutorial is energised using low signal.

The module is powered through the 5V and GND pins, when IN pin is low, the relay triggers
and COM pin is internally connected to NC, when IN pin is high, the COM pin is back to NO.

35 | P a g e
setup(){
//setup here
pinMode(1, OUTPUT); //set pin1 as output
digitalWrite(1, LOW); // pin1 when low energises the relay IN pin

loop(){
//Program here

}
}

Read a Pushbutton
A pushbutton is an electronic component that connects two points in a circuit when being
pressed. It is a simple toggle switch used to control a device or machine. They also have a
very wide application in electronics and we come across them in our everyday life on almost
all appliances or gadgets we use in the house.

The button pressed detection can be designed in two forms namely; Pull-up or Pull-down.
When the pushbutton is open (not pressed) there is no connection between the two legs of
the pushbutton, so one of the pin is connected to GND through a pull-down resistor of 10k
ohms while its second pin is connected to 5 volts, this method is called Pull-down. Following
the diagram below, the cloudx microcontroller keeps on checking when a 5V is detected on
P1 before it takes actions.

36 | P a g e
When one leg of a pushbutton is connected to GND and the second leg (same connected to
a microcontroller pin) is connected through resistor 10k ohms to 5v, hence, checking for
LOW on the microcontroller pin, this method is called a Pull-up design.

In this quick tutorial, we will use a pushbutton to turn on an LED and delay for a second
when pressed.

setup(){
//setup here
pinMode(1, INPUT); //set pin1 as input
pinMode(2, OUTPUT); // set pin2 as output
loop(){
//Program here
if(digitalRead(1) is HIGH) // check if button is pressed
{
pin2 = HIGH; // send 5V on pin2
delay(1000); // wait 1 second
pin2 = LOW; // send 0V on pin2
}
}
}

37 | P a g e
Power your CloudX Board
It is very important that we know the various means to power up the cloudx microcontroller
board, I will explain briefly about this before we continue with the projects in the other
episodes.

a. You can power up the board from


the softcard USB port. You can
power the board directly from the
computer using the softcard but
this is not advisable when dealing
with serial devices like Wi-Fi,
Bluetooth, GSM etc. as the TX and
RX pins will be used by the softcard
chip hence not allowing any other
serial devices to communicate
properly.
b. Through the VIN pin on the power
socket(which takes within 7v to 12v
with maximum input current of
1Amps), the VIN has an on-board
protection diode that protects it from high input current or wrong polarity
c. Through the 5V inputs pin socket.

The CloudX Dev 1.0 needs a power source in order for it to operate and can be powered in a
variety of ways. You can do what most people do and connect the board directly to your
computer via a USB cable. If you want your project to be mobile, consider using a 9V battery
pack to give it juice. The last method would be to use a 9V AC power supply.

38 | P a g e
About Contributors
Author – Ayinde Olayiwola
An Embedded System Programmer, Robotic
Engineer, a Maker, Co-Founder and CTO ByteHub
Embedded, Inventor of CloudX, a Writer, Tech
Instructor and Educationist. He studied computer
engineering at Yaba College of Technology.
He has developed hundreds of embedded systems
based projects since the last 8 years and has
featured on various Nigeria tech platforms like
NaijaTechGuy and many more!
He has won several awards like the “Most Creative
Student of the Year” by Yabatech Heroes Awards
Twitter, Facebook (YAHA) and others.
Instagram, LinkedIn

Firmware Engineer – Fidel Onwuka


An Embedded Systems Engineer and a senior
firmware programmer on the CloudX MCU
board. He has developed hundreds of libraries
for various microcontroller boards. He is the
team lead for the CloudX Library and
Documentation department.

He has BSc in Electrical and Electronics


Engineering at University of Benin (UNIBEN).

He has contributed greatly to the opensource


hardware community and he is currently
Profile @ cloudx.ng lecturing at the CloudX Training Institiute (CTI).

39 | P a g e

You might also like