Programming A Driverless Car Chapter 12: Programmable Systems and Microcontrollers

Programmable System:

A programmable system is anything that can carry out a sequence of operations via instructions from the user. An example of this is a VCR which can be set to record, play via a remote control or touchpad. An alarm clock is a programmable system as is a microwave oven, an iPod, a TV or a laptop.

A non-programmable system is one with a fixed function i.e a thermostat or a standard light switch. Processor, RAM, Flash memory and I/O ports.

Microcontrollers:

-A micro controller can be defined as a “Computer-on-chip”

-As CPU,a Microcontroller also has:
 Input/Output Ports
 A processor embedded into a single chip

 Memory

-A micro controller is a complete self-sufficient Intelligent system usually requiring no external components.

Microprocessors and microcontrollers:

Microprocessors: Central processing Unit (CPU) on a single integrated circuit (IC). Ex. AMD Athlon, Intel Pentium

Microcontrollers: An integrated CPU, memory and peripherals capable of input and output. Ex. Atmel AVR, PIC. They are cheap.

Types of Microcontrollers:

Various types of families of microcontrollers

  • AVR Family
  • 8051 Family
  • PIC Family
  • ARM Family

ATMEL AVR-

The AVR is a Modified Harvard architecture 8-bit RISC single chip microcontroller developed by Atmel in 1996

The AVR was one of the first microcontroller families to use on-chip flash memory for program storage.

Basic Families:

  • tinyAVRs Ex. ATiny11
  • megaAVRs. Ex:ATmega8,ATmega16,ATmega32
  • XMEGA. Ex.ATxmega64

 

Device Architecture:

ALU- Arithmetic Logic Unit for fetching, decoding and execution of instructions.

Registers: Usually 32*8 bit registers. The CPU does all the calculations on these registers.

Program Memory- Storage of instructions that form the program. A non-volatile Flash memory is used to store the program. The size of the program memory is occasionally indicated in the naming of the device itself.

SRAM- Storage of data-variables, stack etc

EEPROM- Internal Electronically Erasable programmable Read Only  Memory for semi-permanent data storage. Like Flash memory,EEPROM can maintain its content when electrical power is removed.

 

Arduino:

Arduinos are the development port which uses microcontroller families if AVR and based on that it customises the use of microcontrollers to design small scale programmable systems. They are development series of board with micro controllers and can be used as ready to use. They are open source.

 

  • Open source electronic prototyping platform
  • You can make your own board
  • Easy to use hardware
  • Cheap,easily available
  • Open source software
  • Very widespread, many projects are available
  • Extra hardware available
  • USB programmable

 

Arduino I/O boards

Fig: Arduino Input/Output Ports

Here, we will use Arduino UNO boards.

  • 14 digital I/O  pins (0-13)
  • 6 Analog In(pins 0-5)
  • 6 Analog Out( pins 3,5,6,9,10,11)

Fig: Arduino UNO

 

Programming in Arduino:

Atmega 328 pin mapping-

We first reset the device and upload the sketch on Arduino and LED starts blinking.

  • Write Program
  • Compile(Check for errors)
  • Reset board
  • Upload to board

Fig:Flowchart representing Ardunio Working

 

An arduino sketch has 3 things majorly- Global Variables, setup(), loop()

  • Declare the variables at the top
  • Initialize- setup()- run once at the beginning, set pins
  • Running- loop() – run  repeatedly, after setup()

pinMode-

Configures the specified pin to behave either as an input or an output. See the description of digital pins for details on the functionality of the pins. As of Arduino 1.0.1, it is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pullups.

Syntax- pinMode(pin,MODE)

Pin- the number of pin whose mode you wish to set

MODE- INPUT,OUTPUT or INPUT_PULLUP

digitalRead()-

Reads the value from a specified digital pin, either HIGH or LOW.

Syntax- digitalRead(pin)

pin-The pin whose input you wish to read.

digitalWrite()-

Write a HIGH or LOW value to the pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V for HIGH, 0V for LOW.

If the pin is configured as an INPUt, digitalWrite() will enable(HIGH) or disable(LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLOUT to enable the internal pull-up resistor.

Syntax- digitalWrite(pin,value)

analogRead()-

Reads the value from the specified analog pin. The Arduino board contains a 6 channel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts/1024 units or 0.0049 volts per unit.

Syntax- analogRead(pin,Value)

Pin- The number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)

analogWrite()-

Writes an analog value(PWM wave) to a pin. Can be used to light a LED at varying brightness or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite(). The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz.

Syntax- analogWrite(pin,Value)

Pin- The pin to write to

Value-the duty cycle between o to 255

ADC

In electronics, an analog-to-digital converter(ADC) is a system that converts an analog signal, such as a sound picked up by a microphone or light entering a digital camera, into a digital signal.

An ADC may also provide an isolated measurement such as an electronic device that converts an input analog voltage or current to a digital number proportional to the magnitude of the voltage or current.

Arduino Analog Input-

  • Resolution– The number of different voltage levels (ie states) used to discretize an input signal
  • Resolution values range from 256 states(8 bits) to 4,294,967,296 states (32 bits)
  • The Arduino uses 1024 states (10 bits)
  • Smallest measurable voltage change is 5V/1024 or 4.8mV
  • Maximum sample rate is 10,000 times a second

LAB: Working with Arduino Programming Language, Controlling LEDs, Switches

int led=13;  —-LED is an output device

int led2=12;

void setup()

{

pinMode(led,OUTPUT);

pinMode(led2,OUTPUT);

}

void loop()

{

#switching on
digitalWrite(led,HIGH);

delay(2000);

#switching off

digitalWrite(led,LOW);

delay(2000);

digitalWrite(led2,LOW);

delay(1000);

digitalWrite(led2,HIGH);

delay(1000);

}

Control switches:

int sw=11;

int led=12;

void setup()

{

pinMode(11,INPUT);

pinMode(12,OUTPUT);

}

void loop()

{

int val=digitalRead(sw);

digitalWrite(led,val);

}

LAB: Interfacing BO DC Motor with Arduino Uno R3

 

int m1=3; //defining terminal 1 of motor connected to pin 3 of arduino
int m2=4; //defining  terminal 2 of motor connected to pin 4 of arduino

void setup()

{

pinMode(m1,OUTPUT); //defining pin 3 as OUTPUT

pinMode(m2,OUTPUT); //defining pin 4 as OUTPUT

}

void loop()

{

#to move in clockwise direction

digitalWrite(m1,HIGH);

digitalWrite(m2,LOW);

delay(3000)

digitalWrite(m1,LOW);

digitalWrite(m2,HIGH);

delay(3000);

}

Check the port(Arduino port). Upload the code and check the output. This is how you need to make the connections. When we do the connections, we will see the motor moving.

Fig: Representation of the connections

 

Recommended Reading: Programming A Driverless Car Chapter 11: Robotics actuators, Sensors, and Batteries

Next: 

To start reading from a topic of your choice, you can go back to the Table of Contents here

This course evolves with your active feedback. Do let us know your feedback in the comments section below.

Looking for jobs in Artificial Intelligence? Check here

Written By
More from Level Ten

Programming A Driverless Car Chapter 15: HC05 Interfacing

Bluetooth Technology Bluetooth is a wireless technology standard for exchanging data over...
Read More