Programming and Interfacing With Microcontroller 1
Programming and Interfacing With Microcontroller 1
UNIT-I
Introduction – History - Creative Coding Platforms - Open Source Platforms – PIC - Arduino,
Sketch, Raspberry Pi, Iterative coding methodology – Python Programming - Mobile phones
and similar devices - Arm Devices - Getting used to Arduino - Sensor Characterization:
Safety, Basic Electronics (circuit theory, measurements, parts identification) Sensors and
Software: Understanding Processing Code Structure, variables and flow control, Interfacing
to the Real World
Introduction to Coding Platforms
Coding platforms are online tools and environments that allow users to write, test, and
debug code in various programming languages. These platforms are widely used by
developers, students, and educators for learning, practicing coding skills, participating in
competitions, and preparing for technical interviews.
They typically offer features like:
Interactive code editors
Compiler and runtime support for multiple languages
Problem-solving challenges
Code submission and automated testing
Gamification through points, badges, and leader boards
Creative coding refers to writing code primarily for creative expression — such as digital
art, generative visuals, music, animations, and interactive experiences. Unlike traditional
software development, creative coding often values aesthetics, exploration, and
experimentation over efficiency or functionality.
Processing the roots: The foundations of creative coding trace back to the ideas of
visual programming and early computer art, including work by pioneers like
Harold Cohen (creator of AARON) and institutions like MIT Media Lab.
Languages like Logo (with its turtle graphics) introduced many young learners to
visual expression through code.
Processing (2001)
Processing was a game-changer. Created by Casey Reas and Ben Fry, it is an open-
source programming language and IDE built on Java, designed specifically for artists
and designers.
Its simple syntax and visual output made it highly accessible for non-programmers
interested in digital art.
It inspired a huge community of artists, educators, and students around the world.
OpenFrameworks and Cinder were developed as powerful C++ toolkits for creative
coding.
They offer more low-level control and performance than Processing, making them
popular for installations, real-time visuals, and multimedia performances.
The shift to the browser and web technologies led to platforms like:
o p5.js (2014): A JavaScript version of Processing, maintaining its accessibility
and community spirit.
o Three.js: A JavaScript library for 3D graphics using WebGL, widely used in
interactive and immersive web projects.
o Tone.js: For creative sound synthesis and music-making in the browser
Open Processing: A sharing platform for p5.js and Processing sketches. Like
YouTube for creative code.
Glitch: A friendly platform for building and remixing full web apps, popular among
creative developers.
Replit: While general-purpose, it supports collaborative, creative, and interactive
coding projects.
ShaderToy: A platform focused on GLSL (OpenGL Shading Language) for creating
stunning visual effects with code.
Summary Table
Open-source platforms are tools, software, or hardware projects whose source code or
design is freely available for anyone to use, modify, and distribute. In the context of PIC,
these platforms help developers program and build systems with PIC microcontrollers
without relying only on proprietary software.
PIC Platform:
PIC stands for Peripheral Interface Controller, though it’s now more commonly referred
to simply as PIC microcontroller. The PIC platform refers to the ecosystem around these
microcontrollers, including:
PIC Microcontroller Families (e.g., PIC10, PIC12, PIC16, PIC18, PIC24, and
dsPIC)
Development Tools (hardware and software)
Programming and Debugging Interfaces
Software Libraries and IDEs
1. Wide Range of Models: From low-cost 8-bit MCUs to more powerful 16- and 32-bit
MCUs.
2. Low Power Consumption: Designed for battery-powered or energy-efficient
devices.
3. Integrated Peripherals: Timers, ADCs, UART, I2C, SPI, PWM, and more.
4. Ease of Use: Supported by user-friendly development tools.
5. Scalability: Easy to move between devices in the same family.
Development Environment
Home automation
Automotive systems
Industrial control
Consumer electronics
IoT devices
Robotics and embedded systems education
MPLAB X IDE:
MPLAB X IDE is the official integrated development environment (IDE) from Microchip Technology
used to program and debug PIC microcontrollers and dsPIC Digital Signal Controllers.
It’s built on the NetBeans platform and runs on Windows, macOS, and Linux.
Key Features of MPLAB X IDE:
Code Editing: Includes syntax highlighting, code completion, and refactoring tools.
MCC is a plugin for MPLAB X IDE that auto-generates C code for initialization and configuration of
microcontroller peripherals.
Great for beginners AND experts — speeds up development and reduces human error.
MCC is a plugin for MPLAB X IDE that auto-generates C code for initialization and configuration of
microcontroller peripherals.
Great for beginners AND experts — speeds up development and reduces human error.
These tools allow you to upload code, debug in real-time, and program devices in-circuit:
Budget-friendly
USB-powered
Supports a wide range of devices
Can be used for debugging and programming
Faster programming/debugging
More advanced than PICkit
Ideal for professional use
3. Curiosity Boards
Arduino Sketch:
An Arduino sketch is the name given to the program written for Arduino boards. It's the code that
tells the Arduino what to do—like turning on LEDs, reading sensors, controlling motors, etc.
1. File Type
Structure
cpp
CopyEdit
void setup() {
// runs once when the Arduino is powered on or reset
}
void loop() {
// runs continuously in a loop after setup() finishes
}
cpp
CopyEdit
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
}
4. Upload Process
5. Language Used
Raspberry pi:
Key Features
Common Models
Model Description
Raspberry Pi Zero Very small and cheap, great for compact projects
Operating System
Programming on Raspberry Pi
It’s like writing your code in layers—starting with something basic that works, then adding
features, improving performance, or fixing bugs in each new version.
How It Works
1. Start small: Write a simple version of the program that works (a "minimum viable version").
2. Test it: Make sure it runs and does what it should.
3. Get feedback or reflect: What can be improved or added?
4. Refine or expand: Make improvements or add features.
5. Repeat: Go through the process again with the next goal in mind.
Iterative Coding Cycle
Plan → Code → Test → Review → Improve → (repeat)
Benefits
Real-World Example
What is Python?
Python is a high-level, interpreted programming language known for its simplicity, readability, and
versatility. It’s great for both beginners and professionals.
Key Features
Simple Example
python
CopyEdit
# This program prints "Hello, world!"
print("Hello, world!")
Popular Libraries
Mobile Phones
Mobile phones are portable electronic devices used primarily for communication—calls, texts, and
now, much more.
Touchscreen interface
Run on operating systems like Android or iOS
Access the internet via Wi-Fi or mobile data
Support apps for messaging, gaming, productivity, and more
Have cameras, GPS, sensors, and more
Smart Devices
Smart devices are electronic gadgets that can connect, share data, and often be controlled
remotely. They are part of the Internet of Things (IoT).
Wi-Fi/Bluetooth connectivity
App integration (control via phone)
Voice control (Alexa, Google Assistant, Siri)
Automation (e.g., lights turn on at sunset)
Data sharing for analytics or syncing
How They Work Together
Smartphones often act as a control center for other smart devices. For example:
ARM devices are electronic devices powered by ARM processors — a type of CPU architecture
designed by ARM Holdings. ARM stands for Advanced RISC Machine.
These processors are widely used in mobile phones, tablets, smartwatches, Raspberry Pi, and many
IoT devices, because they are small, energy-efficient, and powerful.
Why Is It Important?
Before using a sensor in a real project (like a weather station or robot), you need to know:
Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s
perfect for beginners to learn about microcontrollers and electronics.
Sensor characterization means understanding how a sensor behaves and how its output relates to
what it’s measuring. This is crucial if you want accurate and reliable readings.
Example:
cpp
CopyEdit
int raw = analogRead(A0);
float voltage = raw * (5.0 / 1023.0);
float temperatureC = voltage * 100; // LM35 gives 10mV/°C
Serial.println(temperatureC);
Sensor Characterization: A Full Overview
1. Safety
Safety is always the first step when working with electronics and sensors.
Power Off Before Wiring: Always disconnect power before changing connections.
Use Correct Voltage: Supplying more voltage than a sensor can handle can damage it (and
your Arduino).
Avoid Shorts: Ensure no wires are accidentally touching or crossing.
Use Resistors Where Needed: Especially with LEDs, buttons, and some sensors (e.g., voltage
dividers).
Check Polarity: Don’t reverse VCC and GND—this can instantly destroy a component.
Understanding core electronics helps with proper sensor connection and data interpretation.
Components Identification:
Ohm’s Law: V = I × R
(Voltage = Current × Resistance)
Series vs Parallel Circuits:
o Series: Current is same everywhere.
o Parallel: Voltage is same across branches.
Voltage Dividers:
o Used with analog sensors like thermistors or LDRs.
o Formula:
Vout = Vin × (R2 / (R1 + R2))
A device that converts physical phenomena (temperature, light, pressure, etc.) into an electrical
signal.
Tools Use:
In Arduino, your code is written in a simplified C/C++ language. Every program is called a "sketch"
and follows this basic structure:
cpp
CopyEdit
void setup() {
// runs once when the Arduino starts
}
void loop() {
// runs repeatedly forever
}
What Each Part Does:
setup() – Used to initialize things like pin modes, serial communication, sensors, etc.
loop() – Where your main program runs over and over again.
Example:
cpp
CopyEdit
void setup() {
pinMode(13, OUTPUT); // set pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // turn LED on
delay(1000); // wait 1 second
digitalWrite(13, LOW); // turn LED off
delay(1000); // wait 1 second
}
Variables
Variables are used to store data (numbers, text, etc.) that your program uses.
Flow Control
These are the tools that let your program make decisions and repeat actions:
if, else – Decision-making
cpp
CopyEdit
if (temp > 30) {
// turn on fan
} else {
// fan stays off
}
cpp
CopyEdit
for (int i = 0; i < 10; i++) {
// do something 10 times
}
cpp
CopyEdit
while (digitalRead(buttonPin) == LOW) {
// wait for button to be pressed
}
cpp
CopyEdit
switch (mode) {
case 1: // do something; break;
case 2: // do something else; break;
}
This is where things get exciting! You write software to interact with the physical world using inputs
(sensors) and outputs (actuators).
cpp
CopyEdit
int lightLevel = analogRead(A0);
cpp
CopyEdit
bool motion = digitalRead(2);
Outputs: Controlling Things
cpp
CopyEdit
digitalWrite(5, HIGH); // turn on motor
Communication with the PC
cpp
CopyEdit
Serial.begin(9600); // start serial
Serial.println(lightLevel); // print value
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(100);
}