How to control relay 5v with nanoframework?

240 views Asked by At

everyone. I am trying to control relais 5v with nanoframework on esp32. The problem that I get is that the relay is still in action when I connect pin 21 with the In pin of the relay, even though the esp32 pin is in PinValue.Low. My thanks and appreciation to everyone who try to help. Hers is my code:

using System;
using System.Diagnostics;
using System.Threading;
using System.Device.Gpio;
using nanoFramework.Hardware.Esp32;


namespace NFApp3
{
    public class Program
    {
        //private static GpioController s_GpioController;

        public static void Main()
        {
            GpioController s_GpioController =new GpioController();
            GpioPin led = s_GpioController.OpenPin(21,PinMode.Output);
            led.Write(0);
            //led.Write(PinValue.High);
            Thread.Sleep(Timeout.Infinite);

        }


    }
}
2

There are 2 answers

1
José Simões On

You should be using led.Write(PinValue.Low); instead of writing 0.

1
si3012 On

After thinking for a while, I found this solution:

using System;
using System.Diagnostics;
using System.Threading;
using System.Device.Gpio;
using nanoFramework.Hardware.Esp32;
using Iot.Device.Rtc;
using System.Device.I2c;

namespace NFApp3
{
    public class Program
    {
        //private static GpioController s_GpioController;

        public static void Main()
        {
            GpioController s_GpioController =new GpioController();
            GpioPin led = s_GpioController.OpenPin(21);
            led.SetPinMode(PinMode.Input);
            Console.WriteLine("Sleep");
            Thread.Sleep(300);
            while (true)
            {
                led.SetPinMode(PinMode.Output);
                led.Toggle();
                Console.WriteLine("High");
                Thread.Sleep(3000);
                led.SetPinMode(PinMode.Input);
                Thread.Sleep(2000);
                Console.WriteLine("Low");
            }
            Thread.Sleep(Timeout.Infinite);

        }


    }
}

Hoping to be usefull to anyone.