9. Embedded programming¶
In this week I will be programing the PCB board which I have made in Electronics Design Week
Step 1: Install ISP Programmer driver¶
So I have downloaded it by using this link which has the setup guidline as well.
Step 2: Arduino software setup¶
Arduino is a software application and has a source code editor that uses C language for programing. IDE stands for Integrated Development Environment. The software needs to know the type of my board so I had to download the type of my board through this Link then I have changed the tools acccording to my board:
Step 3: Arduino Basics¶
Sktech:¶
is where you can write your code and upload it to your board it has instructions which can help you to communicate with your board. every sktech has two main sections
void setup:¶
its where you write the things of the fuctions which you want them to be done when the board is powerd or reset. for example for initalizing variable and pin mode
void loop:¶
where you write the functions that you want them to run and repeat continuously until you power off. here where you can write the main logic of the code.
PinMode:¶
its a function which you can use it to set a pin on your board as input or output.
pinMode(PA2,INPUT); //sets pin 2 as input pinMode(ledPin, OUTPUT); //sets LED pin as output
According to Attiny44 Datasheet the following screenshot illustrates the Microcontroller pins configurations which I will need to look at to know the pin numbers when I am using any funtion.
TX and RX¶
FTDI Driver is a driver that helps my computer understands what my board is saying by converting RS-232 or TTL serial transmissions to USB signals.
I am communicating with the FTDI through TX and RX as ATTiny has no hardware TX and RX
Reading/writing digital values:¶
I am connecting the phototransistor and LED to the digital pins
digitalWrite(ledPin, LOW);// makes the output voltage on LED pin 0V so the light will be off digitalWrite(ledPin, HIGH);// makes the output voltage on LED pin 5V so the light will be on.
they are a reference of codes that can help you to connect a module, display, sensor, etc.
Step 4: My code¶
I started my code with adding a library for serial code which I will be using to get values from the phototransistor.
#include <SoftwareSerial.h>
After that, I have defined my TX and RX pins
#define RX PA1 // *** D3, Pin 2 #define TX PA0 // *** D4, Pin 3
then I have defined my pins in the code for
const int buttonPin = PA3; const int ledPin = PA7;
const is abbreviation of constant which declares the variable to be constant numbers and it cannot be altered by the program and int is a datatype variable that holds a whole number between -32,768 and 32,767.
So I used const int to define my pins on the software. where float is a datatype used to represent a fraction. This entails the use of decimal points for floating point numbers. and I used it for the values of phototransistor.
float lightSens = 0.0;
after that I have added the functions that I want them to be run once only when the board is powered in the void setup section.
void setup() { pinMode(PA2,INPUT); //phototransistor pin pinMode(ledPin, OUTPUT); Serial.begin(9600); //opens serial port, sets data rate to 9600 bps Serial.println("hi there"); }
I used pinmode to determine the input and output pins from the board and according to my schematic board that I have done in Electronics Design Week
and Serial begin is a funtion that sits the data rate in bits per second (baud) for serial data transmission
where serial. printIn funtion is a funtion that prints data and as I have wrote it on the void setup it will print data just once
void loop() { lightSens = analogRead(PA2); if ( lightSens >= 300 ){ Serial.println(lightSens); digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, HIGH); //digitalWrite(ledPin, LOW); } }
for the voild loop first I have defined the array of lightSense as its the data that comes from PA2 which is the phototransistor
I added the if command which says if data that comes from the phototransistor is more or equal than 300 as the data is increased whith darkness and decrease whith the lightness, the LED pin should be off and else which means when the data from the phototransistor is less than 300, the LED should be on
Embedded Programming from ZAINABARAHMAN on Vimeo.
Step 5: Refering to the datasheet¶
It is always good to refer to the datasheet for having more knowledge about the Microcontroller I refered to it to know the operating voltage and the ADC Which stands for Analog Digital Converter which is the ability of converting the analog value to digital codes