{"id":190150,"date":"2025-02-11T07:40:26","date_gmt":"2025-02-11T07:40:26","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=190150"},"modified":"2025-02-11T07:40:29","modified_gmt":"2025-02-11T07:40:29","slug":"traffic-light-controller-finite-state-machine","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/11\/traffic-light-controller-finite-state-machine\/","title":{"rendered":"Traffic Light Controller Finite State Machine"},"content":{"rendered":"\n<p>4 TLC FSM: Traffic Light Controller Finite State Machine 6 library IEEE; 7 use IEEE. STD LOGIC 1164.ALL; 9 L0 entity TLC FSM is Port ( clk in std logic; in std logic; in std logic; rst intervalDone: selGreenInterval : out std_logic; lampVec: L2 13 1 4 15 6 Lend TLC FSM; L7 18 E] architecture RTL of TLC FSM is 9 type stateType is (GnsRew, onsRew, RnsRew0, RnsGew, RnsOew, RnsRewl); state names 0 signal CS, NS: stateType; out std_logic_vector (5 downto 0) &#8212; state signal begin 4 stateReg_i: 7 8 end RTL;<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>It looks like you&#8217;re trying to design a Traffic Light Controller (TLC) using a Finite State Machine (FSM) in VHDL, and you need help with both the code and an explanation. From the code snippet you provided, there are several missing elements and some incomplete statements, so I\u2019ll try to clarify the logic and fix it to provide you with a correct version.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Correct VHDL Design<\/h3>\n\n\n\n<p>Here&#8217;s an improved version of your VHDL code for the Traffic Light Controller FSM:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library IEEE;\nuse IEEE.STD_LOGIC_1164.ALL;\nuse IEEE.STD_LOGIC_ARITH.ALL;\nuse IEEE.STD_LOGIC_UNSIGNED.ALL;\n\nentity TLC_FSM is\n    Port ( clk          : in std_logic;\n           rst          : in std_logic;\n           intervalDone : in std_logic;\n           selGreenInterval : out std_logic;\n           lampVec      : out std_logic_vector(5 downto 0));\nend TLC_FSM;\n\narchitecture RTL of TLC_FSM is\n    type stateType is (Green, Yellow, Red, Reset);\n    signal CS, NS: stateType;\n    signal timer : integer range 0 to 1000; -- example range for a timer\n\nbegin\n    -- State Register: Holds the current state\n    process(clk, rst)\n    begin\n        if rst = '1' then\n            CS &lt;= Reset; -- On reset, go to Reset state\n        elsif rising_edge(clk) then\n            CS &lt;= NS; -- On clock edge, update current state\n        end if;\n    end process;\n\n    -- Next State Logic: Determines the next state based on current state and inputs\n    process(CS, intervalDone)\n    begin\n        case CS is\n            when Green =&gt;\n                if intervalDone = '1' then\n                    NS &lt;= Yellow; -- If green interval is done, switch to yellow\n                else\n                    NS &lt;= Green; -- Stay in Green\n                end if;\n            when Yellow =&gt;\n                if intervalDone = '1' then\n                    NS &lt;= Red; -- If yellow interval is done, switch to red\n                else\n                    NS &lt;= Yellow; -- Stay in Yellow\n                end if;\n            when Red =&gt;\n                if intervalDone = '1' then\n                    NS &lt;= Green; -- If red interval is done, switch to green\n                else\n                    NS &lt;= Red; -- Stay in Red\n                end if;\n            when Reset =&gt;\n                NS &lt;= Green; -- Reset to Green state\n            when others =&gt;\n                NS &lt;= Reset; -- Default to Reset state\n        end case;\n    end process;\n\n    -- Output Logic: Controls the traffic light outputs\n    process(CS)\n    begin\n        case CS is\n            when Green =&gt;\n                lampVec &lt;= \"100000\"; -- Green light on\n                selGreenInterval &lt;= '1'; -- Select green interval\n            when Yellow =&gt;\n                lampVec &lt;= \"010000\"; -- Yellow light on\n                selGreenInterval &lt;= '0'; -- Select yellow interval\n            when Red =&gt;\n                lampVec &lt;= \"001000\"; -- Red light on\n                selGreenInterval &lt;= '0'; -- Select red interval\n            when Reset =&gt;\n                lampVec &lt;= \"000000\"; -- No lights on in reset\n                selGreenInterval &lt;= '0'; -- Reset interval selection\n            when others =&gt;\n                lampVec &lt;= \"000000\"; -- Default to no lights on\n                selGreenInterval &lt;= '0'; -- Default selection\n        end case;\n    end process;\n\nend RTL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<p>In this VHDL design, we are creating a finite state machine (FSM) to control the traffic light behavior. The FSM has 4 states: <strong>Green<\/strong>, <strong>Yellow<\/strong>, <strong>Red<\/strong>, and <strong>Reset<\/strong>.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>State Register (CS and NS):<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CS (Current State)<\/strong> holds the current state of the traffic light.<\/li>\n\n\n\n<li><strong>NS (Next State)<\/strong> is calculated based on the current state and input conditions (e.g., whether the interval is done).<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>State Transitions (Next State Logic):<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>From <strong>Green<\/strong>: If the interval is done (intervalDone = &#8216;1&#8217;), the system transitions to <strong>Yellow<\/strong>.<\/li>\n\n\n\n<li>From <strong>Yellow<\/strong>: If the interval is done, it transitions to <strong>Red<\/strong>.<\/li>\n\n\n\n<li>From <strong>Red<\/strong>: If the interval is done, it transitions back to <strong>Green<\/strong>.<\/li>\n\n\n\n<li><strong>Reset<\/strong>: This is a default state that clears the lights.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Output Logic (lampVec and selGreenInterval):<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The output <strong>lampVec<\/strong> controls the traffic light LEDs, using a 6-bit vector where each bit represents a light (Green, Yellow, Red).<\/li>\n\n\n\n<li>The output <strong>selGreenInterval<\/strong> indicates whether the green interval is selected.<\/li>\n<\/ul>\n\n\n\n<p>This VHDL code provides a simple, cyclic traffic light controller with states that transition based on whether the light&#8217;s timing interval is complete.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Now, I can create an image of the state machine diagram for you. Please hold on!<\/p>\n\n\n\n<p>Here is the FSM diagram for your Traffic Light Controller. It shows the states (Green, Yellow, Red, Reset) and how the system transitions between them based on events like the completion of a timing interval. You can use this as a reference to visualize the state transitions in your design.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/02\/image-312.png\" alt=\"\" class=\"wp-image-190151\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>4 TLC FSM: Traffic Light Controller Finite State Machine 6 library IEEE; 7 use IEEE. STD LOGIC 1164.ALL; 9 L0 entity TLC FSM is Port ( clk in std logic; in std logic; in std logic; rst intervalDone: selGreenInterval : out std_logic; lampVec: L2 13 1 4 15 6 Lend TLC FSM; L7 18 E] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-190150","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/190150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=190150"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/190150\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=190150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=190150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=190150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}