THE MICROCONTROLLERS

Presented by: Forest Shick, WA2MZG

For those who don’t know me I am Forest Shick, WA2MZG. I have been a ham for about 50 years.

Ground rule – ask questions on the current slide

First, I will briefly cover other microcontroller boards - quickly

1 Buyer Beware

$0.95

Is it worth it?

Buyer beware – I purchased these connectors from Adafruit. This picture shows 3 connectors. They lasted about 10 insertion removal cycles. Spend the money and buy a good connector

2 Qualifications

Did you ever wonder Why is this person presenting? I do!!

I am an electronics engineer. I have been designing and developing hardware and software for microcontroller based products since 1980. The first product I designed with a microcontroller was the hand held terminal on the left – in assembly language for RCA CDP1802 microprocessor.

My next project was the battery-operated printer in 1982.

My last 2 projects, around 2014, were the remote-controlled lights and the electronic beer tap. Those were written in C language and each had a different microcontroller from Motorola / Freescale.

For these products I did the electronic and software design and development. The beer tap also had software for a PC and a PDA with network, USB and IR communications.

I have designed products with microcontrollers from RCA, Motorola, Freescale, Microchip, Atmel, Texas Instrument and a DSP from Analog Devices

3 Single Board Computers

Beagleboard.org Raspberrypi.org Asus.com/us/single-board- computer/tinker-board

Just to acknowledge the existence of other devices – are not the only hobbyist microcontrollers, here are 3 single board computers from Beagle board, and Asus.

The Pi is a decent entry level product for the beginner - from a software point of view because of the support.

The Beagle Bone Black is a superior product relative to the Pi but is not as well supported.

The Asus – I have seen very little info on in the hobbyist market or anywhere else – only one article stating it was better for ft8 than the Pi because it was faster.

4 Other Microcomputer Modules

Pjrc.com/teensy/

3.2 – 72MHz Cortex M4 3.6 – 180MHz Cortex-M4F 4.0 – 600 MHz ARM Cortex M7 NANO – 16MHz 8 bit

Teensy 3.2 Teensy 4.0

Arduino NANO Teensy 3.6

Here is a less popular competitor to the Arduino. It claims it is compatible with the Arduino Integrated Development Environment – after adding additional files.

I have not had any experience with these boards, but they appear to have much the same capabilities but with a much faster microcontroller.

Notice the speed difference – The NANO is the slowest.

5 What is an Arduino?

These may be the 3 most popular Arduino boards: UNO, NANO & MEGA.

The UNO and NANO are very similar. The slide shows the relative size of the board.

Each has its own advantage: NANO – size MEGA – capability and features UNO – compatibility with Arduino shields

There are many vendors selling their own version of the Arduino boards and you can get them very inexpensively on-line from “unknown” vendors. As always – buyer beware. Remember the 2nd slide!

6 Other Arduino Modules

If you are purchasing an Arduino – beware – 6 of the smaller boards are all NANOs but with different functionality. The other 4 boards are just examples of the many boards available in the Arduino product line

7 Arduino Shields

There are also other boards or modules called shields available for the Arduino.

They will be plug compatible with the UNO and possibly the MEGA but not the NANO.

They will work with the NANO but you will have to wire them together.

There are many different shields from Arduino and other vendors.

There are motor driver shields, sensor shields, prototyping shields, SD card shields, GPS shields, robot and touch screen LCD shields and more

8 What is an Arduino good for?

• Weather station • Station control / accessories • Rotor controller • Keyer • Station 24 Hr Clock • Home automation • Use your imagination • Robot • Steppingstone to the • Light Controller Raspberry Pi and other more • Simple Security System powerful devices • Homebrew test equipment • Having fun • Game design – remember • Saying – “I DID THAT”!! Simon?

The big question is WHAT IS AN ARDUINO GOOD FOR?

Within limits – use your imagination. You are not going to make an SDR transceiver, but you could make a VFO.

See my projects on the table.

9 Prototyping

Let’s stop for a quick look at prototyping technique that might be useful.

Using a simple prototyping strip you can build many of your ideas.

Notice the blue, red and green lines – they show which contacts are connected. The black lines show the break

A 1/4W resistor is a good source of jumpers. Cut the wires off at the body of the resistor and they fit perfectly.

Old solid conductor phone lines make good jumpers.

Of course you can buy ready-made jumpers: M-M, F-F, M-F

10 Projects – Direct Conversion Receiver

NANO controls the LCD and the DDS – the VFO in this direct conversion receiver

Samples of projects using different Arduino boards. This uses a NANO This is a project from N6QW A direct conversion receiver I did a presentation on this Sept 2015, I believe

11 Projects

First steps in an SSB transceiver. This uses an UNO.

Using an UNO to control the VFO, BFO, LO and display.

This is the evolution of the direct conversion receiver shown on the last slide into a SSB transceiver.

It never was completed

12 Projects – Morse Code Tool

The Arduino MEGA was used because the SD card required a lot of memory space – program and RAM

Details are in a previous issue of the RAG

Using a MEGA This is what happens when you start a project and don’t stop. I have 2 more options for this: CW transmitter and morse code decoder but came to my senses and stopped.

13 Let’s Get Started

• To be fair – there is more relevant material than we can cover in 1 hour • We will start with an overview of the NANO • We will investigate the more common / easy to use features first • Initialization • Digital IO • Analog Conversion • As time permits, we will do a 10,000 ft view of as many features as possible

14 Comparison

UNO NANO MEGA Microcontroller ATmega328P ATmega328 ATmega2560 Digital IO 14 22 54 Analog IO 6 8 16 PWM 6 6 15 UART 1 1 4 Flash 32K 32K 256K RAM 2K 2K 8K EEPROM 1K 1K 4K Clock 16MHz 16MHz 16MHZ LED_BUILTIN 13 13 13 Pins 28 32 100

This is a chart of some of the highlights of the 3 popular Arduino boards. As you can see the UNO and NANO use the same microcontroller.

• Digital IO – these are logic level inputs and outputs basically used to set a condition or test a condition using only 2 states – ON and OFF or 1 and 0 or 5 volts and 0 volts • Analog IO – Inputs to a 10 bit A/D converter – 0 to 1023 • PWM – Pulse Width Modulator • UART – used for serial IO

It appears as if there are 26, 36, 85 available signals – there are not! These functions share common pins for example D0 & D1 are also UART RX & TX.

• Flash – program storage • RAM – variable and stack storage • EEPROM – non-volatile data storage (data is not lost when power is removed) • LED_BUILTIN – D13 on each board has a resistor and LED attached to it.

15 Microcontroller Block Diagram

ATmega328 ATmega2560

Block diagram for comparison of the 328 and 2560 microcontrollers Notice the increased complexity of the 2560 We do not have the time to examine each block in detail, but I will try to cover highlights of many of the blocks – enough so you can get started giving the Arduino boards a try.

Notice that each block diagram has a block titled AVR CPU. This is where the work is done

16 ATmega328 Data Sheet

BUT Arduino has done a great job of getting you started There are 294 pages in without having to depend this data “book” on this book. At some level of The Arduino web site has project complexity much to offer if you have many of these pages the time to look through it. will be important to your project TAKE THE TIME

NOTE: I have not found a hardware description – it could be there somewhere!

Let’s take a quick look at the data sheet for the ATmega329

There are 27,489 sqin of specifications – happy reading!

17 AVR CPU Core and Sample of Instruction Set

On the left is a block diagram of the AVR CPU.

The structures inside the heavy black line are memory and calculation hardware.

The structures to the right of the black line are the interface hardware to all the external I/O – digital, analog, etc.

On the right is a peek at some of the 130+ assembly language instructions which are common to both micros. A good reason to start with the Arduino IDE – it hides these instructions from you.

That is NOT C. These are the instructions higher level languages, like C, get compiled down to before they are assembled into code the micro can run.

NOTE: In the RARA Academy, we will be programming in C and using many of the Arduino predefined functions.

18 NANO / ATmega328 Features

• 32K bytes of • 8 channel, 10 bit A/D • 1K bytes of EEPROM • Internal temperature measurement • 2K bytes of RAM • USART (UART) • Two 8 bit Timer / Counter • SPI • Prescaler • I2C / IIC / TWI • Output Compare • Watchdog timer and oscillator • One 16 bit Timer / Counter • Analog Comparator • Prescaler • Interrupt • Input Capture • Output Compare • Pin Change Interrupts • Real time counter and oscillator • 23 Programmable IO lines • Six Pulse Width Modulator channels

This is a summary of the features of the microcontroller used on the UNO and NANO.

This is a very capable micro controller. You must take the time to read about and understand the features in order to take full advantage of the capability.

The Arduino IDE makes using a few of these features relatively easy – BUT – it doesn’t go into detailed use.

19 Shared Pin Functionality

As you can see there are only 32 pins, 7 of which are power and ground. Leaving only 25 for all the SHARED hardware functions See the multiple functions for each pin.

This slide shows the shared pin functionality. There are very few pins with a single use. Being shared – you can only use 1 function!

20 Arduino UNO Schematic

I have included the schematics of the UNO and MEGA for comparison

The red circle highlights the microcontroller

Everything else is fluff

The USB interface is more complicated than the actual micro

21 Arduino MEGA Schematic

Bigger micro – more connectors

The USB circuit is more complicated than the micro

22 NANO Schematic

Here is the schematic for the NANO. Pretty simple: power supply, micro, USB, connector

23 NANO Pin Out

Notice the multiple functions assigned to each pin

POWER - red GND - black Discussed on the next slide

Be aware of the multiple numbering systems. The VIOLET numbers are the names of the digital IO. The GREEN letter number combination are the analog inputs. The GRAY numbers are the pin number of the microcontroller.

Here is a pin out guide. NOTE – story about colors

Red is power Black is ground

Violet are the digital IO numbers. On the board, digital IO 2 thru 12 are labelled as D2 thru D12 BUT referring to them in a program use just 2 thru 12. There is no D included in the digital IO reference

Dark Green are the inputs to the A/D converter. On the board and in a program, they are referred to as A0 thru A7

The small dark gray squares are the pin numbers of the microcontroller IC

The light brown squares are the port names that are referenced in the microcontroller data sheet: PB0 – 5, PD0 – 7, PC0 – 6

24 3 communication functions are available, light blue squares: UART(RX TX), SPI (MISO, MOSI, SS, SCK) and IIC / I2C / TWI (SCL SDA)

Interrupts – INT0 & 1, pink

Pin Change Interrupts – PCINT0 – 5 & 8 – 14 & 16 – 23. I hope to have time to cover interrupts tonight

Light violet other functions: Timer, Output compare, input capture, analog comparator

24 Powering the NANO

• For development, when you connect the NANO to the USB port on your computer, the board is powered from the USB port – shown by the blue circles • External power may be applied to the Vin pin, red circle. It is then regulated to 5V to power the board and accessories • 5 volt power, violet circle, may also be applied to the 5V pin to power the board and accessories • The 3.3V pin, light blue is NOT power input. It is power output not to exceed 50ma.

The upper right block shows a 5 volt regulator from Vin. This is diode or’d with USB power. If both are present, the higher voltage will power the board.

The NANO is simply powered from the USB port of your computer. After your project is complete, you may want it to be stand alone. You can connect a 7 – 12 power supply to the Vin pin

You may also power it from the 5V or you may use the 5V as power to accessories.

The 3.3V pin is NOT an input. It is a power source for 3.3V logic but do not exceed 50ma

25 NANO Pin Functions

This slide simply shows the relationship between the board, schematic, microcontroller and functions

26 Arduino NANO Conclusion

There is not much to say about the simple Arduino boards: UNO, NANO & MEGA. These are the ones you want to learn because their functionality is relatively simpe.

The meat of the product is in the microcontroller and the Arduino IDE (Integrated Development Environment).

We will cover programming and the IDE at the RARA Academy this month.

Now let’s look into the microcontroller.

27 Initial Conditions

• Most pins are tri-state during reset • Many registers provide the initial condition of the pins • Properly terminate unused pins • DDR – 0 = Input and 1 = Output • WHY?

The first problem you will face when designing a circuit around a microcontroller is – What happens when the power is first applied?

Initially, the micro is in reset and most of the pins are tri-state – essentially floating – not driven high or low.

That’s the micro’s initial condition – what about the devices that are attached to the micro?

Some devices may have inputs to receive a 1 or 0 from the micro. Others may have outputs sending a 1 or 0 to the micro.

Device Inputs – initially the port pins are tri-state. Very early in the program you would initialize the micro port pins as output to drive the device inputs. For that very short time between power on and initialization, it is best to pull those signal high or low depending on circuit conditions.

Device Outputs – initially the port pins are tristate. Very early in the program you

28 will initialize the micro ports as inputs. Depending on your circuit design, you may need to add pull up or down resistors to your design.

That said, most of the port pins have a pull up resistor associated with it so it could be turned on with no need for an external resistor.

28 Digital IO – Chapter 13

Each of the 14 digital pins on the Nano can be used as an input or output, using pinMode(), digitalWrite(), and digitalRead() functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kOhms. In addition, some pins have specialized functions • Circles • RED – pull up resistor • BLUE – output • GREEN - input

• Squares • RED – Pull up disable signal • BLUE – reads the state of the port output • GREEN – reads the state of the port input • NOTE – the port output and port input may not be the same

This is the basic circuit of the digital IO pins.

It is important to realize that reading PINB and PORTB registers may give 2 different answers – so read the correct register.

For example, the pin is defined as an input and there is a 1 on the port pin. The Port B register has been written as a 0. If you read PORT B you will read a 0. If you read PIN B you will read a 1.

The pin is defined as an output and you write a 1 to PORT B, this will set the pin to 1. If you read PORT B you will read a 1.

What happens if you read PIN B and it is a 0?

It means there is a short or something is driving the pin to 0.

29 Digital IO

pinMode(8, OUTPUT); // Make D8 an output • pinMode(pin, mode) • pin 0 – 22 (D0 - D22) In C • mode: INPUT, OUTPUT, INPUT_PULLUP DDRB = (1<< DDB0); // Set DDRB0 to 1 (output) • digitalRead(pin) • Returns the state of the pin digitalWrite(8, 1); // set D8 to 1 • digitalWrite(pin, value) • Value – write a 0 or 1 to the pin In C PORTB = (1 << PB0); // set PORTB0 to 1

Registers • DDxn – Data Direction, Port x, Bit n • PORTxn – Data output port • PINxn – Data input port

Truth table for the digital IO Data Direction: • 0 = input • 1 = output • not obvious! 2 examples of Arduino functions vs C code

30 Digital IO Examples

Turn a LED on and off Read a push button switch Drive and read a 4 x 4 keypad

pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, INPUT_PULLUP); pinMode(9, INPUT_PULLUP); pinMode(10, INPUT_PULLUP); pinMode(11, INPUT_PULLUP);

PORTD = 0xf0;

val = PINB & 0x0f;

pinMode(8, OUTPUT); pinMode(5, INPUT_PULLUP); digitalWrite(8, 1); sw = digitalRead(5);

3 examples of using digital IO Turn on an LED – make the pin an output and write a 1 to the pin

Read a switch – make the pin an input with a pull up and read the pin

Driving and reading a 4 x 4 keypad, 16 buttons – can be done but too complicated for the simple Arduino functions – time to switch to C

31 A/D Converter Inputs – Chap 23

The Nano has 8 analog inputs, each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though it is possible to change the upper end of their range using the analogReference() function.

Analog pins 6 and 7 cannot be used as digital pins. Additionally, some pins have specialized, alternate functionality:

I2C: A4 (SDA) and A5 (SCL). Support I2C (TWI) communication

Analog Input Resistance: 100M ohms Reference Input Resistance: 23k ohms

It is important to know the input resistance. At this interface you are dealing with an analog part – not a digital part

32 A/D Converter

• analogReference(type) • DEFAULT – 5V power • INTERNAL – 1.1V • EXTERNAL – voltage on the AREF pin

• analogRead(pin) • A0 – A7 (ADC0 – ADC7)

analogReference(DEFAULT); // optional . . int value; . . // Measure the voltage on ADC1 value = analogRead(A1);

The analog reference can be used to change the full-scale voltage. Typically, you would use the 5V power supply with some filtering.

Or you could provide your own reference voltage, but it cannot be greater than the power supply voltage of the micro.

There is an internal 1.1V reference

Reading an analog input is simple as shown.

NOTE you must specify A0 to A7, unlike the digital which was just a pin number

33 A/D Converter

Reading a potentiometer Reading a temperature sensor

int val;

int val; analogReference(EXTERNAL);

val = analogRead(A0); val = analogRead(A6);

2 examples: First, using the supply voltage as a reference, read the voltage from the pot. NOTE, the internal 5V from the power supply could also be used.

Second, using an external 2.5V reference, read a temperature sensor. Reading the A/D converter is easy

34 Alternate Port Functions

• Clock oscillator • Timer Oscillator • Pin change interrupt • SPI • Output Compare • Input capture • Reset • A/D Converter • IIC / I2C / TWI • Analog Comparator • USART • External clock • External Interrupt

The complexity of the circuit to include all the main and alternate functions

35 Analog Comparator – Chap 22

The Analog Comparator compares the input values on the positive pin AIN0 and negative pin AIN1. When the voltage on the positive pin AIN0 is higher than the voltage on the negative pin AIN1, the Analog Comparator output, ACO, is set. The comparator’s output can be set to trigger the Timer/Counter1 Input Capture function. In addition, the comparator can trigger a separate interrupt, Uses exclusive to the Analog • Period / frequency measurement Comparator. The user can select • Voltage threshold Interrupt triggering on comparator • Phase measurement output rise, fall or toggle.

I have not had the opportunity to use this feature BUT – here is an example Period Measurement - AIN1 = 2.5VDC - AN0 has a SINEWAVE that is centered at 2.5VDC - Select rising edge interrupts - on each interrupt read the free running counter. - The difference between 2 successive measurements is the period of the waveform

We will cover timer counter and interrupts shortly

36 Communications

USART – Chap 19 SPI – Serial Peripheral Interface – Chap 18 • Full duplex • Full duplex • Baud rate generator • Master or slave • 5 – 9 data bit and 1 or 2 stop bits • 7 programmable bit rates • More • MISO – D12 – Master in / Slave out • MOSI – D11 – Master Out / Slave in • The NANO and UNO only have 1 • SCK – D13 – Serial Clock • It is assigned to the USB comms • SS – D10 – Slave Select

• D0 – RX • Use the SPI library • D1 - TX TWI – 2 wire Serial Interface – Chap 21 • Also known as IIC & I2C • SCL – A5 • SDA – A4

• Use the Wire library

You have 3 communications options available. The USART / UART is tied up with the USB interface to the IDE. It can be used in your program, but precautions need to be taken – not sure what they are. I haven’t used the USART

SPI & TWI are common interfaces to peripherals such as display and sensors.

The IDE provides functions to ease the burden of learning how these peripherals work – in detail

37 8 Bit Timer / Counter – Chap 14 & 17

• There are 2, 8 bit timer / counters • Output Compare • PWM • Frequency Generation

OUTPUT COMPARE • A value can be set in the OC register • When the counter reaches that value a port pin will change state

PULSE WIDTH MODULATOR • Creates a repetitive waveform with a duty cycle set by an 8 bit word

The 2 counters differ in features and operation, but the stated function are common to both.

The PWM feature is used by the analogWrite() command to create a varying duty cycle waveform

It may be filtered to create a DC voltage of varying amplitude

38 16 Bit Timer / Counter – Chap 15

• One 16 bit timer counters • Output Compare • Input Capture • PWM • Frequency generation • Event counting

39 Interrupts – Chap 11

There are 26 sources of interrupts

What is an interrupt? • An interrupt changes the normal flow of a program. • An interrupt is usually caused by an asynchronous process that requires attention • When an interrupt occurs, the current instruction is completed and control jumps to a special interrupt routine • When the interrupt routine is complete, the next instruction, in the main program, is executed

• The only interrupt that you have no control over is RESET • All other interrupts may be enabled or disabled • All interrupts are disabled at reset

40 External Interrupts – Chap 12

• External interrupts are triggered from a source outside of the microcontroller • There are 2 external interrupts, INT0 & INT1, which are addressed using the IDE functions • External interrupts can be triggered by: • Low level • Logic change • Falling edge • Rising edge • There are 23 pin change interrupts that the IDE functions DO NOT handle : PCINT0 to PCINT23 (No PCINT15) • Pin change interrupts are triggered by: • Rising and falling edges • You don’t get to pick which one

NOTE: A level sensitive interrupt creates a problem. Low Level Int – when this occurs, the pin is held low by some external device.

This causes the interrupt routine to be executed.

After the routine is executed, control is passed back to the main loop.

IF the low level has not returned to a high level, the interrupt routine will immediately be executed.

You will be stuck here until the low level has returned high.

You must have a plan for changing the level to HI

41 Internal Interrupts

The internal interrupts are triggered by actions of the internal peripherals – although external stimulus may be involved – receiving data on the TWI – the interrupt occurs because 8 data bits were transferred on the TWI lines

There are 20 internal interrupts

Beyond the scope of our discussion today

They are extremely useful but require studying the appropriate sections of the manual.

42 Timer / Counter and Pin Change Interrupt Example

• D14 & 15 Use PCINT to detect key down and up – triggering a timer for dot and dash length • D11 & 12 use PCINT to detect rotation of the rotary encoder and direction • A11 uses PCINT to detection pushbutton action • A8 uses a timer for tone generation • A10 use PCINT and a timer to detect and decode incoming Morse code

• TWI is used to communicate with the LCD

• SPI is used to read and write the SD Card

Dot / Dash length – A count that represents the desired element length is loaded into a timer. The timer is turned on which in this case increments the count. When the timer overflows and interrupt occurs This signals the end of the element

43 Resources

• Information & pictures are taken from the following: • Arduino web site • ATmega data sheet • Various advertiser in a Google search • Adafruit

On the table I have some examples of projects I have started 3 different Arduino boards.

Thank you for patiently listening

44