Uln2003 and 28byj-48 code

Hey. I need help with my code. Are stepping sequences and code good or should I change anything? So my question is how do I get best torque from 5v, 28byj-48 motor? Speed is not important to me. Thanks for your help!


#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

  int val;
  int lightbulb = 7;
  int sec = 50;
  int min = 59;
  int i=0;



    #define A 8
    #define B 9
    #define C 10
    #define D 13
 
    #define NUMBER_OF_STEPS_PER_REV 2048


       // #include <Adafruit_Sensor.h>    //   POIZKUSI BREZ
        #include <DHT.h>

        #define DHTPIN 6
        #define DHTTYPE DHT22   // DHT 22  (AM2302)
        DHT dht = DHT(DHTPIN, DHTTYPE);




byte tem[8] =             //thermometer
{
    B00100,
    B01010,
    B01010,
    B01110,
    B01110,
    B11111,
    B11111,
    B01110
};

byte drop[8] =            //droplet
{
    B00100,
    B00100,
    B01010,
    B01010,
    B10001,
    B10001,
    B10001,
    B01110,
};

byte bulb[8] =            //light bulb
{
    B01110,
    B10001,
    B10001,
    B10001,
    B01110,
    B01110,
    B01110,
    B00000
};

byte degree[8] =            //degree
{
    B01100,
    B10010,
    B10010,
    B01100,
    B00000,
    B00000,
    B00000,
    B00000
};
  
byte Motor[8] =             //motor
{
    B00010,
    B11111,
    B00010,
    B00000,
    B10001,
    B11011,
    B10101,
    B10001
};


void setup() {
  Serial.begin(9600); 
  
    dht.begin();
  
      lcd.begin(16,2);
      lcd.createChar(1,tem);       
      lcd.createChar(2,drop);       
      lcd.createChar(3,bulb);
      lcd.createChar(4,degree);
      lcd.createChar(5,Motor);


        pinMode(A,OUTPUT);
        pinMode(B,OUTPUT);
        pinMode(C,OUTPUT);
        pinMode(D,OUTPUT);
  
delay(2000);
}



 void write(int a,int b,int c,int d){
  digitalWrite(A,a);
  digitalWrite(B,b);
  digitalWrite(C,c);
  digitalWrite(D,d);
}

  void onestep(){
    write(1,1,0,0);
    delay(2);
    write(0,1,0,0);
    delay(2);
    write(0,1,1,0);
    delay(2);
    write(0,0,1,0);
    delay(2);
    write(0,0,1,1);
    delay(2);
    write(0,0,0,1);
    delay(2);
    write(1,0,0,1);
    delay(2);
    write(1,0,0,0);
}


void loop() {



    
 // Read the humidity in %:
  float h = dht.readHumidity();
  // Read the temperature as Celsius:
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again):
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

 

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" % ");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" \xC2\xB0");
  Serial.println("C | ");



  
lcd.setCursor(2,0);  
   lcd.print(t); 
    lcd.write(4);
     lcd.print("C");

lcd.print("   ");
  lcd.setCursor(0,0);
lcd.write(1);   
      
      lcd.setCursor(2,1);  
   lcd.print(h); 
   lcd.print("%");

lcd.print("   ");
  lcd.setCursor(0,1);
lcd.write(2);         
   
       
  if(t<37.5){
   analogWrite(lightbulb,0); 
     lcd.setCursor(15,0);
   lcd.write(3);}


    if(t>38.5){
    analogWrite(lightbulb,255); 
       lcd.setCursor(15,0);
       lcd.print(" ");

}


     
  sec=sec+1;
  delay (825);
  
  lcd.setCursor(11,1);  
  if(min<10)lcd.print("0");
 lcd.print(min);
 lcd.print(":");
 if(sec<10)lcd.print("0");
 lcd.print(sec);
    if(sec>58){
  sec=-1;
  min=min+1;
 }
 
  if(min>59){
    lcd.setCursor(11,1);
    lcd.print("00:00");
    lcd.setCursor(13,0);
   lcd.write(5);

    
  delay(10);

  
  
    
 while(i<NUMBER_OF_STEPS_PER_REV){
onestep();
i++;
}

  if(min>59){
      min=0;}
 if(i=2048){ i=0;}

  }

    
     lcd.setCursor(13,0);
       lcd.print(" ");
    
  }

Does the code work?

If so, that is about the best you can do with that motor and driver. It is not a high torque motor.

Tell, or show by schematics, how You power the motor.

It should work.. I changed just something from dht22 (temp. sensor), but yes it was working. I just dont know if that part Void onestepp is good or could it be better..

Yeah, sorry i totaly forgot. I am powering uln 2003 with external power (5v, 0.5A). Sorry for this image I know I should create a scheme:
image

External powering is needed. Good! However I feel 0.5 amp is a bit too little for the motor to start. However, if the 5 volt supply doesn't cut off and it works, You're okey.

No problem with the painting showing the colour of things and what they look like. Joking but using space to show the shape and colour of things is the weak point of Fritzings.

Use a modern MOSFET based driver! The Uln2003 uses old fashioned silicon transistors wasting 0.7 - 1 volt. MOSFETs waste a lot less.

Is that part of code okay? Are delays okay or should they be longer? And is better to use full-step or half-step? Would it help if I used 6V external power?

void onestep(){
    write(1,1,0,0);
    delay(2);
    write(0,1,0,0);
    delay(2);
    write(0,1,1,0);
    delay(2);
    write(0,0,1,0);
    delay(2);
    write(0,0,1,1);
    delay(2);
    write(0,0,0,1);
    delay(2);
    write(1,0,0,1);
    delay(2);
    write(1,0,0,0);

Start by making onestep work.
I can't tell what that code is trying to do.
The delays look too small to me. For bigger steppers one step per 10 mS usually works as first try.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.