The Kynix Blog
Stay Ahead with Expert Electronics Insights,
Industry Trends, and Innovative Tips
- Electronic Components
- News Room
- General electronic semiconductor
- Components Guide
- Sort by
- Robots
- Transmitters
- Capacitors
- IC Chips
- PCBs
- Connectors
- Amplifiers
- Memory
- LED
- Diodes
- Transistors
- Battery
- Oscillators
- Resistors
- Transceiver
- RFID
- FPGA
- Mosfets
- Sensor
- Motors, Solenoids, Driver Boards/Modules
- Relays
- Optoelectronics
- Power
- Transformer
- Fuse
- Thyristor
- potentiometer
- Development Boards
- RF/IF
- Semiconductor Information
- PCB
- transistor
Catalog Resistors and Capacitors Electrolytic capacitors Transistor & diode packages Integrated circuit SMD packages Ball Grid Array Small Outline Packages Flat Packages Surface Mount Technology (SMT) is a technique for mounting electrical components directly to the surface of a printed circuit board (PCB). The component that is mounted on the surface of the PCB using surface mount technology is called Surface Mount Device (SMD). SMT has essentially replaced the through-hole PCB manufacturing technology to reduce cost, and increase efficiency and productivity. Since the size of SMD components is very small, compared to through-hole components many more SMD components can be arranged in a given place. Selecting and knowing the right SMD component for your PCB is very necessary. SMD components have standard codes and sizes which as a PCB designer one should know. Kynix offers all sorts of SMD components for PCB manufacturing which can be found here. This article will help you choose the right SMD component from the Kynix library. SMD components come in various packages and sizes to facilitate the automated manufacturing of PCBs. Most of the SMD components are standardized to make manufacturing easy. The most commonly used SMD components are capacitors and resistors. The standards of these components are set by Joint Electron Device Engineering Council. There are different types of packages. When a new package is introduced in the industry, it is named after its initials such as Quad Flat Package (QFP). While some packages have no name, it creates confusion in the industry. Below we have discussed flat chip SMD resistors, capacitors, diodes & transistors, and IC packages. The size of the SMD chip for resistor, capacitors, and some of the diodes is given by a 4 digits code, which represents the dimension of the flat chip either in inches or in millimeters. In the US it is represented in inches while outside the US it is represented in mm. The first two digits represent the length (L) of the component while the last two digits represent the width (W) of the component. While the thickness is also an important factor in manufacturing, it is not mentioned in the 4 digits code, for this, the actual datasheet of the component provided by the manufacturer should be used. Many PCB components such as resistors, capacitors, diodes, FETs, and other transistors are available in SMD. SMD resistors and capacitors, also known as passive devices, come in different sizes. Depending upon the availability of space, soldering capability, and environment temperature, different packages can be used. The names of these packages given in the table below are derived from the size of the components in inches. Resistors and Capacitors Below are the most common size codes for capacitors and resistors. You can find these resistors and capacitors here. S. NoPackageDimensions (in)12010.02x0.01220160.2x0.1632020.02x0.0242040.02x0.0452070.02x0.0763030.03x0.0373060.03x0.0684020.04x0.0294040.04x0.04104060.04x0.02115020.05x0.05125050.05x0.08135080.05x0.1145100.05x0.1156030.06x0.03166060.06x0.06176120.06x12187050.07x0.05198050.08x0.05208080.08x0.08218150.08x0.15228160.08x0.16238300.08x0.32410100.1x0.12510200.1x0.22610500.1x0.52712060.12x0.062812100.12x0.12912160.12x0.163012180.12x0.183112200.12x0.23212240.12x0.243312250.12x0.253414050.14x0.053515050.15x0.053615060.15x0.063715100.15x0.13815750.15x0.753916080.16x0.084016320.16x0.324118120.18x0.124220100.2x0.14320120.2x0.124420180.2x0.184520300.2x0.34622080.22x0.084724090.24x0.094824120.24x0.124925100.25x0.15025120.25x0.125125150.25x0.155226150.26x0.155327250.27x0.255427260.27x0.265527280.27x0.285628160.28x0.165728170.28x0.175828180.28x0.185930140.3x0.146030200.3x0.2 At present most manufacturers can manufacture PCBs with SMD components up to 0603 easily, going below this size to such as 0402 or 0201 is still difficult for the manufacturers, and thus the cost of manufacturing increases if these components are included in the design. Therefore, most of the manufacturers recommend using 0603 components for PCB design. Electrolytic capacitors The electronic industry adopted EIA and IECQ standards for molded tantalum capacitors. These packages are named A, B, C, D, and E. These correspond to different sizes in millimeters. Package height is not included in the size code. EIA codeMetric codeDimensionA32163.2 x1.6 mmB35283.5 x 2.8 mmC60326.0 x 3.2 mmD73437.3 x 4.3 mm Several other electronic devices can not follow any standard because of their unique nature. SMD components like an inductor, transformers, crystals, resonators, and temperature-controlled oscillators require different packages often larger than the standard packages. It is very unlikely that these packages will be standardized because of their unique nature. However, the package must be chosen in a way to make pick and place possible. These capacitors can be found here. Transistor & diode packages SMD transistors and diodes have the same package type. Transistors have three pins while a diode has two pins. The third pin is added to the diode package to keep the orientation right. Diodes are packages that come in different varieties. Some packages follow the standards of capacitors and resistors that we discussed above. Some of the most common diode and transistor packages are SOT-23 - Small Outline Transistor: It is the most common diode and transistor package. It has three pins and measures 3 mm x 1.75 mm x 1.3 mm. It is used for low-power applications.SOT-223 - Small Outline Transistor: This diode package is used for high-power applications. It is bigger than SOT-23. It measures 6.7 mm x 3.7 mm x1.8 mm. It has four pins with which the fourth one is used for heat dissipation. Integrated circuit SMD packages IC packages are found in many packages and can be classified in many different ways. It is very common to hear the terms DIP, SOP, SIP, TSOP, QSOP, MSOP, SOIC, QFP etc. These are the different packages of IC. They can be categorized as. There are three main package types for surface mount integrated circuits: Ball grid array (BGA)Small outline package (SOP)Quad flat pack (QFP) Ball Grid Array Ball Grid Array package has solder balls attached to the underside of the package. Beneath the balls are electrical traces of IC. Ball Grid Array has further the following types. Molded Array Process Ball Grid Array (MAPBGA)Plastic Ball Grid Array (PBGA) Thermally Enhanced Plastic Ball Grid Array (TEPBGA)Tape Ball Grid Array (TBGA)Package on Package MicroBGA. Small Outline Packages Small Outline Package is another IC package in which pins come out from the sides of the IC. The convention used for SOIC or SO package is the name followed by the number of pins used in the package. i.e SO-12 means the IC has 12 pins. Further types of SOP/SOIC are SOJ - Small Out-Line J-Leaded PackageTSOP -Thin Small Outline PackageVSOP -Very Small Outline Package).TSSOP -Thin Shrink Small Outline Package SSOP -Shrink Small Outline PackageQSOP -Quarter-size Small Outline Package Flat Packages Flat IC package have pins arranged on its side in L or J shape. These pins are arranged on the side of the package with the leads coming out. This package further has many subtypes. QFP (Quad Flat Package)TQFP (Thin Quad Flat Package)STQFP (Small Thin Quad Plastic Flat Package)FQFP (Fine-pitch Quad Flat Package),(Low profile Quad Flat Package)VQFP (Very-small Quad Flat Package)ETQFP (Exposed thin quad Flat Package)PQFN (Power Quad Flat Package)PQFP (Plastic Quad Flat Package)QFJ (Quad Flat J-Leaded Package)QFN (Quad Flat Non-Leaded Package)
Allen On 2022-11-29
Overview of switchesMain parameter of switchesSwitch SymbolSwitch DiagramWhat is a toggle switch?How does toggle switches work?Video related to toggle switchesFive ways toggle switch wiringCircuit diagram of toggle switchTypes of Toggle SwitchDPDT Toggle SwitchDPST Toggle SwitchSPDT Toggle SwitchSPST Toggle SwitchLED Toggle SwitchAdvantages and Disadvantages of toggle switchesRocker Switches vs Toggle SwitchesToggle Switch ApplicationsToggle Switch FAQ Overview of switchesA switch is a piece of equipment used to stop current flow in a circuit. Simply put, a switch has the power to complete or disrupt an electrical circuit. To turn a device ON and OFF, every electrical and electronic application makes use of at least one switch.Switches are therefore a component of the control system, and control action is impossible without them. A switch has two possible states: completely ON (by shutting its contacts) and completely OFF (by opening its contacts).A switch establishes a closed conduit for the current to flow when its contacts are closed, which causes the load to draw power from the source. As demonstrated in the figure below, no power is used by the load when a switch's contacts are open. Main parameter of switchesSwitches ParametersVoltage ratingThe insulation materials, contact separation, rate of separation, and general safety considerations are some of the variables that affect voltage rating.Current carrying ratingThe electronics switches' current specifications are crucial since they will only be able to transport a particular amount of current through their contacts.Current switching ratingAn electromechanical switch's current switching rating is typically lower than its current carrying rating. The problem is that forming and breaking contacts creates arcing, and the contacts can only handle so much of it before the wear on the contacts significantly limits the operational life.Switch formatFor every electronics circuit design, choosing the appropriate switch format is essential. Toggle switches, slider switches, rotary switches, DIPs, thumbwheels, and many more are among the many various switch formats. Any switch's selection process includes the format.Number of operationsThere is always a tiny degree of wear as the contacts of a switch move across one another to ensure the best resistance is obtained.Contact resistanceThere is a higher contact resistance than there would be if the conductor were continuous because a switch's contacts are not a continuous conductor but can be broken and rebuilt.Power ratingThe maximum power that the switch can manage while working is indicated by its power rating. Overriding this rating may result in an excessive buildup of heat inside the device, which could lead to the switch failing and create a safety issue.Inductive ratingIf not handled properly, any inductance in a circuit will raise the intensity of arcing at the contacts and shorten the life of the switch. This means that once current is flowing, it is challenging to stop since the inductance will produce significant back EMFs when the current is stopped. This is because any inductance in the circuit will oppose the change taking place.Contact typeThere are two different types of contacts used in change-over type electromechanical: Break before make and Make before break. Switch SymbolSwitch Symbol Switch DiagramSwitch Diagram What is a toggle switch?Toggle switches are electrical switches that be opened or closed using a lever or handle that is moved forward and backward. Toggle power switches and joystick switches are other names for these switches. These switches can be used in any electrical application because they are flexible devices.Since switches are typically manipulated manually, the toggle switch, which functions as a straightforward ON/OFF switch in many electrical circuits, is essential. The toggle switch controls the flow of current from the power source to a device or within a device by use of a lever.Toggle Switch How does toggle switches work?The armature switches the contact into position to start or halt an electrical flow when the switch operator pulls the toggle (the actuator). To put it simply, pushing the toggle can turn a gadget on or off. There are two functions that can be used; a momentary function means that the switch is only engaged when the force is applied. An internal spring mechanism forces the armature to return to its initial position in order to accomplish this. A latching toggle switch, in contrast, keeps the state after being depressed until the toggle is depressed once again to release the switch.The basic toggle switch design is as follows, although being available in various forms and configurations. When the toggle is pulled, an armature (a component conducting electrical current) attached to it moves, adding or removing an electrical contact from a circuit and activating or deactivating the circuit.Even though momentary switches also have an associated spring that will draw the actuator back to its starting point if released, the switch will typically stay in place unless manually pushed again. Video related to toggle switchesVideo Description: Add a switch easily to any household electronics item or automotive project easily by watching this short video. Five ways toggle switch wiringStep One: Look at the instruction about the toggle switchesElectrical setups for the many types of equipment that you might wish to install toggle switches on vary substantially. As a result, no single guide is likely to offer a universally applicable solution. The procedures in this section are intended to serve as general guidance for a straightforward on-off (also known as SPST) toggle switch. They should never take precedence over any installation instructions that came with your toggle switch or the appliance you're placing it into.You can look at the below instruction about the toggle switches:Toggle Switches Instruction Step Two: Your device's supply wire should be cutYou must link your toggle switch to the device's power source in order for it to act as an on-off switch. Cut the supply wire for your device with wire cutters at a spot that will make it easiest to route one or both ends of the cable to the switch. Using a wire stripper, remove about 12 inch (1.3 cm) of insulation from each end of the wire.Cut the supply wire Step Three: If the cable does not reach the switch from either end, add a pigtail.A pigtail is a brief wire piece with both ends stripped, often measuring under 6 inches (15 cm). It can be used as a form of "extender" by being attached to cables that aren't quite long enough to reach your toggle switch.Pigtail connetced to switches Step Four: Connect the supply wire to the toggle switchNow that the device's supply wire has been severed, you must insert your toggle switch so that it can control the circuit's current flow in the middle of the break. The kind of toggle switch you have will determine how you should proceed. Step Five: Test your switchWhen your toggle switch is properly wired, carefully reattach the power to the device and check the toggle switch's operation. You can swap out the panel or housing if everything functions as it should. Congratulations! A toggle switch has been fitted successfully. Circuit diagram of toggle switchThe SPDT toggle switch's circuit schematic is displayed below. A 6V battery, two resistors R1 & R3, two LEDs D1 & D3, and a 21236N switch can be used to construct this circuit.Three terminals, including one input and two outputs, make up this switch. Thus, we can acquire two outputs, the first of which comes from pins 2 (COM) and 1 and the second of which comes from pins COM and 3. In three-way circuits, this switch is mostly used to control electrical appliances from two places.The pins 1 and 3 are connected to D1 (an LED) and D2 (an LED), respectively, in the circuit shown above. When pin 1 is toggled, the D1 LED turns on and the D2 LED turns off. In a same manner, when pin 3 is toggled, D2 LED will turn ON and D1 LED will turn OFF. Consequently, we are able to manage two loads using a single SPDT toggle switch.Toggle Switch Circuit Diagram Types of Toggle SwitchThere are four main versions of these switch designs, each of which has a different combination of throws and poles, such as SPDT, SPST, DPDT, and DPST. Poles of these switches are often the distinct power supply controlled by each switch, whereas throws are the various areas the switch can be used, such as ON & OFF.DPDT Toggle SwitchDPST Toggle SwitchSPDT Toggle SwitchSPST Toggle SwitchLED Toggle Switch DPDT Toggle SwitchDouble pole, double throw (DPDT) toggle switches are used to establish or terminate connections between two conductors and two different circuits. There are six terminals on these switches, and terminals 3 and 4 get the necessary power to drive the loads on the other terminals, which are 1, 5, 2, and 6. Four-way or four-position switches are the names given to these switches.DPDT Switches The six terminals of a DPDT toggle switch are. The toggle switch is represented by terminals 3 and 4. The electricity required to drive the loads on terminals 1 and 5, as well as 2 and 6, is supplied to these terminals. Between terminals 1 and 5, terminal 3 can switch. Therefore, terminal 3, which represents the toggle switch, can switch between operating the fan and operating the motor if a fan is connected to terminal 1 and a motor is connected to terminal 5. The same is true for terminal 4, too. Between terminals 2 and 6, terminal 4 can switch. In this case, terminal 4, which acts as the toggle switch, can switch between the heater and the blower if a heater is connected to terminal 2 and a blower is connected to terminal 6.Two input switches on a DPDT switch can each be connected to one of two terminals. It can therefore use two switches to control four distinct circuits or devices.The circuit for a DPDT toggle switch is illustrated below:DPDT Toggle Switch Circuit Diagram DPST Toggle SwitchThe name "DPST" refers to a double-pile single throw switch, which is used to establish or terminate the connection between two circuit conductors inside a single branch circuit. These switches typically have four terminals that can be used to simultaneously connect and disconnect two pairs of terminals.DPST Switches A 30A DPST switch is used in this wiring layout to connect a 240V AC load appliance (such as a dryer or water heater). The hot wires for 240V are immediately linked to the two pole, single throw switch and the load point as it is illustrated because there is no need to wire the neutral wire. The dryer is directly linked to the ground wire. When turned OFF, the DPST switch will cut both hot wires. Similar to that, while in the ON position, it will join both Hot wires.DPST Toggle Switch Circuit Diagram SPDT Toggle SwitchA single conductor connection with any of two additional single conductors can be made or broken using a toggle switch known as an SPDT, or single pole, double throw. These switches typically have three terminals, which are typically utilized in pairs. Any load must be connected to the first terminal in order to power a specific gadget. To power the loads on Terminals 1 and 3, Terminal 2 receives the necessary power, while Terminal 3 is utilized to connect to any load and turn on any device. This switch can therefore power any of two circuits. Three-way switches are another name for these kinds of switches.SPDT Switches Three terminals make up an SPDT toggle switch. Any load can be connected to Terminal 1 in order to power a specific appliance. Furthermore, any load can be connected to terminal 3 to power any device. The power required to power the loads on terminals 1 and 3 is delivered to terminal 2 at this point. A SPDT switch can therefore operate any of two circuits. It can switch between the two circuits so that various gadgets or circuits can be powered with just a flick of the switch. The circuit for an SPDT toggle switch is illustrated below:SPDT Toggle Switch Circuit DiagramWe link our 9-volt DC power source to terminal 2 in this circuit. The toggle switch at terminal 2 allows us to switch between terminals 1 and 3. A fan is attached to Terminal 1. The DC motor does not operate when the switch is flipped to the left (terminal 1). The DC motor turns on when the switch is flipped to the right (terminal 3), but the fan does not. You can see how we can control two distinct circuits or devices with an SPDT switch in this manner. A double throw switch offers two possibilities. SPST Toggle SwitchSPST, which stands for "Single Pole Single Throw," refers to a device with two terminals, such as input and output. These switches function just like an ON/OFF switch. This switch's primary job is to establish or terminate a connection between a single conductor and a single branch circuit. Once this switch is opened, the circuit will be cut off, stopping any current flow through the load. When the switch is closed, current flows through the load in a similar manner.SPST Switches A SPST toggle switch only has 2 terminals, as you can see. The input is on terminal 1. The output will be at the other terminal. Simple ON-OFF switches are what SPST toggle switches do. They interrupt the circuit when open, preventing current from reaching the load. Current can move across a closed circuit and drive the load. You can see that the DC motor may be started or stopped using this circuit simply as an ON/OFF switch. The circuit for an SPST toggle switch is illustrated below:SPST Toggle Switch Circuit Diagram LED Toggle SwitchToggle switches with illumination operate at 12 volts DC and light up to show the status of your circuit. Add some flair to your switch and control panels by using these lit toggle switches. Our assortment is completed by toggle switches with LED tips, duck bills, and longer handles. Red, blue, green, and amber are the available illumination colors. Toggle switches with illumination are available in an ON-OFF, single pole, single throw design. The switches' connections, which consist primarily of power in, power out, and a ground for the indicator light, are 1/4 inch push-on terminals. The fundamentals of how to wire an illuminated toggle switch are demonstrated in our video.You shouldn't have any trouble wiring an LED rocker switch if you pay close attention to where your ground, power, and acc pins are placed, follow the diagram below (which uses Oznium's LED Round Rocker Switch with a recommended mounting hole diameter of 3/4 inch) :LED Toggle Switch Circuit Diagram Advantages and Disadvantages of toggle switchesAdvantagesDisadvantagesUse of these switches with circuit boards is optimal.While using these switches, a clicking sound will be heard.These switches often have a small footprint, are incredibly sturdy, and are very easy to seal.When compared to rocker switches, these are bigger and bulkier.These switches are effective in controlling electricity.It requires a toggle lock washer.There are small and regular sizes of these switches available.Only low voltage circuits can use it.A lever can be used to extend and operate them.-These switches are energy efficient since they utilize less -These switches are very strong.- Rocker Switches vs Toggle SwitchesRocker switches and toggle switches are the two primary categories of maintained switches. The advantage of having a rocker switch is that you can typically add images, symbols, or even writing to the switch's face to make it more personalized. It therefore especially helpful in situations where function communication is required. Switches that can be sealed easily are perfect for usage with circuit boards. indicating increased resilience against water and dust. enhancing the switch's suitability for hostile conditions.Which switch is best for your project will ultimately depend on its design and environmental factors. From a design standpoint, some people favor the toggle switch's appearance, while others like the rocker switch's more common appearance. Your choice will be influenced by the circumstances. A toggle switch, for instance, may be preferable if the product is for the marine industry because of its IP rating. Toggle Switch ApplicationsThese switches give drivers of cars easy access to headlights, comparable controls, and indicator lights.Conveyor belts, residual current devices, and other industrial and electrical equipment employ these (RCDs).These switches are present in home, commercial, and industrial power outlets.It can serve as the primary switch for industrial machinery like conveyors and packaging machines as well as AC equipment.These switches are employed in switching headlights, logic-level programs, automobiles, and aviation control panels, among other things.Toggle switches are utilized as switching circuits in communications, commercial, and industrial equipment. Toggle Switch FAQOverview and Applications of toggle switchAn electronic on/off switch is a toggle switch. The best purpose for toggle switches is to change the status of system settings and functionalities. To allow users to select between two opposed states, toggles may be used in place of two radio buttons or a single checkbox. What are the 4 types of switches?Single pole single throw, single pole double throw, double pole single throw, and double pole double throw are the four primary categories of switches. The differences between toggle and switchSince they both manage states but not in the same way, we must first distinguish between a toggle button and a toggle switch: Toggle button: Represents an action that changes a state. Two (or more) mutually incompatible states or possibilities that can be switched are represented by a toggle switch. Should a toggle switch be on the positive or negative?Never switch the ground return side; always switch the supply side. You can flip both lines if you're using a twin pole switch, but you must keep the ground line to the chassis intact. More corrosion is likely to occur on the negative terminal. What is a toggle switch?In order to switch an electrical circuit, toggle switches include an operating lever that can be moved up and down or left and right. A "toggle" is a little wooden rod that is used in place of buttons to fasten garments.
kynix On 2022-11-15
IntroductionWhat is RS485?MaterialsMAX485 pinoutHalf duplex operationHere is how the program worksFull duplex operationHalf duplex operation codeFull duplex codeIntroductionIn digital computer communication between two computers can be made using either parallel or serial method. In parallel communication separate line is dedicated for a one-bit information to transfer. This communication is fast and easy, but it requires a lot of wires at least as many as the number of bits need to be sent in parallel. For example, to transfer a 64-bit data from one device to another, 64 data lines will be required which is impractical in embedded systems. The alternative method to transfer data is to use serial communication. In serial communication one bit at a time is transferred from one device to another one. While this method solves the wiring problem it has a lot of other problems such as bandwidth, data lagging, complex protocol, and electrical standards. There are lot of different methods to do serial communication while one method is good in one situation another one is better in another situation. In this article we will discuss RS485 communication protocol which is one of the many available serial communication methods.Materials1MAX485 module2STM32 F401CDU6What is RS485?An industry specification called RS-485 outlines the physical layer and electrical interface for point-to-point electrical device communication. RS485 is the industrial standard for communication that defines the electrical interface and physical layer for point-to-point communication. RS485 is a robust communication system it can support multiple devices on a single bus, works in a noisy environment as well and requires a maximum of 4 lines.RS485 was first developed in 1983 and has since been used in many industrial applications because of its robustness and simplicity. It has the ability to transmit data over long distances while at the same time it is cheap, thus engineers are using it in all sorts of applications such as automotive, manufacturing, and theater spaces. Nowadays almost all motor controllers, VFDs and manufacturing machines will have a port available for RS485.RS485 is actually a standard that defines the electrical characteristics of the transmitters and receivers for communication protocols. RS482 uses two lines usually called A and B which must be balanced and differential. It means that the two lines must have same impedance, nearly same length and must be differential. The key features of RS485 communication are given belowMultipoint operation10 Mbps data transfer rate at 40 feet lengthMaximum cable length is 4000 feetRS485 works both in half duplex as well full duplex mode. In half duplex mode one device can either transmit or receive data at a time. While in full duplex mode, a device can transmit and receive data at the same time. Having more than one device on a bus can cause problem when two or more devices transmit data at the same time. Therefore, software control is necessary to ensure only one device transmit data at a time.RS485 is the physical layer of communication in the OSI model. It means this layer can be used as a base for other protocols such as UART which in most application people use because UART is an asynchronous communication protocol that does not require any clock signal which make it very easy to use. In this article we will demonstrate how RS485 can be used between two STM32 microcontrollers to communicate and exchange data. We will be using MAX485 module which is an easily available RS485 module. MAX485 pinoutRO → Receiver outputRE → Receiver enableDE → Data enableDI → Data inputVCC → Input voltageGND → GroundA, B → RS485 differential linesHalf duplex operationIn half duplex operation either data can be received or transmitted at a time. Both operations cannot be done at the same time. MAX485 has data flow control pins called DE and RE which puts the module in receiver mode or in transmit mode. Making them low puts the module in receiving mode while making them high puts the device in transmitter mode.In CubeMX the microcontroller of our choice is selected which in our case is STM32 F401CDU6. In connectivity UART1 should be enabled with 115200 bps baud rate. Other necessary settings are given below.RCC → Crystal/Ceramic ResonatorSYS → Debug → Serial WireClock Configuration → HCLK → 84 MHzClock Configuration → PLL Source Mux → HSEGPIO A7 is set as outputHere is how the program worksThe setup has two microcontrollers. We will call one side as A and the other side as B. When a user presses the user key on A STM32 microcontroller it will send the information to the B microcontroller via RS485. The receiving B microcontroller will switch on the onboard LED and will responds with an OK message. The OK message will blink the led on A microcontroller twice. Similarly, when the user presses key on B microcontroller it will transmit a message to A microcontroller and turns on the onboard LED and will responds with an OK message. The OK message will blink LED on B microcontroller twice. Similarly pressing the button again will do the same except this time it will turn off the LED.Full duplex operationIn full duplex operation data can be received or transmitted at the same time. Both operations can be done at the same time. In this mode two MAX485 modules will be required at each end and overall, 4 MAX485 modules will be used. It means that the two MAX485 modules will be constantly in receiving mode while the other two will constantly in transmission mode. MAX485 has data flow control pins called DE and RE which puts the module in receiver mode or in transmit mode. We will put the data control pins of two module as high while put the data control pins of other two module low. The configuration is shown below.The program works the same way as it was working in the half duplex mode however, this time the transmitted and received by MCUs at the same time.Half duplex operation code#include "main.h" UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */int8_t R_Data[1] = {0};int8_t T_Data[1] = {69};/* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_USART1_UART_Init(void); int main(void){ HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); //Put RS485 module in receiving mode HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); //Turn Off LED pin while (1) { HAL_UART_Receive(&huart1, R_Data, 1, 10); // If button is pressed on the other MCU if(R_Data[0] == 83) { HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); //Toggle LED pin HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); //Put RS485 module in transmission mode HAL_UART_Transmit(&huart1, T_Data, 1, 10); //Send acknowledgment HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); //Put RS485 module in transmission mode R_Data[0] = 0; } // If OK message is receive if(R_Data[0] == 69) { if (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13)) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); } R_Data[0] = 0; } // Button is pressed if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); //Put RS485 module in transmission mode T_Data[0] = 83; HAL_UART_Transmit(&huart1, T_Data, 1, 10); T_Data[0] = 69; HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); //Put RS485 module in Receiving mode } } /* USER CODE END 3 */}Full duplex code#include "main.h" UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */int8_t R_Data[1] = {0};int8_t T_Data[1] = {69};/* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_USART1_UART_Init(void); int main(void){ HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); //Turn Off LED pin while (1) { HAL_UART_Receive(&huart1, R_Data, 1, 10); // If button is pressed on the other MCU if(R_Data[0] == 83) { HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); //Toggle LED pin HAL_UART_Transmit(&huart1, T_Data, 1, 10); //Send acknowledgment R_Data[0] = 0; } // If OK message is receive if(R_Data[0] == 69) { if (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13)) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); } R_Data[0] = 0; } // Button is pressed if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)) { T_Data[0] = 83; HAL_UART_Transmit(&huart1, T_Data, 1, 10); T_Data[0] = 69; } } /* USER CODE END 3 */}
Victoria On 2022-10-26
What is a LED?Video related to LEDLED Colours and materialsHow do LEDs work?Types of LedsCalculating LEDs resistor valueHow to Test LED LightsThe warning of LEDs useLEDs FAQWhat is a LED?LED = Light Emitting Diode. An LED must be prevented against transferring too much current because its electrical behavior differs significantly from that of a light. Typically, this is done by connecting a resistor in series with the LED. Never attach an LED directly to a power source or battery.LEDs must be wired in the proper direction; the diagram may be labeled with the letters an or + for the anode and k or - for the cathode (yes, it really is k, not c, for cathode). In the case of spherical LEDs, the cathode is the short lead and there may be a slight flat on the body. Although the cathode is the larger electrode within the LED if you can see it, this is not a recognized method of identification.LEDs Video related to LEDVideo Description: This video is mainly talk about how to design LED circuits, how to calculate resistor size, how to protect LED, how long will a battery power a circuit, how to calculate resistor power rating, how to connect LED and much more. LED Colours and materialsThe semiconductor material, not the coloring of the "package," determines the color of an LED (the plastic body). All colors of LEDs are available in uncolored, diffused (milky), or clear (commonly referred to as "water clear") packaging. The colored packaging is also offered in diffused (the typical type) and clear forms. White and blue LEDs could cost more than the other colors.ColorWavelength (nm)Voltage Drop (V)Semiconductor MaterialInfrared> 760< 1.9Gallium ArsenideInfrared> 760< 1.9Aluminium Gallium ArsenideRed610 - 7601.6 -2.0Aluminium Gallium ArsenideRed610 - 7601.6 -2.0Gallium Arsenide PhosphideRed610 - 7601.6 -2.0Aluminium Gallium Indium PhosphideRed610 - 7601.6 -2.0Gallium PhosphideOrange590 - 6102.0 -2.1Gallium Arsenide PhosphideOrange590 - 6102.0 -2.1Aluminium Gallium Indium PhosphideOrange590 - 6102.0 -2.1Gallium PhosphideYellow570 - 5902.1 -2.2Gallium Arsenide PhosphideYellow570 - 5902.1 -2.2Aluminium Gallium Indium PhosphideYellow570 - 5902.1 -2.2Gallium PhosphideGreen500 - 5701.9 -4.0Gallium Indium PhosphideGreen500 - 5701.9 -4.0Aluminium Gallium Indium PhosphideGreen500 - 5701.9 -4.0Aluminium Gallium PhosphideGreen500 - 5701.9 -4.0Indium Gallium NitrideBlue450 - 5002.5 -3.7Zinc SelenideBlue450 - 5002.5 -3.7Indium Gallium NitrideBlue450 - 5002.5 -3.7Silicon CarbideBlue450 - 5002.5 -3.7SiliconViolet400 - 4502.8 -4.0Indium gallium NitridePurplemultiple types2.4 -3.7Dual Blue/Red LEDsPurplemultiple types2.4 -3.7Blue with Red PhosphorPurplemultiple types2.4 -3.7White with Purple Plasticultraviolet< 4003.1 -4.4Diamondultraviolet< 4003.1 -4.4Boron Nitrideultraviolet< 4003.1 -4.4Aluminium Nitrideultraviolet< 4003.1 -4.4Aluminium Gallium Nitrideultraviolet< 4003.1 -4.4Aluminium gallium Indium NitridePinkmultiple types3.3Blue with phosphorPinkmultiple types3.3Yellow with Red, Orange or Pink phosporPinkmultiple types3.3White with Pink pigmentWhiteBroad spectrum3.5Blue/UV diode with Yellow Phosphor How do LEDs work?A P-type semiconductor (which has a higher hole concentration) and an N-type semiconductor are combined to create LEDs, which are semiconductor light sources (larger electron concentration). The P-N junction's electrons and holes will join once more when a strong enough forward voltage is applied, releasing energy in the form of light.LEDs (Light Emitting Diodes) transform electrical energy directly into light as opposed to conventional light sources, which first convert electrical energy into heat before turning it into light. This results in efficient light creation with minimal electricity waste.LEDs Emit Light Types of LedsDual In-Line Package (DIP) LEDs:The first LED chips were DIP ones, which are what most people think of when considering LED lights. Despite being more established than its more recent counterparts, DIP LED chips are still in use and are more frequently seen integrated into electronics because of their small size. However, they are not very strong and can only provide a small amount of brightness.DIP LEDs Surface Mounted Diode (SMD) LEDs:These are likely the most popular sort of LED chip available; they are installed and soldered onto the circuit board. They are more adaptable when it comes to encasing them within smaller electronics or across other forms of lighting, such as strip lighting, because they are brighter than their DIP counterparts and are also smaller. Three diodes can fit on a single SMD chip, allowing you to produce a variety of colors and provide customers more options. The LED market has undergone this significant progress. SMD 3528 and SMD 5050, both of which measure 5mm in width, are the two most used SMD chip sizes.SMD LEDs Chip on Board (COB) LEDs:The most recent advancement in LED technology is represented by these chips. Out of the three, COB LED chips are the brightest since they can frequently fit nine or more diodes onto a single chip. In what ways does this affect LED lighting? First off, it increases lighting efficiency by improving brightness-to-energy output. They can therefore be utilized with a variety of various lighting types. However, it's important to keep in mind that a COB LED chip's circuitry prevents it from emitting a wide variety of colors.COB LEDs Calculating LEDs resistor valueTo limit the current flowing through an LED, a resistor must be connected in series with the LED; otherwise, the LED will burn out fairly immediately. R, the resistor's value, is determined by:R = (VS - VL) / IR = resistor value in ohms (ohm).VS = supply voltage.VL = LED voltage (2V, or 4V for blue and white LEDs).I = LED current in amps (A) The LED current needs to be lower than what your LED is capable of handling. Since the The maximum current for typical 5mm diameter LEDs is frequently 20mA; however, many circuits can work with 10mA or 15mA. Divide the mA current by 1000 to convert it to amps (A) for the calculation.If the projected value is unavailable, pick the nearest larger standard resistor value so that the current will be a little less than what you chose. If you choose a higher resistor value to reduce the current, the LED will be less bright (for example, to extend the battery life).The color of the LED affects the voltage VL of the LED. The voltage of red LEDs is the lowest; yellow and green have a somewhat higher value. The highest voltages are used in blue and white LEDs. You can use 2V for red, yellow, and green LEDs and 4V for blue and white LEDs for the majority of applications where the precise value is not crucial. According to Ohm's law, the resistor's resistance, R = V/I, is determined by:V = voltage across the resistor (= VS - VL in this case) I = the current through the resistorSo R = (VS - VL) / IResistor Value How to Test LED LightsStep One: Use a MultimeterGet a digital multimeter with a diode reading capability. Simple multimeters only measure voltages, amps, and ohms. A multimeter with a diode setting is required to test LED lighting. Mid-range to high-range multimeters, which are more likely to offer this capability than affordable versions, can be found online or at your neighborhood hardware store.Multimeter Step Two: Connect the black and red test leadsTo the outlets on the front of the multimeter, attach the red and black test leads. The positive charge is in the red lead. The input marked "COM" should be connected in with the black lead, which is the negative.Multimeter Connect Step Three: Select the diode setting on the multimeter's dialTo move your multimeter's front dial from the "off" position, turn it clockwise. Up till you reach the diode setting, keep twisting it. The diode setting may be represented by the diode circuit symbol if it is not labeled explicitly. The cathode and the anode of a diode are both visually represented by the diode symbol. In this digital multimeter dial picture, we need to set the multimeter’s dial on 14 to test diode.Multimeter dial Step Four: The red probe should be connected to the anode and the black probe to the cathodeThe cathode end of the LED, which is typically the shorter prong, should be touched with the black probe. The red probe should then be pressed against the anode, which is the longer prong. Ensure that the black probe is connected before the red probe because doing so can result in inaccurate readings. During this test, be sure the cathode and anode are not in contact with one another since this could prevent electricity from flowing through the LED light and affect your results. Throughout the test, the red and black probes must not come into contact. After making the connections, the LED ought should turn on.Diode test Step Five: Verify the reading on the digital multimeter displayA healthy LED light should show a voltage of about 1600 mV when the probes are in contact with the cathode and anode. If during the test there is no reading displayed on your screen, repeat the procedure to ensure that the connections were completed correctly. This can indicate that the LED light isn't functioning if the test was done correctly. The transformer needs to be changed if your supply does not provide any output voltage. LED lights need to be replaced if there is voltage present at the output. The warning of LEDs useIn general, it is not a good idea to connect multiple LEDs in parallel with just one resistor shared between them. Only the lowest voltage LED will light if the other LEDs require slightly different voltages, and the higher current running through it could damage the other LEDs. One resistor can be used to successfully link identical LEDs in parallel, but since resistors are so inexpensive and the current utilized is the same as connecting the LEDs separately, this rarely provides any significant benefit.LEDs in parallelInstead, we should do as follows: Connecting LEDs in seriesConnecting LEDs in series LEDs FAQWhat can the LEDs be applied to?LEDs (Light Emitting Diodes) are mostly used to illuminate items and even spaces. Due to its small size, low energy consumption, long lifespan, and versatility in terms of use in many applications, it is applied everywhere. LED usage and applications include TV backlighting. How many types that LEDs own?Fundamentally, LED lighting uses three major forms of LED technology: DIP, SMD, and COB. What is LED and how it works?When an electric current passes through a semiconductor device called a light-emitting diode (LED), the LED emits light. When current flows through an LED, the electrons and holes recombine and produce light. How long do LED lights last?The longer lifespan of LED lighting fixtures is one of its main benefits. The most durable LED light fixtures have been evaluated to survive as long as 100,000 hours, whereas incandescent light bulbs were designed to last roughly 1,000 hours. On average, LED light bulbs last at least 20 years before needing to be replaced. Which is not a benefit of LED?On a capital cost basis, LEDs are now more expensive (price per lumen) than the majority of conventional lighting solutions.
kynix On 2022-10-17
Keypads are input devices that are being widely used in many embedded system projects. It can be found in appliances, door locks, and industrial machines. Keypads are used to take input from the user in the form of numbers or characters which can further be used for processing such as password, menu selection and navigating among different options. One of the most common and low-cost keypads is the matrix keypad with 4×4 or 3×3 buttons. In this article we will discuss how a low cost 4×4 matrix keypad can be used in STM32. Before proceeding further, we will need to know few things.Materials14×4 matrix keypad2STM32 F401How a 4×4 keypad works? 4×4 keypadIn microcontrollers usually a pin is used to take input from the user. This input can be either 1 or 0. Multiple 1’s and 0’s can be combined to store more information. For this purpose, multiple input pins will be required. However, this becomes impractical when the input pins required exceed certain number such as 16 or 9 as the microcontrollers do not have this many pins available.The 4×4 matrix keypad solves this problem and reduced the required number of pins to 8 or 6. It is made of a thin and flexible membrane. The 16 keys of the 4 x 4 keypad module are arranged in a matrix of rows and columns. A copper trace connects each of these switches to the others. The rows and column are not connected to each other in normal condition. When we push a key, a column and a row come into contact with each other. In matrix keypads the buttons are divided among rows and columns. Four buttons lie on each row and each column. Thus, columns are connected to external input pins of microcontroller while the rows are connected to output pins of microcontroller. The output pins are high all the time. When a button is pressed the corresponding column goes high and the microcontroller detects it. Finding which column has been activated is easy as each column is connected to a separate pin, however, finding a row is difficult. Once both row and column are identified then the corresponding button can be identified. A clever method to identify the pressed key is to switch off all output pins except one, and then check which input pins is high. Doing this for all the output pins will identify the row. Once the row number is found out, the button can easily be traced out.matrix keypad in STM32STM32 F401 implementationSTM32 F401In CubeMX the relative microcontroller of our choice is selected which in our case is STM32 F401CDU6. The GPIOs that need to be selected as output or external interrupt input are given in the figure above. In the NVIC tab the interrupt should be enabled. Other necessary settings are given below.RCC → Crystal/Ceramic ResonatorSYS → Debug → Serial WireClock Configuration → HCLK → 84 MHzClock Configuration → PLL Source Mux → HSE Once the CubeMX code is generated the following code should be added to the /* USER CODE BEGIN PV */ section./* USER CODE BEGIN PV */ GPIO_InitTypeDef GPIO_InitStructPrivate = {0}; uint32_t previousM = 0; uint32_t currentM = 0; uint8_t key = 0; uint8_t InputData[5] = {0}; int i = 0;/* USER CODE END PV */This code section defines the necessary variables that will come handy later.In the main.c section in /* USER CODE BEGIN 2 */ the output configured pins should be set to 1. /* USER CODE BEGIN 2 */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1); /* USER CODE END 2 */ While the interrupt callback function void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) should be added to /* USER CODE BEGIN 4 */ section.void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){ currentM = HAL_GetTick(); if (currentM - previousM > 10) { /*Configure GPIO pins : PB6 PB7 PB8 PB9 to GPIO_INPUT*/ GPIO_InitStructPrivate.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStructPrivate.Mode = GPIO_MODE_INPUT; GPIO_InitStructPrivate.Pull = GPIO_NOPULL; GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0); if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)) { key = 68; //ASCII value of D } else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)) { key = 67; //ASCII value of C } else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) { key = 66; //ASCII value of B } else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9)) { key = 65; //ASCII value of A } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0); if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)) { key = 35; //ASCII value of # } else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)) { key = 57; //ASCII value of 9 if (i == 4) { //Send Data } else { InputData[i] = 9; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) { key = 54; //ASCII value of 6 if (i == 4) { //Send Data } else { InputData[i] = 6; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9)) { key = 51; //ASCII value of 3 if (i == 4) { //Send Data } else { InputData[i] = 3; } if(i <= 4) { i = i + 1; } else { i = 0; } } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0); if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)) { key = 48; //ASCII value of 0 if (i == 4) { //Send Data } else { InputData[i] = 0; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)) { key = 56; //ASCII value of 8 if (i == 4) { //Send Data } else { InputData[i] = 8; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) { key = 53; //ASCII value of 5 if (i == 4) { //Send Data } else { InputData[i] = 5; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9)) { key = 50; //ASCII value of 2 if (i == 4) { //Send Data } else { InputData[i] = 2; } if(i <= 4) { i = i + 1; } else { i = 0; } } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1); if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)) { key = 42; //ASCII value of * } else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)) { key = 55; //ASCII value of 7 if (i == 4) { //Send Data } else { InputData[i] = 7; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) { key = 52; //ASCII value of 4 if (i == 4) { //Send Data } else { InputData[i] = 4; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9)) { key = 49; //ASCII value of 1 if (i == 4) { //Send Data } else { InputData[i] = 1; } if(i <= 4) { i = i + 1; } else { i = 0; } } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1); /*Configure GPIO pins : PB6 PB7 PB8 PB9 back to EXTI*/ GPIO_InitStructPrivate.Mode = GPIO_MODE_IT_RISING; GPIO_InitStructPrivate.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate); previousM = currentM; }} In the callback function the two linescurrentM = HAL_GetTick(); if (currentM - previousM > 10)takes care of the debouncing of the buttons. In keypad it is a common problem that a but hits once is recorded twice or thrice. So, take of that, a little delay is added at the beginning of callback function.The complete code is given below.#include "main.h" /* USER CODE BEGIN PV */ GPIO_InitTypeDef GPIO_InitStructPrivate = {0}; uint32_t previousM = 0; uint32_t currentM = 0; uint8_t key = 0; uint8_t InputData[5] = {0}; int i = 0;/* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);/* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*//* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */int main(void){ /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */} /* USER CODE BEGIN 4 */void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){ currentM = HAL_GetTick(); if (currentM - previousM > 10) { /*Configure GPIO pins : PB6 PB7 PB8 PB9 to GPIO_INPUT*/ GPIO_InitStructPrivate.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStructPrivate.Mode = GPIO_MODE_INPUT; GPIO_InitStructPrivate.Pull = GPIO_NOPULL; GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0); if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)) { key = 68; //ASCII value of D } else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)) { key = 67; //ASCII value of C } else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) { key = 66; //ASCII value of B } else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9)) { key = 65; //ASCII value of A } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0); if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)) { key = 35; //ASCII value of # } else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)) { key = 57; //ASCII value of 9 if (i == 4) { //Send Data } else { InputData[i] = 9; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) { key = 54; //ASCII value of 6 if (i == 4) { //Send Data } else { InputData[i] = 6; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9)) { key = 51; //ASCII value of 3 if (i == 4) { //Send Data } else { InputData[i] = 3; } if(i <= 4) { i = i + 1; } else { i = 0; } } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0); if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)) { key = 48; //ASCII value of 0 if (i == 4) { //Send Data } else { InputData[i] = 0; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)) { key = 56; //ASCII value of 8 if (i == 4) { //Send Data } else { InputData[i] = 8; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) { key = 53; //ASCII value of 5 if (i == 4) { //Send Data } else { InputData[i] = 5; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9)) { key = 50; //ASCII value of 2 if (i == 4) { //Send Data } else { InputData[i] = 2; } if(i <= 4) { i = i + 1; } else { i = 0; } } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1); if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)) { key = 42; //ASCII value of * } else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)) { key = 55; //ASCII value of 7 if (i == 4) { //Send Data } else { InputData[i] = 7; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) { key = 52; //ASCII value of 4 if (i == 4) { //Send Data } else { InputData[i] = 4; } if(i <= 4) { i = i + 1; } else { i = 0; } } else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9)) { key = 49; //ASCII value of 1 if (i == 4) { //Send Data } else { InputData[i] = 1; } if(i <= 4) { i = i + 1; } else { i = 0; } } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1); /*Configure GPIO pins : PB6 PB7 PB8 PB9 back to EXTI*/ GPIO_InitStructPrivate.Mode = GPIO_MODE_IT_RISING; GPIO_InitStructPrivate.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate); previousM = currentM; }}
Allen On 2022-10-17
Catalog Introduction How does an LDR work? How to setup ADC in STM32 Introduction The majority of streetlights and outdoor lights are typically operated manually. To manually turn on and off lights is not only risky, but it also wastes energy well as the timing of turning on and off is not optimized. Therefore, an optimized, efficient, and automatic light system is needed to efficiently control light brightness and turn on and off them automatically. In this article, a brief introduction to automatic control of light brightness is given as well as its practical implementation using an STM32 microcontroller and a cheap LDR sensor shown in Figure 1 is demonstrated. Figure 1 LDR breakout board In an automatic light control system, a light detection system is employed that senses the light intensity. If the application requires only to turn on and off a light system, then a threshold value of light intensity is set below which the light will turn on, and above it, the light will turn off. However, if the application is to control the light brightness based on the light intensity in the environment, then a PWM-controlled voltage is provided to the automatic light system. For light detection, a Light Dependent Resistor commonly known as LDR is used. LDR is a sensor whose resistance varies with the intensity of light. This property of an LDR can be used to sense darkness and brightness. Thus, it can be used to automatically control the turn on and off as well as the intensity of the light system. A typical LDR has a maximum resistance value in mega ohms and a minimum resistance value in several kilo ohms. Materials 1STM32 F401/F1032LDR sensor3Potentiometer4LED How does an LDR work? So, how exactly does an LDR operate? LDR works on the principle of photoconductivity. It is an optical phenomenon in which material conductivity increases when light falls upon it. When light or photon strikes the material, the electrons in the semiconductor material's valence band are stimulated to the conduction band. The incident photons must have energy larger than the bandgap of the semiconductor material to cause the electrons to move from the valence band to the conduction band. Hence as light intensity increases more and more electrons are excited to the conduction band which produces a large number of charge carriers. This means that more current will flow in the circuit, and as a result, the resistance will decrease. LDR resistance that changes with the intensity of light cannot be read in a microcontroller. To make it readable in a microcontroller the resistance is represented in terms of voltage. For this purpose, a circuit needs to be designed. Many circuits can be used for LDR. These can be based upon MOSFET, BJET, or an amplifier. However, the most commonly used circuit for LDR to convert its resistance into voltage is the voltage divider circuit. In this circuit, two resistors are installed in series. One side is attached to the positive terminal of the battery while the other is attached to the ground. The schematic of the voltage divider is shown in Figure 2. The output of the voltage divider can be fed to another circuit for other purposes such as a comparator i.e LM393. Usually, a comparator is used in on-off operations where the lights are needed to be turned on and off when a threshold value of light intensity is absorbed by the LDR. A typical circuit for the LM393 comparator is shown below. Figure 2 LM393 comparator usage with LDR The calculation for the voltage divider circuit is pretty easy. Referring to Figure 2, the following equation can be used to measure the output voltage. In this equation, it is assumed that there is no load on the output voltage because that load can affect the output voltage. The output of the circuit is shown in Figure 2 where the change in resistance changes the voltage at the IN1+ pin of the comparator. As we know the voltage changes with the intensity of light. The circuit gives maximum voltage in complete darkness while minimum voltage when placed in bright light. The ADC of the STM32 controller can be used to sense the change in the voltage while the results obtained via ADC can be used to generate PWM. It is the PWM that generates average voltage and hence controls the intensity of light. In this article, both the manual and automatic light intensity control is demonstrated using an LED light. The program and procedure for automatic and manual light brightness control is same, the only difference is that in automatic light brightness control and LDR is used while in manual mode simple potentiometer is used. How to setup ADC in STM32 In STM32, ADC can be configured in three different ways. 1) Polling 2) Interrupt 3) DMA. Polling: In the polling method when ADC conversion starts the CPU operation halts. It is only after the conversion is completed, the CPU resumes working. Interrupt: The second method is by using the interrupt service routine. When ADC conversion competes, it generates an interrupt during which required functions are executed which in our case is to update the PWM value. DMA: The third method is to use direct memory access (DMA). In this method, the ADC directly transfers the data to memory bypassing the CPU altogether. This is the most efficient method of all as it does not involve CPU in the ADC operations and keeps it available for other tasks. In this experiment, we will be using interrupt methods which are both simple and efficient. Required hardware STM32 F401/F103LDR sensor (breakout board will be better)PotentiometerLED Let's build the program step by step Open STM32CubeIDE and start a new projectSelect an MCU which in our case is STM32F401CDGo to SYS -> Debug and select Serial Wire. Select SystTick in TimeBase Source. Go to RCC-> High Speed Clock and select Crystal/Ceramic Resonator. Configure ADC1. Select IN1 and set it to be triggered by software. From the NVIC controller tab check the global interrupt box. Configure Timer 1 in PWM mode with output on CH1. Set the counter period register value to 839 and Prescaler register value to 100. This will ensure 1000 Hz frequency at the output. The following formula can be used to set PWM frequency Setting Prescaler value to 99 while the required frequency is 1000 Hz, the ARR value can be calculated as 839. Finally set the clock frequency to 84 MHz and select HSE as the clock source. And generate the code. The final code is given below #include "main.h"ADC_HandleTypeDef hadc1;TIM_HandleTypeDef htim1;void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_ADC1_Init(void);static void MX_TIM1_Init(void);uint16_t AD_Data = 0; uint16_t minimumADC = 1000; uint16_t maximumADC = 3000;int main(void){ HAL_Init() SystemClock_Config(); MX_GPIO_Init(); MX_ADC1_Init(); MX_TIM1_Init(); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); while (1) { HAL_ADC_Start_IT(&hadc1); TIM1->CCR1 = ((AD_Data-minimumADC)*840)/maximumADC; }}void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){ AD_Data = HAL_ADC_GetValue(&hadc1);} Figure 3 Duty Cycle in Bright Light Figure 4 Duty Cycle in Low Light Resources Automatics Light.zip
Victoria On 2022-10-06
Join our mailing list!
Be the first to know about new products, special offers, and more.
Feature Posts
How Resistors Work: From Basic Principles to Advanced Applications2025-07-30
DC Switching Regulators: Principles, Selection, and Applications2025-05-30
FPGA vs CPLD: In-depth Analysis of Architecture, Performance and Application2025-05-07
MOSFET Technology: Essential Guide to Working Principles & Applications2025-05-04
SMD Resistor: Types, Applications, and Selection Guide2025-04-30