top of page

Search Results

125 items found for ""

  • Arduino_7SEGMENT | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    Introduction to 7Segment Display : The Seven Segment displays are commonly used to display alpha-numerics, which have 7 straight LEDs arranged in 8 format and a dot shaped LED at right bottom corner, packed in a rectangular shape There are two types of 7 segment displays, while considering internal electrical circuit. 1) Common Anode Displays 2) Common Cathode Displays ​ If all the anodes of the 8 LEDs in the package are connected to common wire, which is available as one or two pins externally and all the cathode pins are available individually, externally, then it is called Common Anode (CA) displays . ​ Similarly, if all the cathodes of the 8 LEDs in the package are connected to common wire, which is available as one or two pins externally and all the anode pins are available individually, externally, then it is called Common Cathode (CC) displays . ​ The 7 segments are available in various sizes from 1/4" to 4" or more. The pin diagrams (pinouts) depends on manufacturers and size. Now-a-days, 7 segments displays can emit various colours (using various colour LEDs), like, red, green, blue, white and multi-colour also. The seven segment LEDs are identified as a,b,c,d,e,f,g and the round LED as dp (decimal point). Some vardious sizes of 7 segment displays and a model 7 segment display with segment and pin connections are shown here, for easy understanding. Connecting 7Segment Display to Arduino : To connect a 7 segment display to Arduino, the 8 pins of built-in LEDs (available in rectangular package) have to be connected to 8 pins of Arduino, through series resistances. The common pin(s) have to be connected to either 5V (for Common Anode) or Ground (for Common Cathode) pins. ​ A test circuit diagram of conecting 7 segment display (CA & CC) to Arduino Nano is shown below. Now, open File->New in Arduino IDE, then initially, declare the pin connections of 7 segment display to Arduino pin connections, in proper order, as shown below. ​ // Pin connection sequence a , b , c , d , e , f , g , dp int segPins[] = { 4, 5, A4, A3, A2, 3 ,2, A5 }; ​ Then, set the declared pins as out put using pinMode function in setup function, as shown below. ​ void setup() { // set all 8 pins as output for (int i = 0; i < 8; i++) { pinMode(segPins[i], OUTPUT); } } ​ Then, let the each segment glow, one-after-another in sequence, as a self test. So, in the loop function, start with switching OFF all the segments and dp in a for loop using digitalWrite as LOW . Then, switching ON each segment, one-by-one, in consecutively using digitalWrite as HIGH . A delay function is used to visualize the sequence of glowing each LED easily. ​ void loop() // for CC display { for (int i = 0; i < 8; i++) { digitalWrite(segPins[i], LOW ); // 0 } for (int i = 0; i < 8; i++) { digitalWrite(segPins[i], HIGH ); // 1 delay(1000); } } ​ The above sketch is written for Common Cathode display. For, Common Anode Display, invert the key word LOW with HIGH and vice-versa. In case of Common Anode, the common pin should be connected to 5V pin and incase of Common Cathode, the common pin should be connected to GND pin of Arduino. ​ In place of HIGH, 1 may be used and similarly, in place of LOW, 0 (zero) may be used. The loop function is modified for Common Anode and using 0 and 1 in place of LOW and HIGH keywords below. ​ void loop() // for CA display { for (int i = 0; i < 8; i++) { digitalWrite(segPins[i], 1 ); // HIGH } for (int i = 0; i < 8; i++) { digitalWrite(segPins[i], 0 ); //LOW delay(1000); } } ​ So, you have to invert the LOW to HIGH and vice-versa for CA and CC displays and connect to 5V or GND pins for CA or CC displays. The successive coding (sketch) is written for CC only and follow the same rule for CA displays, which are marked as remarks /* */ in brown colour. Showing Numbers on 7Segment Display : To show Numbers on a 7 segment display using Arduino (or a micro-controller), the segments' ON/OFF control is to be declared initially, in an array, for easy editing and accessing the code to be displayed. Here, integer array is declared, using code 1 for ON and 0 (zero) for OFF state of each segment and dp sequentially, to display number 0 to 9 and dp LED (as 10). Remember to declare the array before setup function, as shown below. ​ // Pin connection sequence a , b , c , d , e , f , g , dp int segPins[] = { 4, 5, A4, A3, A2, 3 ,2, A5 }; int number[11][8] = { { 1, 1, 1, 1, 1, 1, 0, 0 }, // 0 { 0, 1, 1, 0, 0, 0, 0, 0 }, // 1 { 1, 1, 0, 1, 1, 0, 1, 0 }, // 2 { 1, 1, 1, 1, 0, 0, 1, 0 }, // 3 { 0, 1, 1, 0, 0, 1, 1, 0 }, // 4 { 1, 0, 1, 1, 0, 1, 1, 0 }, // 5 { 1, 0, 1, 1, 1, 1, 1, 0 }, // 6 { 1, 1, 1, 0, 0, 0, 0, 0 }, // 7 { 1, 1, 1, 1, 1, 1, 1, 0 }, // 8 { 1, 1, 1, 1, 0, 1, 1, 0 }, // 9 { 0, 0, 0, 0, 0, 0, 0, 1 } // dot or dp }; There is no change in setup function. Now, you have to control the output code at 8 pins of Arduino board. For showing the number as per the code declared in the array, a user defined function, show , is to be added in between setup and loop functions. ​ void show ( int n ) // n is the row number of the array { for (int i = 0; i < 8; i++) // elements (or columns) in each row of array { // write each element to each pin of Arduino as per the value 1 or 0 digitalWrite(segPins[i], number[n][i]); // for CC /* digitalWrite(segPins[i], 1-number[n][i]); // invert for CA */ } } ​ Now, you have to write code to display the numbers (and dp) sequentially in loop function as shown below. ​ void loop() { for (int i = 0; i < 11; i++) // select each row of the array { show ( i ); // show the code written for the row in the array delay(1000); } } ​ The output is shown below: Showing Alphabet on 7Segment Display : 7 segment displays are highly used to display numbers, but, it may be used to show specific Alphabet also, which are easy to read / understand. For showing Alphabet, similar to numbers, a new array declaration is required, using code 1 for ON and 0 (zero) for OFF state of each segment. The array is declared before setup function, as shown below. ​ // Pin connection sequence a , b , c , d , e , f , g , dp int segPins[] = { 4, 5, A4, A3, A2, 3 ,2, A5 }; int alpha[16][8] = { { 1, 1, 1, 0, 1, 1, 1, 0 }, // A { 1, 1, 1, 1, 1, 0, 1, 0 }, // a { 0, 0, 1, 1, 1, 1, 1, 0 }, // b { 1, 0, 0, 1, 1, 1, 0, 0 }, // C { 0, 0, 0, 1, 1, 0, 1, 0 }, // c { 0, 1, 1, 1, 1, 0, 1, 0 }, // d { 1, 0, 0, 1, 1, 1, 1, 0 }, // E { 1, 0, 0, 0, 1, 1, 1, 0 }, // F { 1, 0, 1, 1, 1, 1, 0, 0 }, // G { 0, 1, 1, 0, 1, 1, 1, 0 }, // H { 0, 1, 1, 1, 1, 0, 0, 0 }, // J { 0, 0, 0, 1, 1, 1, 0, 0 }, // L { 1, 1, 0, 0, 1, 1, 1, 0 }, // P { 0, 0, 0, 1, 1, 1, 1, 0 }, // t { 0, 1, 1, 1, 1, 1, 0, 0 }, // U { 0, 1, 1, 1, 0, 1, 1, 0 } // Y }; There is no change in setup and show functions. Now, you have to modify code to display the alphabet ( now the array size is 16 instead of 11 ) sequentially in loop function as shown below. ​ void loop() { for (int i = 0; i < 16; i++) // select each row of the array; 16 rows { show ( i ); // show the code written for the row in the array delay(1000); } } ​ The output is shown below: Playing DICE with 7Segment Display : As an entertainment, you may write a code to display random numbers 1 to 6, like a Dice (which is having 6 faces indicating 1 to 6 with dots on each face). The circuit diagram is shown below for digital dice display. There is no much difference w.r.t. previous circuit, but, a push button switch is added in between pin 7 and pin GND of Arduino Nano board. ​ You may connect the push button switch to any button and declare the same number in the sketch (instead of 7). The complete sketch for DICE is shown below with comments/remarks for easy understanding. ​ // Pin connection sequence a , b , c , d , e , f , g , dp int segPins[] = { 4, 5, A4, A3, A2, 3 ,2, A5 }; #define readPin 7 // declare pin 7 as readPin for input int num = 0; // declare num variable as counter (1 to 6) // declare number array and set the code to be displayed int number[11][8] = { { 1, 1, 1, 1, 1, 1, 0, 0 }, // 0 { 0, 1, 1, 0, 0, 0, 0, 0 }, // 1 { 1, 1, 0, 1, 1, 0, 1, 0 }, // 2 { 1, 1, 1, 1, 0, 0, 1, 0 }, // 3 { 0, 1, 1, 0, 0, 1, 1, 0 }, // 4 { 1, 0, 1, 1, 0, 1, 1, 0 }, // 5 { 1, 0, 1, 1, 1, 1, 1, 0 }, // 6 { 1, 1, 1, 0, 0, 0, 0, 0 }, // 7 { 1, 1, 1, 1, 1, 1, 1, 0 }, // 8 { 1, 1, 1, 1, 0, 1, 1, 0 }, // 9 { 0, 0, 0, 0, 0, 0, 0, 1 } // dot }; //========================== void setup() { // select and set all the pins as output for (int i = 0; i < 8; i++) { pinMode(segPins[i], OUTPUT); } pinMode(readPin, INPUT); // set the readPin i.e., 7 as input to read the digital value digitalWrite(readPin, HIGH); // internal pull-up resistor activated show ( 0 ); // intially show zero } //========================== void show ( int n ) // function to show the required number / dp { for (int i = 0; i < 8; i++) // show the code for each pin (total 8 pins connected) { // output the code to specific pin ( 1 to 8 ) as per the number (CC) digitalWrite(segPins[i], number[n][i]); // use following statement in case of Common Anode Display /* digitalWrite(segPins[i], 1-number[n][i]); // invert for CA */ } } //========================== void loop() { // check and run the code when button is PRESSED if ( digitalRead ( readPin ) == LOW ) { show ( 10 ); // show DOT only when button is PRESSED num++; // increment number when button is PRESSED if (num>6) // reset to 1 if exceeds 6 num = 1; } else // run this code when button is NOT PRESSED show( num ); // show the current number when button is NOT PRESSED } //========================== Good Luck to you.

  • AVR_ADC_DAC | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    AVR - ADC (Analog to Digital Conversion) Almost all AVR microcontrollers have ADC (Analog to Digital Conversion) pins, except a few, like ATTINY 2313 / 4313 series. ​ The main concept of ADC is, a stair-case type reference voltage is generated in steps by the micro-controller internally and compares the signal / input voltage with the generated reference voltage. If the input voltage just crosses the reference voltage, then the step number in the stair case is noted and saved in to its related Register (ADC). Our code reads the related Register and process as per our requirement. RESOLUTION AND REFERENCE VOLTAGE SELECTION: So, the Resolution of the ADC value depends on the number of steps generated for a range of voltage. Normally AVR micro-controllers generate 1024 steps (equals to 2 to power 10) from 0VDC to Reference Voltage. So, the data shows 10 bit resolution of ADC. The Reference voltage may be Vcc voltage or separate external input voltage other than Vcc or fixed internal reference voltage ( like 1.2V). The reference voltage input may be selected by setting the required bits in the related register (ADMUX ). ​ Bits 7 & 6 of ADMUX are named as REFS1 and REFS0. The bit setting serves for various input reference voltages in combination as shown below: REFS1=0 and REFS0=0 means external voltage as reference at VREF pin. REFS1=0 and REFS0=1 means Vcc as reference and connect 0.1uF capacitor at VREF pin to ground. REFS1=1 and REFS0=1 means Internal Reference Voltage used as reference and connect 0.1uF capacitor at VREF pin to ground. ​ eg: assume that the reference voltage is set to 5VDC. then, the ADC divides it into 1024 steps, ranging from 0000 to 1023 digital values. so, each step of 5V (= 5000 milli Volts) is equals to 5000/1024 = 4.8828125 mV which is approximately equal to 5 milli Volts. ​ So, the ADC of AVR in this case cannot measure less than 5 milli Volt fraction, in other wards, the ADC value of output available in the Register is approximately multiples of 5 milli Volts (= 0.005V) ​ So, you should not expect less than 5 milli volt accuracy with the above settings. ​ Similarly, you may calculate and set your input. ​ Note/Tip: if the external input reference voltage is 1.023V, then the ADC value is directly proportional to the milli Volt signal input. (1 ADC unit = 1 milli Volt) ACCURACY AND speed of ADC conversion: ​ The accuracy of ADC for AVR micro-controller also depends on the speed of the comparision with the step voltage. You have to sacrifice accuracy to a little extent to get the conversion quickly. The accuracy is sufficiently good upto 200 KHz clock set for the ADC comparision. If the ADC clock speed is set more than 200 KHz, then little accuracy will lost in ADC conversion. The frequency dividing w.r.t. CPU clock is called Prescaling for ADC conversion. ​ ​ In case 8 bit conversion (0 to 255 steps) is suffiecient for your requirement, then the conversion may cross 200 KHz. ​ The clock speed may be set by division factor w.r.t. to the main CPU clock in related Register (ADCSRA ), i.e., ADC Control and Status Register A. The LSB bits of ADCSRA are names as ADPS2, ADPS1, ADPS0 and 16, 4 and 2 are the division factors respectively. The minimum division factor is 2 by default if all set to zeroes. ​ eg: if ADPS2=1, ADPS1=0 and ADPS0=1 means division factor is 16X2 = 32. ​ SELECTING CHANNEL FOR ADC conversion: ​ Normally, AVR micro-controllers have more than one ADC channel connected to specific pins to the exeternal world for ADC conversion. Internally the ADC processor is same for all the channels, but a multiplexer is used to select a specific pin at a time for conversion. ​ The multiplexing Register (ADMUX ) is not only used for selection of the particular pin, but also to select differential input and gain in some micro-controllers by setting the specific bits. The LSBs of the ADMUX are used for directly selecting the channel (or pin). ​ eg: set 000 to LSB of ADMUX , to select channel 0(pin ADC0), 001 for channel 1 (pin ADC1) etc. ​ READING ADC VALUE FROM THE REGISTER: ​ The ADC value, which is the digital value for the analog voltage / signal input is stored in Register (ADC ). The result is also available in ADCL and ADCH registers, which contains 8 LSB of the ADC and 8 MSB of ADC. The value may be directly read from the ADC register by setting / returning to the variable name set by you in the program. ​ eg: a = ADC; or return ADC; b = ADCL; or return ADCL; c = ADCH; or return ADCH; ​ The result of conversion of ADC is 10 bit resolution. i.e. it varies from 0000 to 1023. ​ If required, the result may be converted to 8 bit resolution by setting bit 5 (ADLAR) in ADMUX register as shown below: starting the adc: Before starting the Analog to Digital Conversion, the settings as required, as explained above to be processed in the program. Also, bit 7 ,ADEN (ADC enable), should be set (to 1). ​ Now, to start ADC for every loop, the bit 6 ,ADSC (ADC Start conversion) of ADCSRA should be set to 1. Then the Analog to Digital Conversion will start. On completion of conversion bit 4, ADIF (ADC Interrupt Flag) of ADCSRA will be set (to 1) and also ADSC will be reset (to 0). So, by checking the status of ADIF or ADSC bit in ADCSRA register, you will come to know the conversion status. Example code for adc: ​ //================================================= void initializeAdc ( ) { //SET DIVISION FACTOR FOR F_CPU TO GET ADC CLOCK FREQUENCY ADCSRA |= ( (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) ); //ADPS2 divides by 16. ADPS1 divides by 4. ADPS0 divides by 2. ​ //SET REFERENCE VOLTAGE SOURCE ADCSRA |= ( (1 << REFS1) | (1 << REFS0) ); //set REFS1,REFS0 = 0,0 for Reference at AREF pin. // 0,1 for AVCC as Reference. 1,1 for Internal Reference Voltage. ​ // ENABLE ADC ADCSRA |= (1 << ADEN); } //================================================= int readAdc ( int channelno ) { // reset previous channel number upto 8 channels, if any. ADMUX &= (0b11111000); ​ // select current Channel number to read ADC. ADMUX = (channelno); // START conversion now. ADCSRA |= (1 << ADSC); // WAIT till conversion is complete while (ADCSRA & (1 << ADSC)); ​ // READ ADC value and return the function. return ADC; } //================================================= Note: ​ This is a general concept of ADC conversion in AVR micro controllers. Some small changes may be observed for each AVR micro-controller, which may be obtained from the data sheet specific to the micro-controller. ​ eg:for ATTINY micro-controllers have only 4 ADC channels and only two options for reference voltage selection. REFS0=0 means Vcc or REFS0=1 means 1.1V internal Voltage Reference. REFS1 is not available. ​ More controls are available while reading ADC of AVR micro controller. The specific ADC usage is available in programs and explained the specific concept and usage in the project. ​ << AVR: TYPES

  • AVR_GPIO | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    AVR - GPIO (General Purpose Input Output): All AVR micro-controllers have GPIO pins, which are used for reading or writing digital data (either logic 0 or 1). You, as a programmer use the GPIO control so frequently in your programs and knowledge about GPIO is very essential before programming any micro-controller, including AVR. GPIO for AVR is controlled using respective Registers. Almost all AVR external pins may be configured as GPIO pins, except power supply pins etc. AVR identifies GPIO registers using Alphabet, like A, B, C, D . . . ​ A set of GPIO pins are grouped together (nomally 8 or less, depends on availability of external pins) and named as PORT_A, PORT_B, PORT_C, PORT_D . . . and so on. The GPIO oridentation, the number of ports and number of bits (maximum 8) depends on the micro-controller pins availability and its pinouts. So, each port and each bit in a port, may be configured as either Input or Output. If, a port or a bit of a port is configured as input , then it can read the logical value ( either 0 or 1) from the external pins. Similarly, if a port or a bit of a port is configured as output , then it can be (used to send ) set to the required logical value (either 0 or 1). ​ Before using the registers the following statement should be used in the program: #include // input and output header file The above statment should be declared globally in the program before using the registers and main function / method to identify the registers with its names explained below. ​ Many concepts are used to program GPIO. The programming / code explained below is directly accessing the register(s) and simple to understand. Programming AVR GPIO for DIRECTION: While programming GPIO, initially set the direction of data transifer (read / write) by setting the DDR (Data Direction Register) of the particular port, like DDRA for PORT_A, DDRB for PORT_B, DDRC for PORT_C. . . so on. You have to check the existance of particular port and number of pins for the selected micro-controller, before programming. ​ eg 1: write the following code (any one line) in your program to set 3 MSB (Most Significant Bits) as OUTPUT and 5 LSB (Least Significant Bits) as INPUT for PORT_A. So, note that, setting a bit to 1 for OUTPUT and 0 for INPUT. DDRA = 0b11100000; // Binary format for easy under standing DDRA = 0xE0; // Hexadecimal format ​ eg 2: write the following code (any one line) in your program to set 6 MSB as OUTPUT and 2 LSB as INPUT for PORT_B. DDRB = 0b11111100; // Binary format DDRB = 0xFC; // Hexadecimal format Programming AVR GPIO for OUTPUT: Once the direction of data transfer is set as OUTPUT in DDR register of the particular port and its bits (by setting 1s), then the actal data may be WRITTEN to its Ouput Register PORTA or PORTB or PORTC or PORTD . . . so on. The value to the register may be written as a whole ( all 8 bits, i.e., a byte ), or each required bit of the register. The value in the register reflects at the external pins of the micro controller connected to the register, named as port and its bits. ​ eg 1: write the following code (any one line) in your program to set all 8 bits (byte) in a single command / statement. So, note that, setting a bit to 1 in a port register is for logic HIGH and 0 for logic LOW, which reflects the value as output at the connected external pin of the micro-controller. PORTA = 0b11001000; // Binary format for easy under standing PORTA = 0xC8; // Hexadecimal format ​ eg 2: write the following code (any one line) in your program to set logic 1 for only one bit (PB2 or bit 2) of register PORT_B, without disturbing other bits in the PORT_B. Here, logic OR symbol ( | ), is used for the purpose. The | symbol is also called pipe key on keyboard and commonly used for logic OR in C, C++, Java etc. PORTB |= 0b00000100; // Binary format PORTB |= 0x04; // Hexadecimal format ​ eg 3: write the following code (any one line) in your program to set logic 1 for ANY bits (PC1, PC4 & PB7) of register PORT_C, without disturbing other bits in the PORT_C. Here also, logic OR symbol ( | ), is used, wherever the value of bits to be changed to 1. PORTC |= 0b10010010; // Binary format PORTC |= 0x92; // Hexadecimal format ​ eg 4: write the following code (any one line) in your program to set logic 0 for only one bit (PB4 or bit 4) of register PORT_B, without disturbing other bits in the PORT_B. Here, logic AND symbol ( & ), is used for the purpose. The & symbol is also called ampersand key on keyboard and commonly used for logic AND in C, C++, Java etc. PORTB &= 0b11101111; // Binary format PORTB |= 0xEF; // Hexadecimal format ​ eg 5: write the following code (any one line) in your program to set logic 0 for ANY bits (PD2, PD3 & PD6) of register PORT_D, without disturbing other bits in the PORT_D. PORTD |= 0b01001100; // Binary format PORTD |= 0x4C; // Hexadecimal format Programming AVR GPIO for INPUT: Once the direction of data transfer is set as INPUT in DDR register of the particular port and its bits (by setting 0s), then the actal (logical) data may be READ from external pins of the port, which is always available at its Input Register PINA or PINB or PINC or PIND . . . so on. The value of the register may be read as a whole ( all 8 bits, i.e., a byte ), or each required bit of the register. The value in the register reflects the logical value available at the external pins of the micro controller connected to the register. ​ eg 1: write the following code (any one line) in your program to read all 8 bits (byte) in a single command / statement. Here, always read FULL register (8 bits) named as PIN, then analyse the required bit(s) using logical operators. So, the external logical values are always available at registers PINA, PINB, PINC, PIND . . . etc., and you are reading the values, whenever is required while programming. unsigned char val = PINA; // unsigned char can hold 8 bits and PINA size is also 8 bits. int val = PINA; // int can hold 16 bits and PINA size is 8 bits ​ eg 2: write the following code (any one line) in your program to read only one bit (PB3 or bit 3) of register PORT_B and save it to a variable, for further processing. Here, logic AND symbol ( & ), is used for the purpose. unsigned char val = (PINB & 0b00001000); // Binary format unsigned char val = (PINB & 0x08); // Hexadecimal format ​ ​ eg 3: write the following code (any one line) in your program to read multiple bits (PC6 and PC1) of register PORT_C and save it to a variable, for further processing. Here also, logic AND symbol ( & ), is used for the required bits to read.. unsigned char val = (PINC & 0b01000010); // Binary format unsigned char val = (PINC & 0x42); // Hexadecimal format ​ Full Programming Code for AVR GPIO: Note the steps to start programming to access and control GPIO pins of AVR micro-controller as 1) include input and output header file and other required header files. 2) define by name for any required pins 3) start main function / method 4) set Data Direction Registers as per your requirement. 5) use Port registers. i.e., Read from Registers or Write to Registers 6) you may use loop to make it continuous operation(s). ​ eg 1: writing the code for sending high output to all bits of PORT_B and 3 LSB of PORT_A (you may change to any PORT, which is avialable for the selected AVR micro-controller) #include // input and output header file int main ( ) // start main function / method { DDRB = 0xFF; // set (all bits) Data Direction of PORT_B as output DDRA = 0b00000111; // set (3 LSB) Data Direction of PORT_A as output, remaining as input PORTB = 0xFF; // set (all bits) Data in PORT_B as high PORTA = 0x07; // set (3 LSB) Data in PORT_A as high return 0; // exit from main } Once you compile and write the HEX code to AVR micro-controller, then the external pins of PB0 to PB7 and PA0 to PA2, were set to logic high (about Vcc voltage of micro-controller), which may be tested with an LED (with series resistance) or using mult-meter in voltage mode. ​ eg 2: writing the code for reading input value from one port (POPRT_A) and send the same to another port (PORT_B) (you may change to any PORT, which is avialable for the selected AVR micro-controller) #include // input and output header file int main ( ) // start main function / method { unsigned char val; // use a variable to store the value read from the input port DDRB = 0xFF; // set (all bits) Data Direction of PORT_B as output DDRA = 0b00000000; // set (all bits) Data Direction of PORT_A as input while ( 1 ) // start a never ending loop { val = PINA; // read (all bits) from PORT_A save it to variable val PORTB = val; // write (all bits) value avialble in val to PORT_B } // end of while(1) loop and control goes to first statement in while loop. return 0; // exit from main } Once you compile and write the HEX code to AVR micro-controller, then the external pins of PB0 to PB7 are replica of PA0 to PA7, in logic levels. i.e., if any pin of PORT_A is set to high (connect to Vcc through a series resistance), like PA3, then same pin of PORT_B (PB3) also set to logic high (about Vcc voltage of micro-controller), which may be tested with an LED (with series resistance) or using mult-meter in voltage mode. SUMMARY: The main registers used to control, read and write the GPIO of the AVR micro controller are: DDRX, PINX, PORTX, where X is the port name A / B / C / D ... ​ for more details, refer at following link (courtesy:microchip): http://ww1.microchip.com/downloads/en/Appnotes/90003229A.pdf

  • mcuTimersCounters | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    microcontroller - COUNTERS & TIMERS Almost all micro controllers have one or more inbuilt counters. They are either 8 bit or 16 bit counters. The input for the counters may be from either external pins or internal system clock through pre-scalars. The current counter value is always saved to a register in the micro-controller and may directly access the value at any time. ​ Delay is one of the commonly used timer function, which is single use timer and not so accurate when compared to the internal programmed timers. Delay works on counting the ticks of micro-controller system clock frequency. If the counter is programmed for external input , then the counter register may either increase or decrease by 1, by changing the logic (voltage) at the programmed pin of micro controller. You can access the value in the register and may show on a displaying device. This type of programming is useful for event counter project etc. In case, the counter is read for every one second and reset to zero after reading the current value, then the programming may be used as frequency counter project. For programming as internal counter , the counter should receive signal from system clock frequency through pre-scalar circuit. The pre-scalar divides the system clock frequency by the value set in the pre-scalar register. In case, the counter register is full (i.e., exceeds its capacity), then it resets and starts counting again. An Interrupt may be activated when the particular counter register is full and resets, which calls COUNTER OVERFLOW SERVICE ROUTINE . Every time the counter is full or overflown, the Interrupt service routine is called, where you may program do specific tasks. This Interrupt is very useful to get exact and fixed time period, to do some specific operations in loop at a particular time period. These are called Timers in micro-controllers. ​ These timers are used for data aquisition system, frequency generation, wave form generation etc.

  • Packages | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    PACKAGES All Integrated Circuits (ICs) and Transistors are available in one or more standard packages. Type of packages: DIP / DIL = Dual In Line Package. Mostly through hole type package used to put on one side of the PCB ( Printed Circuit Board ) and solder on the opposite side of the PCB. ​ SIL / SIP = Single In Line Package. ​ PDIP = Plastic Dual In Line Package. ​ CDIP = Ceramic Dual In Line Package. ​ SO / SOIC / SOP : Small-Outline Integrated Circuit ​ QIP / QIL = Quadraple In Line Package. ​ SMT = Surface Mount Technology package. The IC is placed on the PCB and solder on the track of same side of the PCB. SOJ , SOIC , LQFP, QFN, MLF, QFP, MQFP are some variants of SMT ICs, having various types pin outs. LQFP = Low-profile Quad Flat-Package is SMT IC. TQFP = Thin Quad Flat-Package (having leads) is SMT IC. TQFN = Thin Quad Flat No-lead PLCC = Plastic Leaded Chip-Carrier CLCC = Ceramic Leadless Chip-Carrier QFN/MLF = Quad flat No-leads package. Also called as Micro Lead Frame. QFP = Quad Flat Package. MQFP = Metric Quad Flat Package. TO-220 = Through-hole plastic package with three leads attached with a metal heat sink tab with hole on top. SOT-23 / SOT-223 = Small Outline Transistor Package is SMT IC, having three pin-outs.

  • Arduino | Adruino | Uno | Mega | Nano | Arduino Projects | Simple MECHATRONICSsimple mechatronics

    Arduino TYPES Arduino IDE Arduino BLINK LED Arduino DIGITAL I/O Arduino 7SEGMENT Arduino LCD control Arduino KEYPAD Arduino ADC Arduino PROJECTS Sanitizer - Full Automatic Super Power LiFePO4 Cell Charger Satellite Clock Arise, Awake and Stop NOT until the goal is reached – Swamy Vivekananda Arduino Nokia Display N

  • Arduino_LCD | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    Introduction to LCD (text mode) : The LCDs (Liquid Crystal Displays) are highly used in Arduino projects for displaying required text message and status of the process. The price of the LCD is affordable and easily available. The standard LCDs can display text message with various colour LED background s. The commonly available text sizes of LCDs are: 1) 1 row x 16 columns 2) 2 rows x 16 columns 3) 4 rows x 16 columns 4) 4 rows x 20 columns with green or blue or while background colour light. ​ The 2 rows x 16 columns with green background is highly used in the Arduino projects. For all the above listed LCD s, 16 pins are available to control and display the text. The pins 1 and 2, are for power supply of the circuit and 15 and 16 are background LED power supply. The pin 3 is for adjusting contrast of the display and pins 4, 5 and 6 are control pins i.e., RS (Register Select), RW (Read/Write) and Enable. The remaining 8 pins (byte) from 7 to 14 are data pins. (Refer pinouts of LCD below.) The LCD have two options for its data bits operation. i.e., 8 bit mode and 4 bit mode. While in 8 bit mode, the data byte is read/write in one enable clock pulse, whereas in 4 bit mode, the data byte is divided in to two parts and read/write using two clock pulses of enable pin. So, in 4 bit mode, you may save number of pins connected to Arduino board, at an expense of a very little delay (double time) for data transfer. ​ Normally, you may be writing the data to LCD. So, the R/W pin (no 5) is connected to ground (logic low). In case you want to read any register of LCD, the R/W pin should be set to logic high (hardly reads the LCD register). Interfacing LCD to Arduino : Any LCD (which are listed above), may be connected to any Arduino board with minimum 6 digital pins. i.e., 4 data pins and 2 control pins, RS & enable. A simple generalized circuit diagram for interfacing LCD with Arduino Uno board is shown below. You may observe that the pin 5 (R/W) is connected to ground and pin 3 (VEE) is connected to a trimpot's mid pin. The trimpot's end pins are connected to Vcc and ground. The trimpot should be adjusted for good contrast of display after displaying any text on LCD. Now, open Arduino IDE, then select from menu, File->Examples->Liquid Crystal->Hello World. Then, declare the pin connections of LCD to Arduino board, as shown below. // include the library code #include ​ // Pin connection sequence RS, Enble, D4, D5, D6, D7 LiquidCrystal my_lcd { 2, 3, 4, 5, 6, 7 }; ​ Initially, set LCD's number of columns and rows in begin function. Then, enter the required text to be displayed using print function. The setCursor function locates the cursor on the LCD to display the next text using print function. ​ void setup() { // set up the LCD's number of columns and rows: my_lcd.begin(16, 2); // std : 16,2 or 16,4 or 20,4 // Print a message to the LCD. my_lcd.print("Simple"); my_lcd.setCursor (4, 1); my_lcd.print ("MECHATRONICS"); } ​ Then, continue with the existing loop function, to display counting seconds at 1st col 2nd row ​ void loop() // for displaying number of seconds lapsed { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): my_lcd.setCursor(0, 1); // print the number of seconds since reset: my_lcd.print(millis() / 1000); } ​ The, output of the sketch is shown here. You may adjust the trimpot for good contrast. ​ For 16 coloumns x 1 row also, it should be declared as my_lcd.begin(16, 2); only. The first row text will be displayed on left 8 columns and second row text will be displayed on right side 8 columns (starting from 9th column) ​ Similarly, declartion for 16 columns x 4 rows, my_lcd.begin(16, 4); and declartion for 20 columns x 4 rows, my_lcd.begin(20, 4); Simple LCD control functions: View VIDEO Some simple functions, which are useful and frequently used are explained below: // for clearing all the contents (text) on the LCD my_lcd.clear(); ​ // To display and hide the text displayed on the LCD my_lcd.display(); // To SHOW contents of LCD my_lcd.noDisplay(); / / To HIDE contents of LCD ​ // for positioning the cursor at required position (x, y) // (x,y) numbers starts from (0,0) // so 0 means 1st, 1 means 2nd. . . and so on. my_lcd.setCursor(5, 1); / / Position at 2nd row, 6th column ​ // To display or hide a rectangular blinking cursor // positioned by setCursor function. my_lcd.blink(); // To SHOW rectangular blinking cursor my_lcd.noBlink(); // To HIDE rectangular blinking cursor ​ // To display or hide an underline type cursor // positioned by setCursor function. my_lcd.cursor(); // To SHOW underline type cursor my_lcd.noCursor(); // To HIDE underline type cursor ​ LCD text scroll functions: View VIDEO Some simple scroll functions, which are used to scroll displayed text on LCD to either side are explained below: ​ // To scroll the displayed text to RIGHTside by one step my_lcd.scrollDisplayRight(); ​ // To scroll the displayed text to LEFT side by one step my_lcd.scrollDisplayLeft(); ​ // for automatically scroll a long text to left // for reading long text sentences. my_lcd.autoscroll(); / / scrolls the text to left by one step ​ Before using the scrolling functions, the required text should be displayed using my_lcd.print function. Then, f or continuous scrolling effect of the text sentence (text string), the above functions should be used in a for loop function.

  • mcuInterrups | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    microcontroller - INTERRUPTS Interrupt means, stopping the main program loop or operations due to allowed / programmed change, then go for some operations to perform, again continue the main program. Interrupt is one of the most required feature in micro-controllers. INTERRUPT - WORKING CONCEPT: ​ Normally micro-controller programs are written to do sequential operations, out of which, most of the operations are framed in a loop to do specific operations. The output depends on the input of signals from sensors or other electronics equipements. ​ But, some operations depend on a particular signal, which is indepenent of the time or position of normal input or output operations. In this situation an INTERRUPT is generated and corresponding bit in the particular Registar is set (to high or low), which is called interrupt flag is set. ​ Once Interrupt Flag is set, the Processor inside the micro-controller saves the current status of operations to separate registers (called as Push to Stack) and calls (or loads) the particular function related to the interrupt is caused, which is called INTERRUPT SERVICE ROUTINE. ​ Now, the processor works on the operations / instructions written in the function, then returns to the main program on completion and resets the interrupt flag (=off). Now, the processor calls back the previous values saved to the specific registers (called as Pop from Stack) and continues its normal operations. CAUSE OF INTERRUPT: ​ Interrupt may be caused by so many reasons, which are set by the programmer (by you) as required. Interrupts are of two types. 1) External 2)Internal. External Interrupt: ​ Some of the pins of micro-controller serve the purpose of Interrupt Input pins. By setting the related bits in the Interrupt register, the relative interrupt is activated. The interrupt may be caused by changing the voltage level to high or low or toggle at the external pins, as per the bits set in the interrupt register by the programmer. Internal Interrupt: ​ Internal Interrupt may be caused by the reasons set by the programmer. Most of the internal interrupts caused by Timers , Counters , ADC , Communications etc., which are set by the programmer as per requirement. NOTE: ​ This is the general concept about Interrupts. The allowed functions and names of routines of Interrupt service routine depends on the specific micro-controller. ALL THE BEST TO YOU.

  • SanitizingMachine | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    Sanitizing Machine (Simple & Economical) In the Coronavirus pandemic time, an automatic, touch free, simple and economical Sanitzing Machine is need of the hour. Simple and easily available items are used for the project. The size of the container may be selected as per individual's requirement. The circuit board is fitted on the top of the container's lid in horizontal position, but, may change to vertical position as required. The project uses two main components, i.e., IR sensor module and mini dc motor pump. ​ When the IR sensor module finds any obstacle (assume your hand), a signal is generated from the OUT pin, which is indicated by an LED on the module. The Signal is amplified using a transistor 2N2222 and drives the motor pump to eject the liquid sanitizer from its outlet. A suitable jet or splash arrangement may be made at the tip of outlet for effective cleaning of your hands. ​ The simple full circuit diagram is available here: INITIAL SETUP & USAGE: ​ Initially, functioning of the IR sensor using 5VDC and motor pump using 9VDC. The 5V regulated DC voltage may be obtained using IC 7805 as shown in the circuit. The trimpot on the IR module may be adjusted, such that, the sensor shown maximum sensitivity towards object in front of it. Then assemble the components as per the circuit diagram on a small PCB. Now, test that the motor is responding properly to the obstruction (your hand). ​ Select a suitable container and required direction (horizontal / vertical) to fit the circuit board. Now, pour the liquid sanitizer in to the container and adjust the nozzle as per requirement. Your Sanitizing Machine is ready to use. Selection of container and arrangement of nozzle depends on your creativity and amount you want to spend on the project. ​ The power supply for the circuit may be connected to 9V battery or 7.4V Li-Ion battery or LiPo battery or 9V DC adapter from mains. ​ Be Safe & ENJOY.

  • mcuProgramFlow | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    microcontroller - PROGRAM FLOW Before programming a micro-controller, one should have basic concept on flow-chart and algorithm. ​ While programming a micro-controller, the following order of sequence is followed in general. A small deviation may be observed, which depends on the habit of the person, who is programming and micro-controller programming IDE (integrated Development Environment) software, which is used for programming and compiling. Some IDEs support the option to directly upload/write the binary code to MCU. ​ comments/remarks and GNU about the program Declaration of frequency of the MCU clock. Sometimes optional, which may be declared in the settings/preferences. include or import the libraries required. Some libraries may be included by default, which depends on the IDE. Declare the global variables and initial values as required for the program Declare the classes or structures, if any ( normally used for OOPS programming ). Declare and write required functions / methods required. Declare ONCE, the ports as input/Output/Analog/Communication and their direction of data flow, like from MCU (Output) , to MCU (Input) etc. The syntax may vary for each type of MCU. Some Variables and Functions, which are defined earlier, may be called for initial setup. Write a continuous loop for actual requirement, which may have functions are called or input/Output/Analog/Communication ports are used to communicate with the external world of the MCU. |This is the main loop, which uses the required logic and sequence of operations to obtain required results. ​ The actual syntax and coding is depending on the programming language, but the logic is same for all the programming languages.

  • StartUp | SimpleMechatronics| Simple MECHATRONICSsimple mechatronics

    Hello Friend(s), ​ So, you want to learn electronics and make your own projects. Then HEARTY WELCOME to Simple Mechatronics . ​ Click the buttons, to have basic idea about the TOOLS and INSTRUMENTS required for Make-at-Home projects. ESSENTIALS INSTRUMENTS For Start learning, practicing and doing Make-at-Home Projects, please remember the following points: ​ 1) Don't be in hurry to learn and master instantly. But learn step-by-step. ​ 2) If, you can't understand any topic, just wait and try to understand, then skip for next topic. Still you have problem, you may contact at https://www.simplemechatronics.com/contact-form ​ 3) Use proper tools and instruments for Make-at-Home projects. ​ 4) Inserting or Removing a component of a power connected circuit may damage component(s). Better to remove power supply, while changing components. ​ 5) Be cautious and take safety measures, while working with high voltages and circuits connected to mains voltages. ​ BASIC TOOLS POWER TOOLS POWER SUPPLIES ROME was not built in a DAY. - (a French proverb)

bottom of page