# Arduino UNO board as ISP programmer and serial to usb convertor with the attiny
## arduino uno
The Arduino Uno is an open-source microcontroller board based on the Microchip ATmega328P microcontroller and developed by Arduino.cc. The board is equipped with sets of digital and analog input/output pins that may be interfaced to various expansion boards and other circuits.
## requirements
* Download and install the [Arduino IDE](https://www.arduino.cc/en/main/software) software
* Install the attiny boards as per the instructions below
## Adding the attiny boards to the Arduino IDE
By default, the attiny boards are not added to the Arduino IDE. Which means that in order to program any Attiny based circuit using the Arduino IDE, the board support must be added.

File > preferences

Add the board URL below in the additional boards URL section
https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

tools > board > board manager

find and install the attiny boards

Select the correct settings for the attiny board you have. If an external oscillator is present select external and the oscillator value, if not, select internal 8MHz.
## FTDI (serial to usb)
The Arduino Uno contains a serial to USB chip built-in in the microcontroller board. This chip allows programming the atmega328p chip directly via USB and it allows reading data and displaying them on the serial monitor or serial plotter in the Arduino IDE.
By default, pin 1 and 0 are the transmit and receive pins on the board. Those are labeled with TX (transmit) and RX (receive). We can use those to utilize the arduino UNO board as a serial to USB device for the Attiny or any chip with no serial support.


### why do we need to do this? cant we plug the attiny to the PC directly?
Not really. The attiny chip has no native hardware serial support and no TTL Serial to USB support. If we wanted to read data from the attiny and display it on the PC, we need to use software serial and external hardware.
In this example, data will be read from an analog sensor connected to an attiny44 and displayed on the PC screen via the Arduino IDE serial monitor / serial plotter.
### ATtiny code
The first step is to determine the pins that will be used. We will need to know the sensor pin and the TX and RX pin. Any two input/output pins of the attiny can be used as TX and RX.

For this example the pinout is as follows:
sensor > pin 3 > PA3
TX > pin 0 > PA0
RX > pin 1 > PA1
The sensor used is an analog light sensor. It can detect when light & dark conditions. The Raw sensor data will be read and displayed on the computer.
The attiny44 does not have hardware serial support, which means that we need to use a library called Softwareserial. The SoftwareSerial library has been developed to allow serial communication using software to replicate the functionality (hence the name "SoftwareSerial"). It is possible to have multiple software serial ports with speeds up to 115200 bps.
#include < SoftwareSerial.h> //including the library that lets us use software serial
SoftwareSerial Monitor(PA1, PA0); //declaring the monitor with RX then TX
int RawData = 0; //variable to store our sensor data
void setup() {
Monitor.begin(9600); //buad rate of the serial
pinMode(PA3, INPUT); //sensor
pinMode(PA0, OUTPUT); //TX
pinMode(PA1, INPUT); //RX
}
void loop() {
RawData = analogRead(PA3); //reading sensor data
Monitor.println(RawData); //sending the data through serial
delay(50); //small wait
}
Upload the code to the attiny board
### connection
The attiny board needs power, the best method to provide power is through the Arduino UNO directly by connecting a jumper between the attiny VCC and GND to the Arduino UNO 5V and GND.
The sensor can be connected to any empty pin in the attiny44.
The attiny RX and TX connect to the Arduino UNO TX and RX.
tiny RX > UNO TX
tiny TX > UNO RX


### Arduino UNO code
The arduino IDE displays the values received on the UNO serial at the serial monitor and serial plotter by default.
An empty code file needs to be uploaded into the UNO for the best results.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
### Results
Select tools > serial monitor or serial plotter to view the data received.



[video demo monitor](..\img\fab\1\VID_20200330_140617.mp4)
[video demo plotter](..\img\fab\1\VID_20200330_140725.mp4)
## Arduino as ISP programmer for the Attiny
Unlike most commercial microcontroller boards available in the market, A circuit containing only an attiny microcontroller cannot be programmed by simply plugging it into a computer and uploading code to it. A programmer is required. There are multiple programmers available in the market and there are programmers that can be manufactured with minimal components as well.

The ATtiny44 can be programmed by In-system programming (ISP) which means that it can be programmed while installed in a complete system, rather than requiring the chip to be programmed prior to installing it into the system.
The Arduino UNO can be used as an In-system Programmer.

### Arduino UNO code
The code used is already available in the Arduino IDE examples.

Go to file > Examples > ArduinoISP > ArduinoISP

Upload the code to the arduino after making sure the Arduino UNO board is selected from the tools settings.
### Connection
Six wires are needed. Three of these wires are referred to as the Serial Peripheral Interface (SPI) and are the Master In - Slave Out (MISO), Master Out - Slave In (MOSI), and Serial Clock (SCK). The "Master" is the ISP or the device you are using to program the AVR chip. The "Slave" is the AVR chip being programmed. The other three wires are for the 5V power supply (VCC), Ground (GND), and Reset (RESET).
The connection must be made from the Arduino UNO pins to the attiny pins as follows:
UNO 5V > VCC
UNO Ground > GND
UNO Pin 13 > SCK
UNO Pin 12 > MISO
UNO Pin 11 > MOSI
UNO Pin 10 > RESET


### Uploading Attiny code
The attiny is now ready to be programmed. open a new code file and write the attiny code. Make sure the correct settings are set in the tools to program the attiny board available.
The blink code is a good code to test the connection and make sure that everything is okay.

make sure the programmer selected is Arduino As ISP

Upload the code to the Attiny
sketch > upload using programmer
If everything is done correctly, a "done uploading" message will appear.
